remark-break-line 0.0.9 → 0.0.10
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 +25 -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 = 80,
|
|
41
|
+
export default function remarkBreakLongLines({ maxLineLength = 80, removeLinebreaksAndMultipleSpaces = false, mergableElements = [] } = {}) {
|
|
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,37 @@ 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
|
-
|
|
103
|
+
n.children.splice(i + 1, 2);
|
|
104
|
+
|
|
101
105
|
}
|
|
102
106
|
}
|
|
103
107
|
|
|
104
|
-
|
|
105
|
-
mergeElements(element);
|
|
106
|
-
}
|
|
108
|
+
mergeElements(element);
|
|
107
109
|
}
|
|
108
110
|
}
|
|
109
111
|
};
|
|
@@ -298,12 +300,13 @@ export default function remarkBreakLongLines({ maxLineLength = 80, removeLinebre
|
|
|
298
300
|
|
|
299
301
|
return { type: 'nothing' };
|
|
300
302
|
}
|
|
303
|
+
if (removeLinebreaksAndMultipleSpaces)
|
|
304
|
+
removeLineBrakesWalker(node);
|
|
301
305
|
if (mergableElements.length > 0)
|
|
302
306
|
mergeElements(node);
|
|
303
307
|
for (let i = 0; i < node.children.length; i++) {
|
|
304
308
|
const element = node.children[i];
|
|
305
|
-
|
|
306
|
-
removeLineBrakesWalker(element);
|
|
309
|
+
|
|
307
310
|
|
|
308
311
|
const result = walk(element, [element.type])
|
|
309
312
|
if (result.type == 'split') {
|