Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Npmjs #64

Merged
merged 5 commits into from
Mar 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ coverage
dist
node_modules
package-lock.json
pnpm-lock.yaml
*.log
.*
!.babelrc.json
Expand Down
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