markdansi 0.1.6 → 0.1.7

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/dist/wrap.js +28 -3
  2. package/package.json +1 -1
package/dist/wrap.js CHANGED
@@ -17,11 +17,36 @@ export function wrapText(text, width, wrap) {
17
17
  const lines = [];
18
18
  let current = "";
19
19
  let currentWidth = 0;
20
+ const trimEndSpaces = (s) => s.replace(/\s+$/, "");
21
+ const orphanPhraseTail = (s) => {
22
+ const trimmed = trimEndSpaces(s);
23
+ const phrase = trimmed.match(/\b(with|in|on|of|to|for)\s+(a|an|the)$/i);
24
+ if (phrase) {
25
+ const preposition = phrase[1];
26
+ const article = phrase[2];
27
+ if (preposition && article)
28
+ return `${preposition} ${article}`;
29
+ }
30
+ const single = trimmed.match(/\b(a|an|the|to|of|with|and|or|in|on|for)$/i);
31
+ return single?.[1] ?? null;
32
+ };
20
33
  for (const word of words) {
21
34
  const w = visibleWidth(word);
22
35
  if (current !== "" && currentWidth + w > width && !/^\s+$/.test(word)) {
23
- lines.push(current);
24
- current = word.replace(/^\s+/, "");
36
+ const nextWord = word.replace(/^\s+/, "");
37
+ const currentNoTrail = trimEndSpaces(current);
38
+ const tail = orphanPhraseTail(currentNoTrail);
39
+ if (tail && currentNoTrail.length > tail.length) {
40
+ const base = trimEndSpaces(currentNoTrail.slice(0, currentNoTrail.length - tail.length));
41
+ if (base !== "") {
42
+ lines.push(base);
43
+ current = `${tail} ${nextWord}`;
44
+ currentWidth = visibleWidth(current);
45
+ continue;
46
+ }
47
+ }
48
+ lines.push(currentNoTrail);
49
+ current = nextWord;
25
50
  currentWidth = visibleWidth(current);
26
51
  continue;
27
52
  }
@@ -29,7 +54,7 @@ export function wrapText(text, width, wrap) {
29
54
  currentWidth = visibleWidth(current);
30
55
  }
31
56
  if (current !== "")
32
- lines.push(current);
57
+ lines.push(trimEndSpaces(current));
33
58
  if (lines.length === 0)
34
59
  lines.push("");
35
60
  return lines;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "markdansi",
3
- "version": "0.1.6",
3
+ "version": "0.1.7",
4
4
  "description": "Tiny dependency-light markdown to ANSI converter.",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",