diff --git a/lib/common/utils.mjs b/lib/common/utils.mjs index b78c9b0..739ce2a 100644 --- a/lib/common/utils.mjs +++ b/lib/common/utils.mjs @@ -278,6 +278,27 @@ function normalizeReference (str) { return str.toLowerCase().toUpperCase() } +// Removes space-like characters that are allowed to be removed in CommonMark spec. +function trimMinimalSpaces (str) { + let start = 0 + for (; start < str.length; start++) { + if (!isSpaceLike(str.charCodeAt(start))) { + break + } + } + let end = str.length - 1 + for (; end >= start; end--) { + if (!isSpaceLike(str.charCodeAt(end))) { + break + } + } + return str.slice(start, end + 1) + + function isSpaceLike (c) { + return c === 0x20 || c === 0x09 || c === 0x0a || c === 0x0d + } +} + // Re-export libraries commonly used in both markdown-it and its plugins, // so plugins won't have to depend on them explicitly, which reduces their // bundled size (e.g. a browser build). @@ -300,5 +321,6 @@ export { isMdAsciiPunct, isPunctChar, escapeRE, - normalizeReference + normalizeReference, + trimMinimalSpaces } diff --git a/lib/rules_block/heading.mjs b/lib/rules_block/heading.mjs index d2f7b79..918ad56 100644 --- a/lib/rules_block/heading.mjs +++ b/lib/rules_block/heading.mjs @@ -1,6 +1,6 @@ // heading (#, ##, ...) -import { isSpace } from '../common/utils.mjs' +import { isSpace, trimMinimalSpaces } from '../common/utils.mjs' export default function heading (state, startLine, endLine, silent) { let pos = state.bMarks[startLine] + state.tShift[startLine] @@ -40,7 +40,7 @@ export default function heading (state, startLine, endLine, silent) { token_o.map = [startLine, state.line] const token_i = state.push('inline', '', 0) - token_i.content = state.src.slice(pos, max).trim() + token_i.content = trimMinimalSpaces(state.src.slice(pos, max)) token_i.map = [startLine, state.line] token_i.children = [] diff --git a/lib/rules_block/lheading.mjs b/lib/rules_block/lheading.mjs index ee3b9a3..5956341 100644 --- a/lib/rules_block/lheading.mjs +++ b/lib/rules_block/lheading.mjs @@ -1,5 +1,7 @@ // lheading (---, ===) +import { trimMinimalSpaces } from '../common/utils.mjs' + export default function lheading (state, startLine, endLine/*, silent */) { const terminatorRules = state.md.block.ruler.getRules('paragraph') @@ -60,7 +62,7 @@ export default function lheading (state, startLine, endLine/*, silent */) { return false } - const content = state.getLines(startLine, nextLine, state.blkIndent, false).trim() + const content = trimMinimalSpaces(state.getLines(startLine, nextLine, state.blkIndent, false)) state.line = nextLine + 1 diff --git a/lib/rules_block/paragraph.mjs b/lib/rules_block/paragraph.mjs index 6ecdcef..a0d7a3f 100644 --- a/lib/rules_block/paragraph.mjs +++ b/lib/rules_block/paragraph.mjs @@ -1,5 +1,7 @@ // Paragraph +import { trimMinimalSpaces } from '../common/utils.mjs' + export default function paragraph (state, startLine, endLine) { const terminatorRules = state.md.block.ruler.getRules('paragraph') const oldParentType = state.parentType @@ -26,7 +28,7 @@ export default function paragraph (state, startLine, endLine) { if (terminate) { break } } - const content = state.getLines(startLine, nextLine, state.blkIndent, false).trim() + const content = trimMinimalSpaces(state.getLines(startLine, nextLine, state.blkIndent, false)) state.line = nextLine diff --git a/test/fixtures/markdown-it/commonmark_extras.txt b/test/fixtures/markdown-it/commonmark_extras.txt index 558c011..84c8a70 100644 --- a/test/fixtures/markdown-it/commonmark_extras.txt +++ b/test/fixtures/markdown-it/commonmark_extras.txt @@ -740,3 +740,18 @@ Html in image description .
U+3000
+.