remark-break-line 0.0.9 → 0.0.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.js +26 -22
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
5
|
import { assert } from 'console'
|
|
6
|
+
import { text } from 'stream/consumers'
|
|
6
7
|
import { visit } from 'unist-util-visit'
|
|
7
8
|
|
|
8
9
|
/**
|
|
@@ -31,13 +32,13 @@ const elementSize = {
|
|
|
31
32
|
* Turn gemoji shortcodes (`:+1:`) into emoji (`👍`).
|
|
32
33
|
* @param {Object} [param0={}]
|
|
33
34
|
* @param {number} [param0.maxLineLength=80]
|
|
34
|
-
* @param {boolean} [param0.
|
|
35
|
+
* @param {boolean} [param0.removeLinebreaksAndMultipleSpaces=false]
|
|
35
36
|
* @param {string[]} [param0.mergableElements=[]]
|
|
36
37
|
* @param {boolean}[param0.removeMultpleSpaces=false]
|
|
37
38
|
* @returns
|
|
38
39
|
* Transform.
|
|
39
40
|
*/
|
|
40
|
-
export default function remarkBreakLongLines({ maxLineLength =
|
|
41
|
+
export default function remarkBreakLongLines({ maxLineLength = Number.POSITIVE_INFINITY, removeLinebreaksAndMultipleSpaces = true, mergableElements = ['emphasis'] } = {}) {
|
|
41
42
|
/**
|
|
42
43
|
* Transform.
|
|
43
44
|
*
|
|
@@ -54,7 +55,7 @@ export default function remarkBreakLongLines({ maxLineLength = 80, removeLinebre
|
|
|
54
55
|
|
|
55
56
|
/**
|
|
56
57
|
*
|
|
57
|
-
* @param {typeof node.children[number]} n
|
|
58
|
+
* @param {typeof node.children[number]|typeof node} n
|
|
58
59
|
*/
|
|
59
60
|
const removeLineBrakesWalker = (n) => {
|
|
60
61
|
if (n.type === 'text' && n.value.length > 0) {
|
|
@@ -74,36 +75,38 @@ export default function remarkBreakLongLines({ maxLineLength = 80, removeLinebre
|
|
|
74
75
|
* @param {typeof node.children[number]|typeof node} n
|
|
75
76
|
*/
|
|
76
77
|
const mergeElements = (n) => {
|
|
77
|
-
if (
|
|
78
|
-
// n.value = n.value.replaceAll(/[\r\n]+/g, ' ').replaceAll(/ {2,}/g, ' ');
|
|
79
|
-
} else if ('children' in n) {
|
|
78
|
+
if ('children' in n) {
|
|
80
79
|
for (let i = 0; i < n.children.length; i++) {
|
|
81
80
|
const element = n.children[i]
|
|
82
|
-
if (mergableElements.includes(element.type) && i
|
|
83
|
-
const first =
|
|
84
|
-
const last =
|
|
81
|
+
if ((element.type == 'text' || mergableElements.includes(element.type)) && i + 1 < n.children.length && n.children[i + 1].type == element.type) {
|
|
82
|
+
const first = element;
|
|
83
|
+
const last = n.children[i + 1];
|
|
85
84
|
if ('children' in first && 'children' in last) {
|
|
86
85
|
first.children.push(...last.children);
|
|
87
|
-
n.children.splice(i, 1);
|
|
88
|
-
|
|
86
|
+
n.children.splice(i + 1, 1);
|
|
87
|
+
} else if (first.type == 'text' && last.type == 'text') {
|
|
88
|
+
if (!last.value.startsWith(' ') && !first.value.endsWith(' '))
|
|
89
|
+
first.value += ' ';
|
|
90
|
+
first.value += last.value;
|
|
91
|
+
n.children.splice(i + 1, 1);
|
|
92
|
+
i--;// we need to repeat this, so we will consoume multiple following text elements
|
|
89
93
|
}
|
|
90
94
|
}
|
|
91
|
-
if (mergableElements.includes(element.type) && i
|
|
92
|
-
const first =
|
|
93
|
-
const middel = n.children[i
|
|
94
|
-
const last =
|
|
95
|
+
if (mergableElements.includes(element.type) && i + 2 < n.children.length && n.children[i + 2].type == element.type) {
|
|
96
|
+
const first = element;
|
|
97
|
+
const middel = n.children[i + 1];
|
|
98
|
+
const last = n.children[i + 2];
|
|
95
99
|
if ('children' in first && 'children' in last && middel.type == 'text' && /^[\s\n\r]*$/.test(middel.value)) {
|
|
96
100
|
middel.value = middel.value.replaceAll(/\r\n|\r|\n/g, ' ')
|
|
97
101
|
first.children.push(middel);
|
|
98
102
|
first.children.push(...last.children);
|
|
99
|
-
n.children.splice(i
|
|
100
|
-
i
|
|
103
|
+
n.children.splice(i + 1, 2);
|
|
104
|
+
i--;
|
|
105
|
+
|
|
101
106
|
}
|
|
102
107
|
}
|
|
103
108
|
|
|
104
|
-
|
|
105
|
-
mergeElements(element);
|
|
106
|
-
}
|
|
109
|
+
mergeElements(element);
|
|
107
110
|
}
|
|
108
111
|
}
|
|
109
112
|
};
|
|
@@ -298,12 +301,13 @@ export default function remarkBreakLongLines({ maxLineLength = 80, removeLinebre
|
|
|
298
301
|
|
|
299
302
|
return { type: 'nothing' };
|
|
300
303
|
}
|
|
304
|
+
if (removeLinebreaksAndMultipleSpaces)
|
|
305
|
+
removeLineBrakesWalker(node);
|
|
301
306
|
if (mergableElements.length > 0)
|
|
302
307
|
mergeElements(node);
|
|
303
308
|
for (let i = 0; i < node.children.length; i++) {
|
|
304
309
|
const element = node.children[i];
|
|
305
|
-
|
|
306
|
-
removeLineBrakesWalker(element);
|
|
310
|
+
|
|
307
311
|
|
|
308
312
|
const result = walk(element, [element.type])
|
|
309
313
|
if (result.type == 'split') {
|