From 7ff28fba2310d844ec907a14f36ab388d2ab5b94 Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Wed, 9 Aug 2023 12:04:06 +0200 Subject: [PATCH] Fix trimming of whitespace around breaks Related-to: remarkjs/remark#1202. --- lib/state.js | 24 ++++++++++++++++++++++-- package.json | 1 + test/break.js | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 2 deletions(-) diff --git a/lib/state.js b/lib/state.js index 3ed78e6..512e960 100644 --- a/lib/state.js +++ b/lib/state.js @@ -291,14 +291,14 @@ export function createState(tree, options) { if (result) { if (index && nodes[index - 1].type === 'break') { if (!Array.isArray(result) && result.type === 'text') { - result.value = result.value.replace(/^\s+/, '') + result.value = trimMarkdownSpaceStart(result.value) } if (!Array.isArray(result) && result.type === 'element') { const head = result.children[0] if (head && head.type === 'text') { - head.value = head.value.replace(/^\s+/, '') + head.value = trimMarkdownSpaceStart(head.value) } } } @@ -447,3 +447,23 @@ export function wrap(nodes, loose) { return result } + +/** + * Trim spaces and tabs at the start of `value`. + * + * @param {string} value + * Value to trim. + * @returns {string} + * Result. + */ +function trimMarkdownSpaceStart(value) { + let index = 0 + let code = value.charCodeAt(index) + + while (code === 9 || code === 32) { + index++ + code = value.charCodeAt(index) + } + + return value.slice(index) +} diff --git a/package.json b/package.json index c44f6ca..5541ae3 100644 --- a/package.json +++ b/package.json @@ -109,6 +109,7 @@ "import/no-cycle": "error", "max-depth": "off", "unicorn/prefer-at": "off", + "unicorn/prefer-code-point": "off", "unicorn/prefer-string-replace-all": "off" } } diff --git a/test/break.js b/test/break.js index 6a2978b..22337d6 100644 --- a/test/break.js +++ b/test/break.js @@ -45,4 +45,36 @@ test('break', async function (t) { h('p', ['alpha', h('br'), '\n', h('em', 'bravo')]) ) }) + + await t.test('should trim text after a `br` (#3)', async function () { + assert.deepEqual( + toHast({ + type: 'paragraph', + children: [ + {type: 'text', value: 'a'}, + {type: 'break'}, + // U+3000 (ideographic space). + {type: 'text', value: ' b'}, + {type: 'break'}, + // U+2003 (em space). + {type: 'text', value: ' c'}, + {type: 'break'}, + // U+00A0 (no-break space). + {type: 'text', value: ' d'} + ] + }), + h('p', [ + 'a', + h('br'), + '\n', + ' b', + h('br'), + '\n', + ' c', + h('br'), + '\n', + ' d' + ]) + ) + }) })