Skip to content

Commit

Permalink
Merge 7694d20 into 0ef45f4
Browse files Browse the repository at this point in the history
  • Loading branch information
brettz9 committed Jan 4, 2022
2 parents 0ef45f4 + 7694d20 commit addb6c4
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 10 deletions.
27 changes: 22 additions & 5 deletions src/gitdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ const _ = require('lodash');
const MarkdownContents = require('markdown-contents');
const marked = require('marked');
const StackTrace = require('stack-trace');
const contents = require('./helpers/contents.js');
const gitinfo = require('./helpers/gitinfo.js');
const Parser = require('./parser.js');
const contents = require('./helpers/contents');
const gitinfo = require('./helpers/gitinfo');
const Parser = require('./parser');

/**
* @param {string} input Gitdown flavored markdown.
Expand Down 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
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const Gitdown = require('./gitdown.js');
const Gitdown = require('./gitdown');

Gitdown.Parser = require('./parser.js');
Gitdown.Parser = require('./parser');

module.exports = Gitdown;
2 changes: 1 addition & 1 deletion src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const Path = require('path');
const Promise = require('bluebird');
const glob = require('glob');
const _ = require('lodash');
const Locator = require('./locator.js');
const Locator = require('./locator');

/**
* Parser is responsible for matching all of the instances of the Gitdown JSON and invoking
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 addb6c4

Please sign in to comment.