Skip to content

Commit c608c41

Browse files
authored
Use mdast-util-newline-to-break
Closes GH-9. Closes GH-10. Reviewed-by: Christian Murphy <[email protected]> Reviewed-by: Titus Wormer <[email protected]>
1 parent a343c6c commit c608c41

2 files changed

Lines changed: 3 additions & 37 deletions

File tree

index.js

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
/**
22
* @typedef {import('mdast').Root} Root
3-
* @typedef {import('mdast').PhrasingContent} PhrasingContent
43
*/
54

6-
import {visit} from 'unist-util-visit'
7-
8-
const find = /[\t ]*(?:\r?\n|\r)/g
5+
import {newlineToBreak} from 'mdast-util-newline-to-break'
96

107
/**
118
* Plugin to support hard breaks without needing spaces or escapes (turns enters
@@ -14,36 +11,5 @@ const find = /[\t ]*(?:\r?\n|\r)/g
1411
* @type {import('unified').Plugin<void[], Root>}
1512
*/
1613
export default function remarkBreaks() {
17-
return (tree) => {
18-
visit(tree, 'text', (node, index, parent) => {
19-
/** @type {PhrasingContent[]} */
20-
const result = []
21-
let start = 0
22-
23-
find.lastIndex = 0
24-
25-
let match = find.exec(node.value)
26-
27-
while (match) {
28-
const position = match.index
29-
30-
if (start !== position) {
31-
result.push({type: 'text', value: node.value.slice(start, position)})
32-
}
33-
34-
result.push({type: 'break'})
35-
start = position + match[0].length
36-
match = find.exec(node.value)
37-
}
38-
39-
if (result.length > 0 && parent && typeof index === 'number') {
40-
if (start < node.value.length) {
41-
result.push({type: 'text', value: node.value.slice(start)})
42-
}
43-
44-
parent.children.splice(index, 1, ...result)
45-
return index + result.length
46-
}
47-
})
48-
}
14+
return newlineToBreak
4915
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"dependencies": {
3636
"@types/mdast": "^3.0.0",
3737
"unified": "^10.0.0",
38-
"unist-util-visit": "^4.0.0"
38+
"mdast-util-newline-to-break": "^1.0.0"
3939
},
4040
"devDependencies": {
4141
"@types/tape": "^4.0.0",

0 commit comments

Comments
 (0)