remark-break-line 0.0.7 → 0.0.9
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.d.ts +10 -3
- package/lib/index.js +95 -16
- package/package.json +1 -1
package/lib/index.d.ts
CHANGED
|
@@ -1,10 +1,17 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Turn gemoji shortcodes (`:+1:`) into emoji (`👍`).
|
|
3
|
-
*
|
|
3
|
+
* @param {Object} [param0={}]
|
|
4
|
+
* @param {number} [param0.maxLineLength=80]
|
|
5
|
+
* @param {boolean} [param0.removeLinebreaks=false]
|
|
6
|
+
* @param {string[]} [param0.mergableElements=[]]
|
|
7
|
+
* @param {boolean}[param0.removeMultpleSpaces=false]
|
|
4
8
|
* @returns
|
|
5
9
|
* Transform.
|
|
6
10
|
*/
|
|
7
|
-
export default function remarkBreakLongLines({ maxLineLength }?: {
|
|
11
|
+
export default function remarkBreakLongLines({ maxLineLength, removeLinebreaks: removeLinebreaksAndMultipleSpaces, mergableElements }?: {
|
|
8
12
|
maxLineLength?: number | undefined;
|
|
9
|
-
|
|
13
|
+
removeLinebreaks?: boolean | undefined;
|
|
14
|
+
mergableElements?: string[] | undefined;
|
|
15
|
+
removeMultpleSpaces?: boolean | undefined;
|
|
16
|
+
} | undefined): (tree: Root) => undefined;
|
|
10
17
|
export type Root = import("mdast").Root;
|
package/lib/index.js
CHANGED
|
@@ -29,11 +29,15 @@ const elementSize = {
|
|
|
29
29
|
|
|
30
30
|
/**
|
|
31
31
|
* Turn gemoji shortcodes (`:+1:`) into emoji (`👍`).
|
|
32
|
-
*
|
|
32
|
+
* @param {Object} [param0={}]
|
|
33
|
+
* @param {number} [param0.maxLineLength=80]
|
|
34
|
+
* @param {boolean} [param0.removeLinebreaks=false]
|
|
35
|
+
* @param {string[]} [param0.mergableElements=[]]
|
|
36
|
+
* @param {boolean}[param0.removeMultpleSpaces=false]
|
|
33
37
|
* @returns
|
|
34
38
|
* Transform.
|
|
35
39
|
*/
|
|
36
|
-
export default function remarkBreakLongLines({ maxLineLength = 80, removeLinebreaks = false } = {}) {
|
|
40
|
+
export default function remarkBreakLongLines({ maxLineLength = 80, removeLinebreaks: removeLinebreaksAndMultipleSpaces = false, mergableElements = [] } = {}) {
|
|
37
41
|
/**
|
|
38
42
|
* Transform.
|
|
39
43
|
*
|
|
@@ -52,19 +56,59 @@ export default function remarkBreakLongLines({ maxLineLength = 80, removeLinebre
|
|
|
52
56
|
*
|
|
53
57
|
* @param {typeof node.children[number]} n
|
|
54
58
|
*/
|
|
55
|
-
const
|
|
59
|
+
const removeLineBrakesWalker = (n) => {
|
|
56
60
|
if (n.type === 'text' && n.value.length > 0) {
|
|
57
|
-
n.value = n.value.replaceAll(/[\r\n]+/g, ' ').replaceAll(/ {2
|
|
61
|
+
n.value = n.value.replaceAll(/[\r\n]+/g, ' ').replaceAll(/ {2,}/g, ' ');
|
|
58
62
|
} else if ('children' in n) {
|
|
59
63
|
for (let i = 0; i < n.children.length; i++) {
|
|
60
64
|
const element = n.children[i]
|
|
61
65
|
if (element.type in elementSize) {
|
|
62
|
-
|
|
66
|
+
removeLineBrakesWalker(element);
|
|
63
67
|
}
|
|
64
68
|
}
|
|
65
69
|
}
|
|
66
70
|
};
|
|
67
71
|
|
|
72
|
+
/**
|
|
73
|
+
*
|
|
74
|
+
* @param {typeof node.children[number]|typeof node} n
|
|
75
|
+
*/
|
|
76
|
+
const mergeElements = (n) => {
|
|
77
|
+
if (n.type === 'text' && n.value.length > 0) {
|
|
78
|
+
// n.value = n.value.replaceAll(/[\r\n]+/g, ' ').replaceAll(/ {2,}/g, ' ');
|
|
79
|
+
} else if ('children' in n) {
|
|
80
|
+
for (let i = 0; i < n.children.length; i++) {
|
|
81
|
+
const element = n.children[i]
|
|
82
|
+
if (mergableElements.includes(element.type) && i - 1 >= 0 && n.children[i - 1].type == element.type) {
|
|
83
|
+
const first = n.children[i - 1];
|
|
84
|
+
const last = element;
|
|
85
|
+
if ('children' in first && 'children' in last) {
|
|
86
|
+
first.children.push(...last.children);
|
|
87
|
+
n.children.splice(i, 1);
|
|
88
|
+
i--;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
if (mergableElements.includes(element.type) && i - 2 >= 0 && n.children[i - 2].type == element.type) {
|
|
92
|
+
const first = n.children[i - 2];
|
|
93
|
+
const middel = n.children[i - 1];
|
|
94
|
+
const last = element;
|
|
95
|
+
if ('children' in first && 'children' in last && middel.type == 'text' && /^[\s\n\r]*$/.test(middel.value)) {
|
|
96
|
+
middel.value = middel.value.replaceAll(/\r\n|\r|\n/g, ' ')
|
|
97
|
+
first.children.push(middel);
|
|
98
|
+
first.children.push(...last.children);
|
|
99
|
+
n.children.splice(i - 1, 2);
|
|
100
|
+
i -= 2;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (element.type in elementSize) {
|
|
105
|
+
mergeElements(element);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
|
|
68
112
|
/**
|
|
69
113
|
*
|
|
70
114
|
* @param {typeof node.children[number]} n
|
|
@@ -85,6 +129,8 @@ export default function remarkBreakLongLines({ maxLineLength = 80, removeLinebre
|
|
|
85
129
|
const current = path[path.length - 1];
|
|
86
130
|
assert(current === 'text');
|
|
87
131
|
|
|
132
|
+
n.value = n.value.trimStart();
|
|
133
|
+
|
|
88
134
|
return {
|
|
89
135
|
type: 'break-before',
|
|
90
136
|
};
|
|
@@ -92,7 +138,7 @@ export default function remarkBreakLongLines({ maxLineLength = 80, removeLinebre
|
|
|
92
138
|
|
|
93
139
|
|
|
94
140
|
} else {
|
|
95
|
-
n.value = '\n' + n.value
|
|
141
|
+
n.value = '\n' + n.value.trimStart();
|
|
96
142
|
currentLine = 0
|
|
97
143
|
}
|
|
98
144
|
}
|
|
@@ -200,7 +246,7 @@ export default function remarkBreakLongLines({ maxLineLength = 80, removeLinebre
|
|
|
200
246
|
}
|
|
201
247
|
}
|
|
202
248
|
}
|
|
203
|
-
} else if ('children' in n) {
|
|
249
|
+
} else if ('children' in n && n.type in elementSize) {
|
|
204
250
|
for (let i = 0; i < n.children.length; i++) {
|
|
205
251
|
const element = n.children[i]
|
|
206
252
|
if (element.type in elementSize) {
|
|
@@ -221,28 +267,61 @@ export default function remarkBreakLongLines({ maxLineLength = 80, removeLinebre
|
|
|
221
267
|
type: 'break-before',
|
|
222
268
|
};
|
|
223
269
|
}
|
|
270
|
+
} else {
|
|
271
|
+
if (n.position && n.position.end.line == n.position.start.line) {
|
|
272
|
+
const length = (n.position?.end.column ?? 0) - (n.position?.start.column ?? 0);
|
|
273
|
+
currentLine += length;
|
|
274
|
+
} else if (n.position) {
|
|
275
|
+
// this spans multiple lnes so we justtake the column of the end.
|
|
276
|
+
currentLine = n.position.end.column - 1;
|
|
277
|
+
}
|
|
278
|
+
if (currentLine >= maxLineLength - baseIndention) {
|
|
279
|
+
n.children.splice(i, 0, { type: 'text', value: '/n' });
|
|
280
|
+
i++ // do not process this node again
|
|
281
|
+
const length = (n.position?.end.column ?? 0) - (n.position?.start.column ?? 0);
|
|
282
|
+
currentLine = length;
|
|
283
|
+
}
|
|
224
284
|
}
|
|
225
285
|
}
|
|
286
|
+
} else {
|
|
287
|
+
if (n.position && n.position.end.line == n.position.start.line) {
|
|
288
|
+
const length = (n.position?.end.column ?? 0) - (n.position?.start.column ?? 0);
|
|
289
|
+
currentLine += length;
|
|
290
|
+
} else if (n.position) {
|
|
291
|
+
// this spans multiple lnes so we justtake the column of the end.
|
|
292
|
+
currentLine = n.position.end.column - 1;
|
|
293
|
+
}
|
|
294
|
+
if (currentLine >= maxLineLength - baseIndention) {
|
|
295
|
+
return { type: 'break-before' };
|
|
296
|
+
}
|
|
226
297
|
}
|
|
227
298
|
|
|
228
299
|
return { type: 'nothing' };
|
|
229
300
|
}
|
|
230
|
-
|
|
301
|
+
if (mergableElements.length > 0)
|
|
302
|
+
mergeElements(node);
|
|
231
303
|
for (let i = 0; i < node.children.length; i++) {
|
|
232
304
|
const element = node.children[i];
|
|
233
|
-
if (
|
|
234
|
-
|
|
305
|
+
if (removeLinebreaksAndMultipleSpaces)
|
|
306
|
+
removeLineBrakesWalker(element);
|
|
307
|
+
|
|
235
308
|
const result = walk(element, [element.type])
|
|
236
309
|
if (result.type == 'split') {
|
|
237
310
|
currentLine = 0
|
|
238
311
|
node.children.splice(i, 1, result.replaceBy[0], { type: 'text', value: '\n' }, result.replaceBy[1])
|
|
239
312
|
} else if (result.type == 'break-before') {
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
313
|
+
const prev = node.children[i - 1];
|
|
314
|
+
if (i > 0 && prev.type == 'text' && prev.value.endsWith('\n')) {
|
|
315
|
+
// skip we already inserted a break before
|
|
316
|
+
} else {
|
|
317
|
+
|
|
318
|
+
currentLine = 0
|
|
319
|
+
node.children.splice(i, 0, { type: 'text', value: '\n' })
|
|
320
|
+
if (i > 0) {
|
|
321
|
+
const prev = node.children[i - 1];
|
|
322
|
+
if (prev.type === 'text') {
|
|
323
|
+
prev.value = prev.value.trimEnd();
|
|
324
|
+
}
|
|
246
325
|
}
|
|
247
326
|
}
|
|
248
327
|
} else if (currentLine > 0) {
|