mdto-messages 0.2.0 → 0.3.0

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,9 +9,11 @@ export interface Fragment {
9
9
  text: string;
10
10
  nodes: DocumentNode[];
11
11
  htmlSize: number;
12
+ endsInWebLink?: boolean;
12
13
  }
13
14
  export declare function html(nodes: DocumentNode[]): string;
14
15
  export declare function literal(text: string): Fragment;
15
16
  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;
16
18
  export declare function join(parts: Fragment[], sep?: string): Fragment;
17
19
  export declare function contains(nodes: DocumentNode[], kind: string): boolean;
package/dist/fragment.js CHANGED
@@ -31,16 +31,30 @@ 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
35
  nodes: [node],
35
36
  htmlSize: part.htmlSize +
36
37
  opening(node).length +
37
38
  (voidElement(kind) ? 0 : kind.length + 3),
38
39
  };
39
40
  }
41
+ export function linkBoundary(endsInWebLink, next) {
42
+ return endsInWebLink && /^[^\p{White_Space}\uFEFF]/u.test(next) ? " " : "";
43
+ }
40
44
  export function join(parts, sep = "") {
41
45
  const nodes = [];
42
46
  let htmlSize = 0;
43
- for (const part of parts) {
47
+ const text = [];
48
+ let endsInWebLink = false;
49
+ for (const [i, part] of parts.entries()) {
50
+ if (i && sep) {
51
+ text.push(linkBoundary(endsInWebLink, sep), sep);
52
+ endsInWebLink = false;
53
+ }
54
+ if (part.text) {
55
+ text.push(linkBoundary(endsInWebLink, part.text), part.text);
56
+ endsInWebLink = !!part.endsInWebLink;
57
+ }
44
58
  htmlSize += part.htmlSize;
45
59
  for (const node of part.nodes) {
46
60
  const last = nodes[nodes.length - 1];
@@ -50,7 +64,7 @@ export function join(parts, sep = "") {
50
64
  nodes.push(node);
51
65
  }
52
66
  }
53
- return { text: parts.map((p) => p.text).join(sep), nodes, htmlSize };
67
+ return { text: text.join(""), endsInWebLink, nodes, htmlSize };
54
68
  }
55
69
  export function contains(nodes, kind) {
56
70
  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, linkBoundary, 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,8 @@ class Renderer {
112
114
  del: this.doubled ? "~~" : "~",
113
115
  strong: this.bold,
114
116
  }[t.type];
115
- value.text = delimiter + value.text + delimiter;
117
+ value.text = delimiter + value.text + linkBoundary(value.endsInWebLink, delimiter) + delimiter;
118
+ value.endsInWebLink = false;
116
119
  }
117
120
  return value;
118
121
  }
@@ -121,8 +124,10 @@ class Renderer {
121
124
  this.warn("FORMAT_AS_TEXT");
122
125
  const value = wrap("h" + t.depth, children(t.tokens));
123
126
  if (this.markers &&
124
- !(t.tokens.length === 1 && t.tokens[0].type === "strong"))
125
- value.text = this.bold + value.text + this.bold;
127
+ !(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;
130
+ }
126
131
  return value;
127
132
  }
128
133
  case "paragraph":
@@ -147,7 +152,7 @@ class Renderer {
147
152
  const authored = angleLink ? t.href : decodeReferences(t.href);
148
153
  const href = normalizeLink(authored);
149
154
  if (contains(label.nodes, "a"))
150
- label = literal(label.text);
155
+ label = { ...literal(label.text), endsInWebLink: label.endsInWebLink };
151
156
  if (t.type === "image")
152
157
  this.warn("IMAGE_AS_LINK");
153
158
  if (!href) {
@@ -158,10 +163,12 @@ class Renderer {
158
163
  if (!label.text || label.text === authored)
159
164
  label = literal(display);
160
165
  const result = wrap("a", label, { url: href });
161
- result.text =
162
- label.text === display
163
- ? label.text
166
+ if (label.text !== display) {
167
+ result.text = this.mobileChat && /^https?:/i.test(href)
168
+ ? label.text + linkBoundary(label.endsInWebLink, ":") + ": " + display
164
169
  : label.text + " (" + display + ")";
170
+ }
171
+ result.endsInWebLink = this.mobileChat && /^https?:/i.test(href);
165
172
  return result;
166
173
  }
167
174
  case "list": {
@@ -219,11 +226,15 @@ class Renderer {
219
226
  const repeatedLabels = header.reduce((n, cell) => n + cell.text.length, 0) * rows.length;
220
227
  if (repeatedLabels > 262144)
221
228
  throw new ConversionError("LIMIT");
222
- const cards = rows.map((row) => row
223
- .map((cell, i) => (header[i]?.text || "Column " + (i + 1)) + ": " + cell.text)
224
- .join("\n"));
229
+ const cards = rows.map((row) => {
230
+ const fields = row.map((cell, i) => {
231
+ const label = header[i]?.text || "Column " + (i + 1);
232
+ return label + linkBoundary(header[i]?.endsInWebLink, ":") + ": " + (this.mobileChat && !cell.text ? "—" : cell.text);
233
+ });
234
+ return this.mobileChat ? "- " + fields.join(" · ") : fields.join("\n");
235
+ });
225
236
  const text = cards.length
226
- ? cards.join("\n\n")
237
+ ? cards.join(this.mobileChat ? "\n" : "\n\n")
227
238
  : header.map((h) => h.text).join(" | ");
228
239
  if (this.target === "slack") {
229
240
  const lines = text.split("\n");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mdto-messages",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {