Skip to content

Commit

Permalink
Fix trimming of whitespace around breaks
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Aug 9, 2023
1 parent b4ec6fa commit 7ff28fb
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
24 changes: 22 additions & 2 deletions lib/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand Down Expand Up @@ -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)
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
Expand Down
32 changes: 32 additions & 0 deletions test/break.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
])
)
})
})

0 comments on commit 7ff28fb

Please sign in to comment.