mdto-messages 0.3.0 → 0.3.1

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.
@@ -9,11 +9,12 @@ export interface Fragment {
9
9
  text: string;
10
10
  nodes: DocumentNode[];
11
11
  htmlSize: number;
12
- endsInWebLink?: boolean;
12
+ startsWithWebURL?: boolean;
13
+ webTailLength?: number;
13
14
  }
14
15
  export declare function html(nodes: DocumentNode[]): string;
15
16
  export declare function literal(text: string): Fragment;
16
17
  export declare function wrap(kind: string, part: Fragment, attributes?: Pick<DocumentNode, "url" | "start">): Fragment;
17
- export declare function linkBoundary(endsInWebLink: boolean | undefined, next: string): string;
18
+ export declare function markText(part: Fragment, delimiter: string): void;
18
19
  export declare function join(parts: Fragment[], sep?: string): Fragment;
19
20
  export declare function contains(nodes: DocumentNode[], kind: string): boolean;
package/dist/fragment.js CHANGED
@@ -31,29 +31,73 @@ export function wrap(kind, part, attributes = {}) {
31
31
  const node = { kind, ...attributes, children: part.nodes };
32
32
  return {
33
33
  text: part.text,
34
- endsInWebLink: part.endsInWebLink,
34
+ startsWithWebURL: part.startsWithWebURL,
35
+ webTailLength: part.webTailLength,
35
36
  nodes: [node],
36
37
  htmlSize: part.htmlSize +
37
38
  opening(node).length +
38
39
  (voidElement(kind) ? 0 : kind.length + 3),
39
40
  };
40
41
  }
