remark-break-line 0.0.6 → 0.0.8
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 +42 -9
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -54,7 +54,7 @@ export default function remarkBreakLongLines({ maxLineLength = 80, removeLinebre
|
|
|
54
54
|
*/
|
|
55
55
|
const prepare = (n) => {
|
|
56
56
|
if (n.type === 'text' && n.value.length > 0) {
|
|
57
|
-
n.value = n.value.replaceAll(/[\r\n]
|
|
57
|
+
n.value = n.value.replaceAll(/[\r\n]+/g, ' ').replaceAll(/ {2..}/g, ' ');
|
|
58
58
|
} else if ('children' in n) {
|
|
59
59
|
for (let i = 0; i < n.children.length; i++) {
|
|
60
60
|
const element = n.children[i]
|
|
@@ -85,6 +85,8 @@ export default function remarkBreakLongLines({ maxLineLength = 80, removeLinebre
|
|
|
85
85
|
const current = path[path.length - 1];
|
|
86
86
|
assert(current === 'text');
|
|
87
87
|
|
|
88
|
+
n.value = n.value.trimStart();
|
|
89
|
+
|
|
88
90
|
return {
|
|
89
91
|
type: 'break-before',
|
|
90
92
|
};
|
|
@@ -92,7 +94,7 @@ export default function remarkBreakLongLines({ maxLineLength = 80, removeLinebre
|
|
|
92
94
|
|
|
93
95
|
|
|
94
96
|
} else {
|
|
95
|
-
n.value = '\n' + n.value
|
|
97
|
+
n.value = '\n' + n.value.trimStart();
|
|
96
98
|
currentLine = 0
|
|
97
99
|
}
|
|
98
100
|
}
|
|
@@ -200,7 +202,7 @@ export default function remarkBreakLongLines({ maxLineLength = 80, removeLinebre
|
|
|
200
202
|
}
|
|
201
203
|
}
|
|
202
204
|
}
|
|
203
|
-
} else if ('children' in n) {
|
|
205
|
+
} else if ('children' in n && n.type in elementSize) {
|
|
204
206
|
for (let i = 0; i < n.children.length; i++) {
|
|
205
207
|
const element = n.children[i]
|
|
206
208
|
if (element.type in elementSize) {
|
|
@@ -221,8 +223,33 @@ export default function remarkBreakLongLines({ maxLineLength = 80, removeLinebre
|
|
|
221
223
|
type: 'break-before',
|
|
222
224
|
};
|
|
223
225
|
}
|
|
226
|
+
} else {
|
|
227
|
+
if (n.position && n.position.end.line == n.position.start.line) {
|
|
228
|
+
const length = (n.position?.end.column ?? 0) - (n.position?.start.column ?? 0);
|
|
229
|
+
currentLine += length;
|
|
230
|
+
} else if (n.position) {
|
|
231
|
+
// this spans multiple lnes so we justtake the column of the end.
|
|
232
|
+
currentLine = n.position.end.column - 1;
|
|
233
|
+
}
|
|
234
|
+
if (currentLine >= maxLineLength - baseIndention) {
|
|
235
|
+
n.children.splice(i, 0, { type: 'text', value: '/n' });
|
|
236
|
+
i++ // do not process this node again
|
|
237
|
+
const length = (n.position?.end.column ?? 0) - (n.position?.start.column ?? 0);
|
|
238
|
+
currentLine = length;
|
|
239
|
+
}
|
|
224
240
|
}
|
|
225
241
|
}
|
|
242
|
+
} else {
|
|
243
|
+
if (n.position && n.position.end.line == n.position.start.line) {
|
|
244
|
+
const length = (n.position?.end.column ?? 0) - (n.position?.start.column ?? 0);
|
|
245
|
+
currentLine += length;
|
|
246
|
+
} else if (n.position) {
|
|
247
|
+
// this spans multiple lnes so we justtake the column of the end.
|
|
248
|
+
currentLine = n.position.end.column - 1;
|
|
249
|
+
}
|
|
250
|
+
if (currentLine >= maxLineLength - baseIndention) {
|
|
251
|
+
return { type: 'break-before' };
|
|
252
|
+
}
|
|
226
253
|
}
|
|
227
254
|
|
|
228
255
|
return { type: 'nothing' };
|
|
@@ -237,12 +264,18 @@ export default function remarkBreakLongLines({ maxLineLength = 80, removeLinebre
|
|
|
237
264
|
currentLine = 0
|
|
238
265
|
node.children.splice(i, 1, result.replaceBy[0], { type: 'text', value: '\n' }, result.replaceBy[1])
|
|
239
266
|
} else if (result.type == 'break-before') {
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
267
|
+
const prev = node.children[i - 1];
|
|
268
|
+
if (i > 0 && prev.type == 'text' && prev.value.endsWith('\n')) {
|
|
269
|
+
// skip we already inserted a break before
|
|
270
|
+
} else {
|
|
271
|
+
|
|
272
|
+
currentLine = 0
|
|
273
|
+
node.children.splice(i, 0, { type: 'text', value: '\n' })
|
|
274
|
+
if (i > 0) {
|
|
275
|
+
const prev = node.children[i - 1];
|
|
276
|
+
if (prev.type === 'text') {
|
|
277
|
+
prev.value = prev.value.trimEnd();
|
|
278
|
+
}
|
|
246
279
|
}
|
|
247
280
|
}
|
|
248
281
|
} else if (currentLine > 0) {
|