From af4e002585d0f95926a47edffabfd6f836a430fc Mon Sep 17 00:00:00 2001 From: Ultroncn <82553854+Ultroncn@users.noreply.github.com> Date: Tue, 17 Nov 2026 17:14:19 +0000 Subject: [PATCH] fix: don't parse list/quote markers inside headings as blocks (fixes #115) '### 1. Agreement' rendered as '

  1. Agreement

' because the heading branch re-parsed its contents with the full block parser, which treats a leading '1. ' as an ordered list. Headings can't contain blocks, so parse heading contents inline-only: a leading list/quote marker is kept as literal text and only the rest is parsed for inline formatting. Adds an internal 'inline' flag to parse(); the public two-argument call signature is unchanged. --- src/index.js | 34 ++++++++++++++++++++++------------ test/index.js | 11 +++++++++++ 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/src/index.js b/src/index.js index 3938c5c..a851ce5 100644 --- a/src/index.js +++ b/src/index.js @@ -22,8 +22,11 @@ function encodeAttr(str) { return (str+'').replace(/"/g, '"').replace(//g, '>'); } -/** Parse Markdown into an HTML String. */ -export default function parse(md, prevLinks) { +/** Parse Markdown into an HTML String. + * @param {boolean} [inline] only parse inline formatting, no block constructs + * (used for heading contents, which can't contain blocks) + */ +export default function parse(md, prevLinks, inline) { 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, context = [], out = '', @@ -60,21 +63,28 @@ export default function parse(md, prevLinks) { // escaped } // Code/Indent blocks: - else if (t = (token[3] || token[4])) { + else if (!inline && (t = (token[3] || token[4]))) { chunk = '
'+outdent(encodeAttr(t).replace(/^\n+|\n+$/g, ''))+'
'; } // > Quotes, -* lists: else if (t = token[6]) { - if (t.match(/\./)) { - token[5] = token[5].replace(/^\d+/gm, ''); + if (inline) { + // a list/quote marker inside a heading is literal text, not a block: + // keep the marker and parse only the rest inline (#115) + chunk = token[5].replace(/^(\s*(?:[>*+-]|\d+\.)\s+)([\s\S]*)$/, (s, marker, rest) => marker + parse(rest, links, inline)); } - inner = parse(outdent(token[5].replace(/^\s*[>*+.-]/gm, ''))); - if (t=='>') t = 'blockquote'; else { - t = t.match(/\./) ? 'ol' : 'ul'; - inner = inner.replace(/^(.*)(\n|$)/gm, '
  • $1
  • '); + if (t.match(/\./)) { + token[5] = token[5].replace(/^\d+/gm, ''); + } + inner = parse(outdent(token[5].replace(/^\s*[>*+.-]/gm, ''))); + if (t=='>') t = 'blockquote'; + else { + t = t.match(/\./) ? 'ol' : 'ul'; + inner = inner.replace(/^(.*)(\n|$)/gm, '
  • $1
  • '); + } + chunk = '<'+t+'>' + inner + ''; } - chunk = '<'+t+'>' + inner + ''; } // Images: else if (token[8]) { @@ -89,9 +99,9 @@ export default function parse(md, prevLinks) { chunk = ''; } // Headings: - else if (token[12] || token[14]) { + else if (!inline && (token[12] || token[14])) { t = 'h' + (token[14] ? token[14].length : (token[13]>'=' ? 1 : 2)); - chunk = '<'+t+'>' + parse(token[12] || token[15], links) + ''; + chunk = '<'+t+'>' + parse(token[12] || token[15], links, true) + ''; } // `code`: else if (token[16]) { diff --git a/test/index.js b/test/index.js index 5262b16..b089f57 100644 --- a/test/index.js +++ b/test/index.js @@ -37,6 +37,17 @@ describe('snarkdown()', () => { expect(snarkdown('### I like tiny libraries')).to.equal('

    I like tiny libraries

    '); }); + it('does not parse numbers in titles as lists', () => { + expect(snarkdown('### 1. Agreement')).to.equal('

    1. Agreement

    '); + expect(snarkdown('### 2. Rights')).to.equal('

    2. Rights

    '); + expect(snarkdown('### 1. **Agreement**')).to.equal('

    1. Agreement

    '); + }); + + it('does not parse list or quote markers in titles as blocks', () => { + expect(snarkdown('### - item')).to.equal('

    - item

    '); + expect(snarkdown('### > quote')).to.equal('

    > quote

    '); + }); + it('parses titles with reference links', () => { expect( snarkdown('# I like [tiny libraries]\n\n[tiny libraries]: https://github.com/developit/snarkdown')