remark-break-line 0.0.2 → 0.0.4
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 -3
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -33,7 +33,7 @@ const elementSize = {
|
|
|
33
33
|
* @returns
|
|
34
34
|
* Transform.
|
|
35
35
|
*/
|
|
36
|
-
export default function remarkBreakLongLines({ maxLineLength = 80 } = {}) {
|
|
36
|
+
export default function remarkBreakLongLines({ maxLineLength = 80, removeLinebreaks = false } = {}) {
|
|
37
37
|
/**
|
|
38
38
|
* Transform.
|
|
39
39
|
*
|
|
@@ -47,6 +47,24 @@ export default function remarkBreakLongLines({ maxLineLength = 80 } = {}) {
|
|
|
47
47
|
if (node.type === 'paragraph') {
|
|
48
48
|
const baseIndention = (node.position?.start.column ?? 1) - 1;
|
|
49
49
|
let currentLine = 0
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
*
|
|
53
|
+
* @param {typeof node.children[number]} n
|
|
54
|
+
*/
|
|
55
|
+
const prepare = (n) => {
|
|
56
|
+
if (n.type === 'text' && n.value.length > 0) {
|
|
57
|
+
n.value = n.value.replaceAll('\n', ' ').replace(/ {2..}/g, ' ');
|
|
58
|
+
} else if ('children' in n) {
|
|
59
|
+
for (let i = 0; i < n.children.length; i++) {
|
|
60
|
+
const element = n.children[i]
|
|
61
|
+
if (element.type in elementSize) {
|
|
62
|
+
prepare(element);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
|
|
50
68
|
/**
|
|
51
69
|
*
|
|
52
70
|
* @param {typeof node.children[number]} n
|
|
@@ -211,9 +229,10 @@ export default function remarkBreakLongLines({ maxLineLength = 80 } = {}) {
|
|
|
211
229
|
}
|
|
212
230
|
|
|
213
231
|
for (let i = 0; i < node.children.length; i++) {
|
|
214
|
-
const element = node.children[i]
|
|
232
|
+
const element = node.children[i];
|
|
233
|
+
if (removeLinebreaks)
|
|
234
|
+
prepare(element);
|
|
215
235
|
const result = walk(element, [element.type])
|
|
216
|
-
|
|
217
236
|
if (result.type == 'split') {
|
|
218
237
|
currentLine = 0
|
|
219
238
|
node.children.splice(i, 1, result.replaceBy[0], { type: 'text', value: '\n' }, result.replaceBy[1])
|
|
@@ -226,6 +245,10 @@ export default function remarkBreakLongLines({ maxLineLength = 80 } = {}) {
|
|
|
226
245
|
prev.value = prev.value.trimEnd();
|
|
227
246
|
}
|
|
228
247
|
}
|
|
248
|
+
} else if (currentLine > 0) {
|
|
249
|
+
// @ts-ignore
|
|
250
|
+
const additional = (elementSize[element.type] ?? 0) * 2;
|
|
251
|
+
currentLine += additional;
|
|
229
252
|
}
|
|
230
253
|
}
|
|
231
254
|
}
|