Skip to content
Open
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
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function encodeAttr(str) {

/** Parse Markdown into an HTML String. */
export default function parse(md, prevLinks) {
let tokenizer = /((?:^|\n+)(?:\n---+|\* \*(?: \*)+)\n)|(?:^``` *(\w*)\n([\s\S]*?)\n```$)|((?:(?:^|\n+)(?:\t| {2,}).+)+\n*)|((?:(?:^|\n)([>*+-]|\d+\.)\s+.*)+)|(?:!\[([^\]]*?)\]\(([^)]+?)\))|(\[)|(\](?:\(([^)]+?)\))?)|(?:(?:^|\n+)([^\s].*)\n(-{3,}|={3,})(?:\n+|$))|(?:(?:^|\n+)(#{1,6})\s*(.+)(?:\n+|$))|(?:`([^`].*?)`)|( \n\n*|\n{2,}|__|\*\*|[_*]|~~)/gm,
let tokenizer = /((?:^|\n+)(?:\n---+|\* \*(?: \*)+)\n)|(?:^```[ \t]*(\w*)[ \t]*\n([\s\S]*?)\n```$)|((?:(?:^|\n+)(?:\t| {2,}).+)+\n*)|((?:(?:^|\n)([>*+-]|\d+\.)\s+.*)+)|(?:!\[([^\]]*?)\]\(([^)]+?)\))|(\[)|(\](?:\(([^)]+?)\))?)|(?:(?:^|\n+)([^\s].*)\n(-{3,}|={3,})(?:\n+|$))|(?:(?:^|\n+)(#{1,6})\s*(.+)(?:\n+|$))|(?:`([^`].*?)`)|( \n\n*|\n{2,}|__|\*\*|[_*]|~~)/gm,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Avoid overlapping whitespace matches when the language is absent

For an unclosed fence without a language, the language capture can be empty, allowing both surrounding whitespace repetitions to consume the same characters and causing quadratic backtracking. On Node 24, snarkdown('```' + ' '.repeat(40000) + '\nbody') took about 1.75 seconds at this commit versus 2 ms in its parent. Applications rendering user-supplied Markdown can therefore have their event loop or UI stalled by a small document. Make the nonempty language and its trailing whitespace optional together so that a language-less opener consumes its whitespace only once.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked the whitespace overlap in the diff. I haven’t independently measured the slowdown. Would you be open to a small revision that removes the overlap while preserving support for spaces and tabs around the language name? I'd appreciate your guidance before updating the PR.

context = [],
out = '',
links = prevLinks || {},
Expand Down
7 changes: 7 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@ describe('snarkdown()', () => {
expect(snarkdown('```js\nfunction codeBlocks() {\n\treturn "Can be inserted";\n}\n```')).to.equal('<pre class="code js"><code class="language-js">function codeBlocks() {\n\treturn &quot;Can be inserted&quot;;\n}</code></pre>');
});

it('parses fenced code blocks with spaces around the language (fixes #62)', () => {
expect(snarkdown('```js \ncode\n```')).to.equal('<pre class="code js"><code class="language-js">code</code></pre>');
expect(snarkdown('``` js \ncode\n```')).to.equal('<pre class="code js"><code class="language-js">code</code></pre>');
expect(snarkdown('```js\t\ncode\n```')).to.equal('<pre class="code js"><code class="language-js">code</code></pre>');
expect(snarkdown('``` \ncode\n```')).to.equal('<pre class="code "><code>code</code></pre>');
});

it('parses tabs as a code poetry block', () => {
expect(snarkdown('\tvar a = 1')).to.equal('<pre class="code poetry"><code>var a = 1</code></pre>');
});
Expand Down