Skip to content

Commit

Permalink
Merge e2c0011 into 0ef45f4
Browse files Browse the repository at this point in the history
  • Loading branch information
brettz9 committed Jan 4, 2022
2 parents 0ef45f4 + e2c0011 commit 5d2ba6f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
21 changes: 19 additions & 2 deletions src/gitdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ Gitdown.read = (input) => {
markdown = Gitdown.nestHeadingIds(markdown);
}

markdown = Gitdown.prefixRelativeUrls(markdown);

await gitdown.resolveURLs(markdown);

return markdown.replace(/<!--\sgitdown:\s(:?off|on)\s-->/g, '');
Expand Down Expand Up @@ -260,6 +262,19 @@ Gitdown.readFile = (fileName) => {
return gitdown;
};

/**
* Prefixes "user-content-" to each Markdown internal link.
*
* @private
* @param {string} inputMarkdown
* @returns {string}
*/
Gitdown.prefixRelativeUrls = (inputMarkdown) => {
return inputMarkdown.replace(/\[(.*?)]\(#(.*?)\)/gm, (match, text, anchor) => {
return `[${text}](#user-content-${anchor})`;
});
};

/**
* Iterates through each heading in the document (defined using markdown)
* and prefixes heading ID using parent heading ID.
Expand Down Expand Up @@ -306,7 +321,9 @@ Gitdown.nestHeadingIds = (inputMarkdown) => {

// <code>test</code>

return '<a name="⊂⊂⊂H:' + articles.length + '⊃⊃⊃"></a>\n' + _.repeat('#', normalizedLevel) + ' ' + normalizedName;
return `<a name="user-content-⊂⊂⊂H:${articles.length}⊃⊃⊃"></a>
<a name="⊂⊂⊂H:${articles.length}⊃⊃⊃"></a>
${_.repeat('#', normalizedLevel)} ${normalizedName}`;
});

outputMarkdown = outputMarkdown.replace(/^⊂⊂⊂C:(\d+)⊃⊃⊃/gm, () => {
Expand All @@ -316,7 +333,7 @@ Gitdown.nestHeadingIds = (inputMarkdown) => {
const tree = contents.nestIds(MarkdownContents.tree(articles));

Gitdown.nestHeadingIds.iterateTree(tree, (index, article) => {
outputMarkdown = outputMarkdown.replace('⊂⊂⊂H:' + index + '⊃⊃⊃', article.id);
outputMarkdown = outputMarkdown.replace(new RegExp('⊂⊂⊂H:' + index + '⊃⊃⊃', 'g'), article.id);
});

return outputMarkdown;
Expand Down
18 changes: 16 additions & 2 deletions tests/gitdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,26 @@ describe('Gitdown', () => {
});
});

describe('prefixRelativeUrls', () => {
it('replaces relative links', () => {
expect(Gitdown.prefixRelativeUrls('A [relative](#link) test')).to.equal('A [relative](#user-content-link) test');
});
});

describe('.nestHeadingIds()', () => {
it('replaces heading markup with HTML', () => {
expect(Gitdown.nestHeadingIds('# Foo\n# Bar')).to.equal('<a name="foo"></a>\n# Foo\n<a name="bar"></a>\n# Bar');
expect(
Gitdown.nestHeadingIds('# Foo\n# Bar'),
).to.equal(
'<a name="user-content-foo"></a>\n<a name="foo"></a>\n# Foo\n<a name="user-content-bar"></a>\n<a name="bar"></a>\n# Bar',
);
});
it('nests heading ids', () => {
expect(Gitdown.nestHeadingIds('# Foo\n## Bar')).to.equal('<a name="foo"></a>\n# Foo\n<a name="foo-bar"></a>\n## Bar');
expect(
Gitdown.nestHeadingIds('# Foo\n## Bar'),
).to.equal(
'<a name="user-content-foo"></a>\n<a name="foo"></a>\n# Foo\n<a name="user-content-foo-bar"></a>\n<a name="foo-bar"></a>\n## Bar',
);
});
});
describe('.nestHeadingIds.iterateTree()', () => {
Expand Down

0 comments on commit 5d2ba6f

Please sign in to comment.