mdto-messages 0.2.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.
- package/dist/fragment.d.ts +3 -0
- package/dist/fragment.js +60 -2
- package/dist/render.js +44 -14
- package/package.json +1 -1
package/dist/fragment.d.ts
CHANGED
|
@@ -9,9 +9,12 @@ export interface Fragment {
|
|
|
9
9
|
text: string;
|
|
10
10
|
nodes: DocumentNode[];
|
|
11
11
|
htmlSize: number;
|
|
12
|
+
startsWithWebURL?: boolean;
|
|
13
|
+
webTailLength?: number;
|
|
12
14
|
}
|
|
13
15
|
export declare function html(nodes: DocumentNode[]): string;
|
|
14
16
|
export declare function literal(text: string): Fragment;
|
|
15
17
|
export declare function wrap(kind: string, part: Fragment, attributes?: Pick<DocumentNode, "url" | "start">): Fragment;
|
|
18
|
+
export declare function markText(part: Fragment, delimiter: string): void;
|
|
16
19
|
export declare function join(parts: Fragment[], sep?: string): Fragment;
|
|
17
20
|
export declare function contains(nodes: DocumentNode[], kind: string): boolean;
|
package/dist/fragment.js
CHANGED
|
@@ -31,16 +31,74 @@ export function wrap(kind, part, attributes = {}) {
|
|
|
31
31
|
const node = { kind, ...attributes, children: part.nodes };
|
|
32
32
|
return {
|
|
33
33
|
text: part.text,
|
|
34
|
+
startsWithWebURL: part.startsWithWebURL,
|
|
35
|
+
webTailLength: part.webTailLength,
|
|
34
36
|
nodes: [node],
|
|
35
37
|
htmlSize: part.htmlSize +
|
|
36
38
|
opening(node).length +
|
|
37
39
|
(voidElement(kind) ? 0 : kind.length + 3),
|
|
38
40
|
};
|
|
39
41
|
}
|
|
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
|
+
}
|
|
58
|
+
}
|
|
40
59
|
export function join(parts, sep = "") {
|
|
41
60
|
const nodes = [];
|
|
42
61
|
let htmlSize = 0;
|
|
43
|
-
|
|
62
|
+
const text = [];
|
|
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
|
+
}
|
|
81
|
+
for (const [i, part] of parts.entries()) {
|
|
82
|
+
if (i && sep) {
|
|
83
|
+
text.push(sep);
|
|
84
|
+
webTailLength = 0;
|
|
85
|
+
}
|
|
86
|
+
if (part.text) {
|
|
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
|
+
}
|
|
101
|
+
}
|
|
44
102
|
htmlSize += part.htmlSize;
|
|
45
103
|
for (const node of part.nodes) {
|
|
46
104
|
const last = nodes[nodes.length - 1];
|
|
@@ -50,7 +108,7 @@ export function join(parts, sep = "") {
|
|
|
50
108
|
nodes.push(node);
|
|
51
109
|
}
|
|
52
110
|
}
|
|
53
|
-
return { text:
|
|
111
|
+
return { text: text.join(""), startsWithWebURL, webTailLength, nodes, htmlSize };
|
|
54
112
|
}
|
|
55
113
|
export function contains(nodes, kind) {
|
|
56
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, 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";
|
|
@@ -9,6 +9,7 @@ class Renderer {
|
|
|
9
9
|
policy;
|
|
10
10
|
warnings = new Set();
|
|
11
11
|
rich;
|
|
12
|
+
mobileChat;
|
|
12
13
|
doubled;
|
|
13
14
|
markers;
|
|
14
15
|
bold;
|
|
@@ -19,6 +20,7 @@ class Renderer {
|
|
|
19
20
|
this.target = target;
|
|
20
21
|
this.policy = policy;
|
|
21
22
|
this.rich = ["html", "email", "teams", "slack"].includes(target);
|
|
23
|
+
this.mobileChat = target === "whatsapp" || target === "google-chat";
|
|
22
24
|
this.doubled = target === "discord" || target === "telegram";
|
|
23
25
|
this.markers =
|
|
24
26
|
this.doubled ||
|
|
@@ -112,7 +114,7 @@ class Renderer {
|
|
|
112
114
|
del: this.doubled ? "~~" : "~",
|
|
113
115
|
strong: this.bold,
|
|
114
116
|
}[t.type];
|
|
115
|
-
value
|
|
117
|
+
markText(value, delimiter);
|
|
116
118
|
}
|
|
117
119
|
return value;
|
|
118
120
|
}
|
|
@@ -121,8 +123,9 @@ class Renderer {
|
|
|
121
123
|
this.warn("FORMAT_AS_TEXT");
|
|
122
124
|
const value = wrap("h" + t.depth, children(t.tokens));
|
|
123
125
|
if (this.markers &&
|
|
124
|
-
!(t.tokens.length === 1 && t.tokens[0].type === "strong"))
|
|
125
|
-
value
|
|
126
|
+
!(t.tokens.length === 1 && t.tokens[0].type === "strong")) {
|
|
127
|
+
markText(value, this.bold);
|
|
128
|
+
}
|
|
126
129
|
return value;
|
|
127
130
|
}
|
|
128
131
|
case "paragraph":
|
|
@@ -146,8 +149,13 @@ class Renderer {
|
|
|
146
149
|
let label = angleLink ? literal(t.text) : children(t.tokens);
|
|
147
150
|
const authored = angleLink ? t.href : decodeReferences(t.href);
|
|
148
151
|
const href = normalizeLink(authored);
|
|
149
|
-
if (contains(label.nodes, "a"))
|
|
150
|
-
label =
|
|
152
|
+
if (contains(label.nodes, "a")) {
|
|
153
|
+
label = {
|
|
154
|
+
...literal(label.text),
|
|
155
|
+
startsWithWebURL: label.startsWithWebURL,
|
|
156
|
+
webTailLength: label.webTailLength,
|
|
157
|
+
};
|
|
158
|
+
}
|
|
151
159
|
if (t.type === "image")
|
|
152
160
|
this.warn("IMAGE_AS_LINK");
|
|
153
161
|
if (!href) {
|
|
@@ -158,10 +166,27 @@ class Renderer {
|
|
|
158
166
|
if (!label.text || label.text === authored)
|
|
159
167
|
label = literal(display);
|
|
160
168
|
const result = wrap("a", label, { url: href });
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
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 + ")";
|
|
189
|
+
}
|
|
165
190
|
return result;
|
|
166
191
|
}
|
|
167
192
|
case "list": {
|
|
@@ -219,11 +244,16 @@ class Renderer {
|
|
|
219
244
|
const repeatedLabels = header.reduce((n, cell) => n + cell.text.length, 0) * rows.length;
|
|
220
245
|
if (repeatedLabels > 262144)
|
|
221
246
|
throw new ConversionError("LIMIT");
|
|
222
|
-
const cards = rows.map((row) =>
|
|
223
|
-
.map((cell, i) =>
|
|
224
|
-
|
|
247
|
+
const cards = rows.map((row) => {
|
|
248
|
+
const fields = row.map((cell, i) => {
|
|
249
|
+
const label = header[i]?.text || "Column " + (i + 1);
|
|
250
|
+
const separator = header[i]?.webTailLength ? " : " : ": ";
|
|
251
|
+
return label + separator + (this.mobileChat && !cell.text ? "—" : cell.text);
|
|
252
|
+
});
|
|
253
|
+
return this.mobileChat ? "- " + fields.join(" · ") : fields.join("\n");
|
|
254
|
+
});
|
|
225
255
|
const text = cards.length
|
|
226
|
-
? cards.join("\n\n")
|
|
256
|
+
? cards.join(this.mobileChat ? "\n" : "\n\n")
|
|
227
257
|
: header.map((h) => h.text).join(" | ");
|
|
228
258
|
if (this.target === "slack") {
|
|
229
259
|
const lines = text.split("\n");
|