41
- export function linkBoundary(endsInWebLink, next) {
42
- return endsInWebLink && /^[^\p{White_Space}\uFEFF]/u.test(next) ? " " : "";
42
+ const leadingPunctuation = /^[.,!?:;)\]}]*/;
43
+ const whitespace = /[\p{White_Space}\uFEFF]/u;
44
+ const leftDelimiter = /[\p{White_Space}\uFEFF(\[{]$/u;
45
+ export function markText(part, delimiter) {
46
+ if (!part.webTailLength) {
47
+ part.text = delimiter + part.text + delimiter;
48
+ part.startsWithWebURL = false;
49
+ return;
50
+ }
51
+ const prefix = part.text.slice(0, -part.webTailLength);
52
+ const content = prefix.replace(/[\p{White_Space}\uFEFF]+$/u, "");
53
+ if (content) {
54
+ const gap = prefix.slice(content.length) || " ";
55
+ part.text = delimiter + content + delimiter + gap + part.text.slice(prefix.length);
56
+ part.startsWithWebURL = false;
57
+ }
43
58
  }
44
59
  export function join(parts, sep = "") {
45
60
  const nodes = [];
46
61
  let htmlSize = 0;
47
62
  const text = [];
48
- let endsInWebLink = false;
63
+ let startsWithWebURL = false;
64
+ let webTailLength = 0;
65
+ // A punctuation-only sibling is terminal only if the following text permits it.
66
+ const punctuationLengths = [];
67
+ const canFollowURL = [];
68
+ if (parts.some(part => part.webTailLength)) {
69
+ let terminal = true;
70
+ for (let i = parts.length - 1; i >= 0; i--) {
71
+ const value = parts[i].text;
72
+ const count = leadingPunctuation.exec(value)[0].length;
73
+ if (count < value.length)
74
+ terminal = whitespace.test(value[count]);
75
+ else if (sep)
76
+ terminal = true;
77
+ punctuationLengths[i] = count;
78
+ canFollowURL[i] = terminal;
79
+ }
80
+ }
49
81
  for (const [i, part] of parts.entries()) {
50
82
  if (i && sep) {
51
- text.push(linkBoundary(endsInWebLink, sep), sep);
52
- endsInWebLink = false;
83
+ text.push(sep);
84
+ webTailLength = 0;
53
85
  }
54
86
  if (part.text) {
55
- text.push(linkBoundary(endsInWebLink, part.text), part.text);
56
- endsInWebLink = !!part.endsInWebLink;
87
+ const before = text[text.length - 1];
88
+ if (!before)
89
+ startsWithWebURL = !!part.startsWithWebURL;
90
+ const gap = (webTailLength && !canFollowURL[i]) ||
91
+ (before && part.startsWithWebURL && !leftDelimiter.test(before));
92
+ if (gap)
93
+ text.push(" ");
94
+ text.push(part.text);
95
+ if (webTailLength && !gap && punctuationLengths[i] === part.text.length) {
96
+ webTailLength += part.text.length;
97
+ }
98
+ else {
99
+ webTailLength = part.webTailLength ?? 0;
100
+ }
57
101
  }
58
102
  htmlSize += part.htmlSize;
59
103
  for (const node of part.nodes) {
@@ -64,7 +108,7 @@ export function join(parts, sep = "") {
64
108
  nodes.push(node);
65
109
  }
66
110
  }
67
- return { text: text.join(""), endsInWebLink, nodes, htmlSize };
111
+ return { text: text.join(""), startsWithWebURL, webTailLength, nodes, htmlSize };
68
112
  }
69
113
  export function contains(nodes, kind) {
70
114
  return nodes.some((n) => n.kind === kind || contains(n.children, kind));
package/dist/render.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { displayLink, normalizeLink } from "./links.js";
2
2
  import { Target, ConversionError, unknownTarget, describe, validate, } from "./api.js";
3
- import { contains, linkBoundary, html, join, literal, wrap, } from "./fragment.js";
3
+ import { contains, markText, html, join, literal, wrap, } from "./fragment.js";
4
4
  import { MarkdownTokenizer, decodeReferences } from "./tokenizer.js";
5
5
  import markedFootnote from "marked-footnote";
6
6
  import { Marked } from "marked";
@@ -114,8 +114,7 @@ class Renderer {
114
114
  del: this.doubled ? "~~" : "~",
115
115
  strong: this.bold,
116
116
  }[t.type];
117
- value.text = delimiter + value.text + linkBoundary(value.endsInWebLink, delimiter) + delimiter;
118
- value.endsInWebLink = false;
117
+ markText(value, delimiter);
119
118
  }
120
119
  return value;
121
120
  }
@@ -125,8 +124,7 @@ class Renderer {
125
124
  const value = wrap("h" + t.depth, children(t.tokens));
126
125
  if (this.markers &&
127
126
  !(t.tokens.length === 1 && t.tokens[0].type === "strong")) {
128
- value.text = this.bold + value.text + linkBoundary(value.endsInWebLink, this.bold) + this.bold;
129
- value.endsInWebLink = false;
127
+ markText(value, this.bold);
130
128
  }
131
129
  return value;
132
130
  }
@@ -151,8 +149,13 @@ class Renderer {
151
149
  let label = angleLink ? literal(t.text) : children(t.tokens);
152
150
  const authored = angleLink ? t.href : decodeReferences(t.href);
153
151
  const href = normalizeLink(authored);
154
- if (contains(label.nodes, "a"))
155
- label = { ...literal(label.text), endsInWebLink: label.endsInWebLink };
152
+ if (contains(label.nodes, "a")) {
153
+ label = {
154
+ ...literal(label.text),
155
+ startsWithWebURL: label.startsWithWebURL,
156
+ webTailLength: label.webTailLength,
157
+ };
158
+ }
156
159
  if (t.type === "image")
157
160
  this.warn("IMAGE_AS_LINK");
158
161
  if (!href) {
@@ -163,12 +166,27 @@ class Renderer {
163
166
  if (!label.text || label.text === authored)
164
167
  label = literal(display);
165
168
  const result = wrap("a", label, { url: href });
166
- if (label.text !== display) {
167
- result.text = this.mobileChat && /^https?:/i.test(href)
168
- ? label.text + linkBoundary(label.endsInWebLink, ":") + ": " + display
169
- : label.text + " (" + display + ")";
169
+ const web = this.mobileChat && /^https?:/i.test(href);
170
+ if (web) {
171
+ const host = /^https?:\/\/([^/:?#]+)/i.exec(display)[1];
172
+ const asciiFold = (s) => s.replace(/[A-Z]/g, c => c.toLowerCase());
173
+ const hostLabel = label.nodes.length === 1 &&
174
+ label.nodes[0].kind === "text" &&
175
+ asciiFold(label.text) === asciiFold(host);
176
+ if (hostLabel || label.text === display) {
177
+ result.text = display;
178
+ result.startsWithWebURL = true;
179
+ }
180
+ else {
181
+ result.text = label.text + (label.webTailLength ? " : " : ": ") + display;
182
+ }
183
+ result.webTailLength = display.length;
184
+ }
185
+ else {
186
+ result.webTailLength = 0;
187
+ if (label.text !== display)
188
+ result.text = label.text + " (" + display + ")";
170
189
  }
171
- result.endsInWebLink = this.mobileChat && /^https?:/i.test(href);
172
190
  return result;
173
191
  }
174
192
  case "list": {
@@ -229,7 +247,8 @@ class Renderer {
229
247
  const cards = rows.map((row) => {
230
248
  const fields = row.map((cell, i) => {
231
249
  const label = header[i]?.text || "Column " + (i + 1);
232
- return label + linkBoundary(header[i]?.endsInWebLink, ":") + ": " + (this.mobileChat && !cell.text ? "—" : cell.text);
250
+ const separator = header[i]?.webTailLength ? " : " : ": ";
251
+ return label + separator + (this.mobileChat && !cell.text ? "—" : cell.text);
233
252
  });
234
253
  return this.mobileChat ? "- " + fields.join(" · ") : fields.join("\n");
235
254
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mdto-messages",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {