remark-break-line 0.0.7 → 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.
Files changed (2) hide show
  1. package/lib/index.js +41 -8
  2. package/package.json +1 -1
package/lib/index.js CHANGED
@@ -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
- currentLine = 0
241
- node.children.splice(i, 0, { type: 'text', value: '\n' })
242
- if (i > 0) {
243
- const prev = node.children[i - 1];
244
- if (prev.type === 'text') {
245
- prev.value = prev.value.trimEnd();
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) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "remark-break-line",
3
- "version": "0.0.7",
3
+ "version": "0.0.8",
4
4
  "type": "module",
5
5
  "description": "",
6
6
  "keywords": [