mdto-messages 0.0.0-stage → 0.1.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.
- package/LICENSE +21 -0
- package/README.md +21 -2
- package/dist/api.d.ts +42 -0
- package/dist/api.js +56 -0
- package/dist/buffer.d.ts +10 -0
- package/dist/buffer.js +37 -0
- package/dist/diagnostics.d.ts +42 -0
- package/dist/diagnostics.js +43 -0
- package/dist/flatten.d.ts +19 -0
- package/dist/flatten.js +110 -0
- package/dist/fragment.d.ts +17 -0
- package/dist/fragment.js +57 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +4 -0
- package/dist/links.d.ts +4 -0
- package/dist/links.js +50 -0
- package/dist/messages.d.ts +53 -0
- package/dist/messages.js +200 -0
- package/dist/render.d.ts +10 -0
- package/dist/render.js +300 -0
- package/dist/slack.d.ts +38 -0
- package/dist/slack.js +192 -0
- package/dist/tokenizer.d.ts +9 -0
- package/dist/tokenizer.js +57 -0
- package/docs/licenses/entities.txt +11 -0
- package/docs/licenses/marked-footnote.txt +21 -0
- package/docs/licenses/marked.txt +44 -0
- package/package.json +61 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Jerome Meinke
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,22 @@
|
|
|
1
|
-
#
|
|
1
|
+
# mdto-messages
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://www.npmjs.com/package/mdto-messages)
|
|
4
|
+
|
|
5
|
+
Render Markdown as a messaging draft, HTML email fragment or provider API body. Conversion runs locally in Node and modern browsers.
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install mdto-messages
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
```js
|
|
12
|
+
import { convert, toMessages } from "mdto-messages";
|
|
13
|
+
|
|
14
|
+
const draft = convert("**Ship** today", "whatsapp");
|
|
15
|
+
console.log(draft.text); // *Ship* today
|
|
16
|
+
|
|
17
|
+
const { messages, diagnostics } = toMessages("**Ship** today", "telegram");
|
|
18
|
+
console.log(messages[0].entities); // [{ type: "bold", offset: 0, length: 4 }]
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
The application supplies recipients, credentials, delivery and clipboard access.
|
|
22
|
+
[API and limits](https://github.com/jmeinke/mdto-messages/blob/main/docs/api.md) · [Examples](https://github.com/jmeinke/mdto-messages/blob/main/docs/how-to.md) · [MIT](https://github.com/jmeinke/mdto-messages/blob/main/LICENSE)
|
package/dist/api.d.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { diagnostics } from "./diagnostics.js";
|
|
2
|
+
export declare const Target: {
|
|
3
|
+
readonly Plaintext: "plaintext";
|
|
4
|
+
readonly HTML: "html";
|
|
5
|
+
readonly Email: "email";
|
|
6
|
+
readonly WhatsApp: "whatsapp";
|
|
7
|
+
readonly Threema: "threema";
|
|
8
|
+
readonly Signal: "signal";
|
|
9
|
+
readonly Viber: "viber";
|
|
10
|
+
readonly GoogleChat: "google-chat";
|
|
11
|
+
readonly Discord: "discord";
|
|
12
|
+
readonly Telegram: "telegram";
|
|
13
|
+
readonly Teams: "teams";
|
|
14
|
+
readonly Slack: "slack";
|
|
15
|
+
};
|
|
16
|
+
export type Target = (typeof Target)[keyof typeof Target];
|
|
17
|
+
export type Policy = "readable" | "compact" | "strict";
|
|
18
|
+
export interface Options {
|
|
19
|
+
policy?: Policy;
|
|
20
|
+
maxInputLength?: number;
|
|
21
|
+
}
|
|
22
|
+
export type DiagnosticCode = keyof typeof diagnostics;
|
|
23
|
+
export interface Diagnostic {
|
|
24
|
+
code: DiagnosticCode;
|
|
25
|
+
severity: "warning";
|
|
26
|
+
action: string;
|
|
27
|
+
}
|
|
28
|
+
export declare function describe(codes: Iterable<DiagnosticCode>): Diagnostic[];
|
|
29
|
+
export type ErrorCode = "INVALID_INPUT" | "LIMIT" | "UNKNOWN_TARGET" | "LOSS";
|
|
30
|
+
export declare class ConversionError extends Error {
|
|
31
|
+
readonly code: ErrorCode;
|
|
32
|
+
constructor(code: ErrorCode, detail?: string);
|
|
33
|
+
}
|
|
34
|
+
export declare function unknownTarget(target: unknown, valid: readonly string[]): ConversionError;
|
|
35
|
+
export interface Result {
|
|
36
|
+
target: Target;
|
|
37
|
+
text: string;
|
|
38
|
+
html?: string;
|
|
39
|
+
format: "html" | "text" | "markers";
|
|
40
|
+
diagnostics: Diagnostic[];
|
|
41
|
+
}
|
|
42
|
+
export declare function validate(markdown: string, options: Options): string;
|
package/dist/api.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { diagnostics } from "./diagnostics.js";
|
|
2
|
+
export const Target = {
|
|
3
|
+
Plaintext: "plaintext",
|
|
4
|
+
HTML: "html",
|
|
5
|
+
Email: "email",
|
|
6
|
+
WhatsApp: "whatsapp",
|
|
7
|
+
Threema: "threema",
|
|
8
|
+
Signal: "signal",
|
|
9
|
+
Viber: "viber",
|
|
10
|
+
GoogleChat: "google-chat",
|
|
11
|
+
Discord: "discord",
|
|
12
|
+
Telegram: "telegram",
|
|
13
|
+
Teams: "teams",
|
|
14
|
+
Slack: "slack",
|
|
15
|
+
};
|
|
16
|
+
export function describe(codes) {
|
|
17
|
+
return Array.from(codes, (code) => ({ code, ...diagnostics[code] }));
|
|
18
|
+
}
|
|
19
|
+
const errors = {
|
|
20
|
+
INVALID_INPUT: "Expected valid Markdown text and conversion options.",
|
|
21
|
+
LIMIT: "Conversion exceeded a document, output or platform limit.",
|
|
22
|
+
UNKNOWN_TARGET: "Unsupported destination for this operation.",
|
|
23
|
+
LOSS: "Strict policy prevents a conversion that would lose formatting or content.",
|
|
24
|
+
};
|
|
25
|
+
export class ConversionError extends Error {
|
|
26
|
+
code;
|
|
27
|
+
constructor(code, detail = errors[code]) {
|
|
28
|
+
super(detail);
|
|
29
|
+
this.code = code;
|
|
30
|
+
this.name = "ConversionError";
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
export function unknownTarget(target, valid) {
|
|
34
|
+
const name = typeof target === "string" && /^[a-z0-9_-]{1,40}$/i.test(target)
|
|
35
|
+
? ' "' + target + '"'
|
|
36
|
+
: "";
|
|
37
|
+
return new ConversionError("UNKNOWN_TARGET", "Unknown target" + name + ". Expected one of: " + valid.join(", ") + ".");
|
|
38
|
+
}
|
|
39
|
+
export function validate(markdown, options) {
|
|
40
|
+
if (typeof markdown !== "string" || !options || typeof options !== "object")
|
|
41
|
+
throw new ConversionError("INVALID_INPUT");
|
|
42
|
+
if (Array.isArray(options) ||
|
|
43
|
+
Object.keys(options).some((key) => key !== "policy" && key !== "maxInputLength"))
|
|
44
|
+
throw new ConversionError("INVALID_INPUT", "Supported conversion options are policy and maxInputLength.");
|
|
45
|
+
const limit = options.maxInputLength === undefined ? 32768 : options.maxInputLength;
|
|
46
|
+
if (!Number.isInteger(limit) || limit < 1 || limit > 131072)
|
|
47
|
+
throw new ConversionError("INVALID_INPUT", "maxInputLength must be an integer from 1 through 131072 UTF-16 units.");
|
|
48
|
+
if (markdown.length > limit)
|
|
49
|
+
throw new ConversionError("LIMIT", `Input contains ${markdown.length} UTF-16 units; maximum is ${limit}.`);
|
|
50
|
+
if (/[\u0000-\u0008\u000b\u000c\u000e-\u001f\uD800-\uDFFF\uFFFE\uFFFF]/u.test(markdown))
|
|
51
|
+
throw new ConversionError("INVALID_INPUT", "Input contains a forbidden control character or unpaired surrogate.");
|
|
52
|
+
if (options.policy !== undefined &&
|
|
53
|
+
!["readable", "compact", "strict"].includes(options.policy))
|
|
54
|
+
throw new ConversionError("INVALID_INPUT", "policy must be readable, compact or strict.");
|
|
55
|
+
return markdown.replace(/^\uFEFF/, "");
|
|
56
|
+
}
|
package/dist/buffer.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { convert } from "./render.js";
|
|
2
|
+
import { type Target } from "./api.js";
|
|
3
|
+
/** Collect UTF-8 chunks and parse the complete draft once, on finish. */
|
|
4
|
+
export declare class DraftBuffer {
|
|
5
|
+
private readonly chunks;
|
|
6
|
+
private size;
|
|
7
|
+
private closed;
|
|
8
|
+
append(chunk: Uint8Array): void;
|
|
9
|
+
finish(destination: Target, options?: Parameters<typeof convert>[2]): import("./api.js").Result;
|
|
10
|
+
}
|
package/dist/buffer.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { convert } from "./render.js";
|
|
2
|
+
import { ConversionError } from "./api.js";
|
|
3
|
+
/** Collect UTF-8 chunks and parse the complete draft once, on finish. */
|
|
4
|
+
export class DraftBuffer {
|
|
5
|
+
chunks = [];
|
|
6
|
+
size = 0;
|
|
7
|
+
closed = false;
|
|
8
|
+
append(chunk) {
|
|
9
|
+
if (this.closed || !(chunk instanceof Uint8Array))
|
|
10
|
+
throw new ConversionError("INVALID_INPUT");
|
|
11
|
+
if (this.size + chunk.byteLength > 131072)
|
|
12
|
+
throw new ConversionError("LIMIT");
|
|
13
|
+
if (chunk.byteLength)
|
|
14
|
+
this.chunks.push(chunk.slice());
|
|
15
|
+
this.size += chunk.byteLength;
|
|
16
|
+
}
|
|
17
|
+
finish(destination, options) {
|
|
18
|
+
if (this.closed)
|
|
19
|
+
throw new ConversionError("INVALID_INPUT");
|
|
20
|
+
this.closed = true;
|
|
21
|
+
const data = new Uint8Array(this.size);
|
|
22
|
+
let offset = 0;
|
|
23
|
+
for (const chunk of this.chunks) {
|
|
24
|
+
data.set(chunk, offset);
|
|
25
|
+
offset += chunk.length;
|
|
26
|
+
}
|
|
27
|
+
this.chunks.length = 0;
|
|
28
|
+
let text;
|
|
29
|
+
try {
|
|
30
|
+
text = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }).decode(data);
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
throw new ConversionError("INVALID_INPUT");
|
|
34
|
+
}
|
|
35
|
+
return convert(text, destination, options);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
export declare const diagnostics: {
|
|
2
|
+
readonly RAW_HTML_LITERAL: {
|
|
3
|
+
readonly severity: "warning";
|
|
4
|
+
readonly action: "Embedded HTML was displayed as literal text.";
|
|
5
|
+
};
|
|
6
|
+
readonly LINK_OMITTED: {
|
|
7
|
+
readonly severity: "warning";
|
|
8
|
+
readonly action: "The label was retained and its destination omitted.";
|
|
9
|
+
};
|
|
10
|
+
readonly IMAGE_AS_LINK: {
|
|
11
|
+
readonly severity: "warning";
|
|
12
|
+
readonly action: "The image became a description with a link when allowed.";
|
|
13
|
+
};
|
|
14
|
+
readonly TABLE_AS_ROWS: {
|
|
15
|
+
readonly severity: "warning";
|
|
16
|
+
readonly action: "Cells became labeled rows.";
|
|
17
|
+
};
|
|
18
|
+
readonly UNSUPPORTED_LITERAL: {
|
|
19
|
+
readonly severity: "warning";
|
|
20
|
+
readonly action: "Unsupported markup was preserved where available.";
|
|
21
|
+
};
|
|
22
|
+
readonly AMBIGUOUS_MARKER: {
|
|
23
|
+
readonly severity: "warning";
|
|
24
|
+
readonly action: "A literal marker was retained. The receiving composer can reinterpret it.";
|
|
25
|
+
};
|
|
26
|
+
readonly FORMAT_AS_TEXT: {
|
|
27
|
+
readonly severity: "warning";
|
|
28
|
+
readonly action: "Formatting was reduced to readable text or target-specific emphasis.";
|
|
29
|
+
};
|
|
30
|
+
readonly ENTITY_SIMPLIFIED: {
|
|
31
|
+
readonly severity: "warning";
|
|
32
|
+
readonly action: "Overlapping formatting was simplified to preserve a link or code span.";
|
|
33
|
+
};
|
|
34
|
+
readonly EMPTY_MESSAGE: {
|
|
35
|
+
readonly severity: "warning";
|
|
36
|
+
readonly action: "An empty draft produced no message payload.";
|
|
37
|
+
};
|
|
38
|
+
readonly MENTION_AS_TEXT: {
|
|
39
|
+
readonly severity: "warning";
|
|
40
|
+
readonly action: "An angle marker was made visible as ordinary text.";
|
|
41
|
+
};
|
|
42
|
+
};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// Generated from spec/diagnostics.json.
|
|
2
|
+
export const diagnostics = {
|
|
3
|
+
"RAW_HTML_LITERAL": {
|
|
4
|
+
"severity": "warning",
|
|
5
|
+
"action": "Embedded HTML was displayed as literal text."
|
|
6
|
+
},
|
|
7
|
+
"LINK_OMITTED": {
|
|
8
|
+
"severity": "warning",
|
|
9
|
+
"action": "The label was retained and its destination omitted."
|
|
10
|
+
},
|
|
11
|
+
"IMAGE_AS_LINK": {
|
|
12
|
+
"severity": "warning",
|
|
13
|
+
"action": "The image became a description with a link when allowed."
|
|
14
|
+
},
|
|
15
|
+
"TABLE_AS_ROWS": {
|
|
16
|
+
"severity": "warning",
|
|
17
|
+
"action": "Cells became labeled rows."
|
|
18
|
+
},
|
|
19
|
+
"UNSUPPORTED_LITERAL": {
|
|
20
|
+
"severity": "warning",
|
|
21
|
+
"action": "Unsupported markup was preserved where available."
|
|
22
|
+
},
|
|
23
|
+
"AMBIGUOUS_MARKER": {
|
|
24
|
+
"severity": "warning",
|
|
25
|
+
"action": "A literal marker was retained. The receiving composer can reinterpret it."
|
|
26
|
+
},
|
|
27
|
+
"FORMAT_AS_TEXT": {
|
|
28
|
+
"severity": "warning",
|
|
29
|
+
"action": "Formatting was reduced to readable text or target-specific emphasis."
|
|
30
|
+
},
|
|
31
|
+
"ENTITY_SIMPLIFIED": {
|
|
32
|
+
"severity": "warning",
|
|
33
|
+
"action": "Overlapping formatting was simplified to preserve a link or code span."
|
|
34
|
+
},
|
|
35
|
+
"EMPTY_MESSAGE": {
|
|
36
|
+
"severity": "warning",
|
|
37
|
+
"action": "An empty draft produced no message payload."
|
|
38
|
+
},
|
|
39
|
+
"MENTION_AS_TEXT": {
|
|
40
|
+
"severity": "warning",
|
|
41
|
+
"action": "An angle marker was made visible as ordinary text."
|
|
42
|
+
}
|
|
43
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { DocumentNode } from "./fragment.js";
|
|
2
|
+
type EntityType = "bold" | "italic" | "strikethrough" | "code" | "pre" | "text_link";
|
|
3
|
+
export interface Entity {
|
|
4
|
+
type: EntityType;
|
|
5
|
+
offset: number;
|
|
6
|
+
length: number;
|
|
7
|
+
url?: string;
|
|
8
|
+
}
|
|
9
|
+
interface Run {
|
|
10
|
+
text: string;
|
|
11
|
+
entities: Entity[];
|
|
12
|
+
}
|
|
13
|
+
export declare function flatten(root: DocumentNode): Run;
|
|
14
|
+
/** Each chunk is bounded in UTF-16 units and ends at a Unicode scalar boundary. */
|
|
15
|
+
export declare function chunks(text: string, limit: number): {
|
|
16
|
+
start: number;
|
|
17
|
+
text: string;
|
|
18
|
+
}[];
|
|
19
|
+
export {};
|
package/dist/flatten.js
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { ConversionError } from "./api.js";
|
|
2
|
+
export function flatten(root) {
|
|
3
|
+
const pieces = [], entities = [];
|
|
4
|
+
let length = 0;
|
|
5
|
+
function add(text) {
|
|
6
|
+
length += text.length;
|
|
7
|
+
if (length > 262144)
|
|
8
|
+
throw new ConversionError("LIMIT");
|
|
9
|
+
pieces.push(text);
|
|
10
|
+
}
|
|
11
|
+
function visit(n, inPre = false) {
|
|
12
|
+
const start = length;
|
|
13
|
+
if (n.kind === "text") {
|
|
14
|
+
add(n.text);
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
if (n.kind === "br" || n.kind === "hr") {
|
|
18
|
+
add(n.kind === "br" ? "\n" : "—");
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
if (n.kind === "table") {
|
|
22
|
+
const rows = n.children.flatMap((section) => section.children);
|
|
23
|
+
const header = rows[0]?.children ?? [];
|
|
24
|
+
const label = (cell) => cell ? flatten(cell).text : "";
|
|
25
|
+
const labels = header.map(label);
|
|
26
|
+
if (rows.length === 1)
|
|
27
|
+
add(labels.join(" | "));
|
|
28
|
+
rows.slice(1).forEach((row, i) => {
|
|
29
|
+
if (i)
|
|
30
|
+
add("\n\n");
|
|
31
|
+
row.children.forEach((cell, j) => {
|
|
32
|
+
if (j)
|
|
33
|
+
add("\n");
|
|
34
|
+
add((labels[j] || "Column " + (j + 1)) + ": ");
|
|
35
|
+
visit(cell);
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
n.children.forEach((child, i) => {
|
|
41
|
+
if (n.kind === "div" && i > 1)
|
|
42
|
+
add("\n\n");
|
|
43
|
+
if (n.kind === "ul" || n.kind === "ol") {
|
|
44
|
+
if (i)
|
|
45
|
+
add("\n");
|
|
46
|
+
add(n.kind === "ul" ? "- " : (n.start ?? 1) + i + ". ");
|
|
47
|
+
}
|
|
48
|
+
else if (i && (n.kind === "document" || n.kind === "blockquote"))
|
|
49
|
+
add("\n\n");
|
|
50
|
+
else if (i &&
|
|
51
|
+
n.kind === "li" &&
|
|
52
|
+
["ul", "ol", "p", "pre", "blockquote", "table", "hr"].includes(child.kind))
|
|
53
|
+
add("\n");
|
|
54
|
+
visit(child, inPre || n.kind === "pre");
|
|
55
|
+
});
|
|
56
|
+
const format = {
|
|
57
|
+
strong: "bold",
|
|
58
|
+
em: "italic",
|
|
59
|
+
del: "strikethrough",
|
|
60
|
+
pre: "pre",
|
|
61
|
+
code: "code",
|
|
62
|
+
a: "text_link",
|
|
63
|
+
h1: "bold",
|
|
64
|
+
h2: "bold",
|
|
65
|
+
h3: "bold",
|
|
66
|
+
h4: "bold",
|
|
67
|
+
h5: "bold",
|
|
68
|
+
h6: "bold",
|
|
69
|
+
};
|
|
70
|
+
const kind = format[n.kind];
|
|
71
|
+
if (kind && !inPre && length > start)
|
|
72
|
+
entities.push({
|
|
73
|
+
type: kind,
|
|
74
|
+
offset: start,
|
|
75
|
+
length: length - start,
|
|
76
|
+
...(n.url ? { url: n.url } : {}),
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
visit(root);
|
|
80
|
+
entities.sort((a, b) => a.offset - b.offset || b.length - a.length);
|
|
81
|
+
const seen = new Set();
|
|
82
|
+
return {
|
|
83
|
+
text: pieces.join(""),
|
|
84
|
+
entities: entities.filter((e) => {
|
|
85
|
+
const key = JSON.stringify(e);
|
|
86
|
+
if (seen.has(key))
|
|
87
|
+
return false;
|
|
88
|
+
seen.add(key);
|
|
89
|
+
return true;
|
|
90
|
+
}),
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
/** Each chunk is bounded in UTF-16 units and ends at a Unicode scalar boundary. */
|
|
94
|
+
export function chunks(text, limit) {
|
|
95
|
+
const result = [];
|
|
96
|
+
let start = 0;
|
|
97
|
+
while (start < text.length) {
|
|
98
|
+
let end = Math.min(text.length, start + limit);
|
|
99
|
+
if (end < text.length) {
|
|
100
|
+
if (/[\uDC00-\uDFFF]/.test(text[end]))
|
|
101
|
+
end--;
|
|
102
|
+
const preferred = text.lastIndexOf("\n", end - 1);
|
|
103
|
+
if (preferred >= start + limit / 2)
|
|
104
|
+
end = preferred + 1;
|
|
105
|
+
}
|
|
106
|
+
result.push({ start, text: text.slice(start, end) });
|
|
107
|
+
start = end;
|
|
108
|
+
}
|
|
109
|
+
return result;
|
|
110
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export interface DocumentNode {
|
|
2
|
+
kind: string;
|
|
3
|
+
text?: string;
|
|
4
|
+
url?: string;
|
|
5
|
+
start?: number;
|
|
6
|
+
children: DocumentNode[];
|
|
7
|
+
}
|
|
8
|
+
export interface Fragment {
|
|
9
|
+
text: string;
|
|
10
|
+
nodes: DocumentNode[];
|
|
11
|
+
htmlSize: number;
|
|
12
|
+
}
|
|
13
|
+
export declare function html(nodes: DocumentNode[]): string;
|
|
14
|
+
export declare function literal(text: string): Fragment;
|
|
15
|
+
export declare function wrap(kind: string, part: Fragment, attributes?: Pick<DocumentNode, "url" | "start">): Fragment;
|
|
16
|
+
export declare function join(parts: Fragment[], sep?: string): Fragment;
|
|
17
|
+
export declare function contains(nodes: DocumentNode[], kind: string): boolean;
|
package/dist/fragment.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
const escape = (s) => s
|
|
2
|
+
.replace(/&/g, "&")
|
|
3
|
+
.replace(/</g, "<")
|
|
4
|
+
.replace(/>/g, ">")
|
|
5
|
+
.replace(/"/g, """);
|
|
6
|
+
function opening(n) {
|
|
7
|
+
const attr = n.kind === "a"
|
|
8
|
+
? ' href="' + escape(n.url) + '"'
|
|
9
|
+
: n.kind === "ol" && n.start !== 1
|
|
10
|
+
? ' start="' + n.start + '"'
|
|
11
|
+
: "";
|
|
12
|
+
return "<" + n.kind + attr + ">";
|
|
13
|
+
}
|
|
14
|
+
const voidElement = (kind) => kind === "br" || kind === "hr";
|
|
15
|
+
export function html(nodes) {
|
|
16
|
+
return nodes
|
|
17
|
+
.map((n) => n.kind === "text"
|
|
18
|
+
? escape(n.text)
|
|
19
|
+
: opening(n) +
|
|
20
|
+
(voidElement(n.kind) ? "" : html(n.children) + "</" + n.kind + ">"))
|
|
21
|
+
.join("");
|
|
22
|
+
}
|
|
23
|
+
export function literal(text) {
|
|
24
|
+
return {
|
|
25
|
+
text,
|
|
26
|
+
nodes: text ? [{ kind: "text", text, children: [] }] : [],
|
|
27
|
+
htmlSize: escape(text).length,
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
export function wrap(kind, part, attributes = {}) {
|
|
31
|
+
const node = { kind, ...attributes, children: part.nodes };
|
|
32
|
+
return {
|
|
33
|
+
text: part.text,
|
|
34
|
+
nodes: [node],
|
|
35
|
+
htmlSize: part.htmlSize +
|
|
36
|
+
opening(node).length +
|
|
37
|
+
(voidElement(kind) ? 0 : kind.length + 3),
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
export function join(parts, sep = "") {
|
|
41
|
+
const nodes = [];
|
|
42
|
+
let htmlSize = 0;
|
|
43
|
+
for (const part of parts) {
|
|
44
|
+
htmlSize += part.htmlSize;
|
|
45
|
+
for (const node of part.nodes) {
|
|
46
|
+
const last = nodes[nodes.length - 1];
|
|
47
|
+
if (node.kind === "text" && last?.kind === "text")
|
|
48
|
+
nodes[nodes.length - 1] = { ...last, text: last.text + node.text };
|
|
49
|
+
else
|
|
50
|
+
nodes.push(node);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return { text: parts.map((p) => p.text).join(sep), nodes, htmlSize };
|
|
54
|
+
}
|
|
55
|
+
export function contains(nodes, kind) {
|
|
56
|
+
return nodes.some((n) => n.kind === kind || contains(n.children, kind));
|
|
57
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { Target, ConversionError, type Diagnostic, type DiagnosticCode, type Options, type Policy, type Result, type ErrorCode, } from "./api.js";
|
|
2
|
+
export { convert, parseDocument, type Document } from "./render.js";
|
|
3
|
+
export type { DocumentNode } from "./fragment.js";
|
|
4
|
+
export { toMessages, type ApiTarget, type MessageBodies, type MessageBatch, type TelegramBody, type DiscordBody, type SlackBody, type Entity, } from "./messages.js";
|
|
5
|
+
export { diagnostics } from "./diagnostics.js";
|
package/dist/index.js
ADDED
package/dist/links.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export declare function normalizeLink(url: string): string | undefined;
|
|
2
|
+
/** Preserve an authored Latin hostname for readers, after the destination
|
|
3
|
+
* has passed IDNA and URL validation. Other scripts use the ASCII URL. */
|
|
4
|
+
export declare function displayLink(authored: string, destination: string): string;
|
package/dist/links.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
const absoluteLink = new RegExp("^(?:https?://[A-Za-z0-9-]+(?:\\.[A-Za-z0-9-]+)*(?::[0-9]{1,5})?(?:[/?#][^\\s]*)?|mailto:[A-Za-z0-9.!#$%&'*+=?^_{}~-]+@[A-Za-z0-9-]+(?:\\.[A-Za-z0-9-]+)*(?:\\?[^\\s]*)?)$", "i");
|
|
2
|
+
// Normalize only the hostname. URL serialization of the full destination would
|
|
3
|
+
// alter authored path escapes, casing, query parameters and default ports.
|
|
4
|
+
export function normalizeLink(url) {
|
|
5
|
+
if (/[\\<>"\s\u0000-\u001f\u007f-\u009f]/u.test(url) ||
|
|
6
|
+
/%(?![a-f0-9]{2})/i.test(url))
|
|
7
|
+
return undefined;
|
|
8
|
+
const webHost = /^(https?:\/\/)([^/:?#]+)(.*)$/i.exec(url);
|
|
9
|
+
if (webHost && /[^\x00-\x7f]/.test(webHost[2])) {
|
|
10
|
+
if (!/^[\p{L}\p{N}\p{M}.\-]+$/u.test(webHost[2]))
|
|
11
|
+
return undefined;
|
|
12
|
+
if (webHost[2]
|
|
13
|
+
.split(".")
|
|
14
|
+
.some((label) => label.startsWith("-") || label.endsWith("-")))
|
|
15
|
+
return undefined;
|
|
16
|
+
try {
|
|
17
|
+
// The suffix keeps numeric labels out of the URL parser's IPv4 rules.
|
|
18
|
+
const suffix = ".example";
|
|
19
|
+
const ascii = new URL("http://" + webHost[2] + suffix).hostname.slice(0, -suffix.length);
|
|
20
|
+
if (ascii.length > 253 ||
|
|
21
|
+
ascii
|
|
22
|
+
.split(".")
|
|
23
|
+
.some((label) => !label.length ||
|
|
24
|
+
label.length > 63 ||
|
|
25
|
+
label.startsWith("-") ||
|
|
26
|
+
label.endsWith("-")))
|
|
27
|
+
return undefined;
|
|
28
|
+
url = webHost[1] + ascii + webHost[3];
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
return undefined;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return absoluteLink.test(url) ? url : undefined;
|
|
35
|
+
}
|
|
36
|
+
/** Preserve an authored Latin hostname for readers, after the destination
|
|
37
|
+
* has passed IDNA and URL validation. Other scripts use the ASCII URL. */
|
|
38
|
+
export function displayLink(authored, destination) {
|
|
39
|
+
const match = /^(https?:\/\/)([^/:?#]+)(.*)$/i.exec(authored);
|
|
40
|
+
if (!match || !/[^\x00-\x7f]/.test(match[2]))
|
|
41
|
+
return destination;
|
|
42
|
+
const host = match[2].normalize("NFC");
|
|
43
|
+
for (const c of host) {
|
|
44
|
+
if (/^[A-Za-z0-9.-]$/.test(c))
|
|
45
|
+
continue;
|
|
46
|
+
if (!/^[\u00c0-\u024f\u1e00-\u1eff]$/u.test(c) || !/^\p{L}$/u.test(c))
|
|
47
|
+
return destination;
|
|
48
|
+
}
|
|
49
|
+
return match[1] + host + match[3];
|
|
50
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { type Entity } from "./flatten.js";
|
|
2
|
+
export type { Entity } from "./flatten.js";
|
|
3
|
+
import { type SlackBody } from "./slack.js";
|
|
4
|
+
export type { SlackBody } from "./slack.js";
|
|
5
|
+
import { type Options, type Diagnostic } from "./api.js";
|
|
6
|
+
export interface TelegramBody {
|
|
7
|
+
text: string;
|
|
8
|
+
entities: Entity[];
|
|
9
|
+
}
|
|
10
|
+
export interface DiscordBody {
|
|
11
|
+
content: string;
|
|
12
|
+
allowed_mentions: {
|
|
13
|
+
parse: never[];
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
export type SingleTarget = "whatsapp" | "threema" | "viber" | "teams" | "google-chat";
|
|
17
|
+
export type ApiTarget = "telegram" | "slack" | "discord" | SingleTarget;
|
|
18
|
+
export interface MessageBodies {
|
|
19
|
+
telegram: TelegramBody;
|
|
20
|
+
slack: SlackBody;
|
|
21
|
+
discord: DiscordBody;
|
|
22
|
+
whatsapp: {
|
|
23
|
+
messaging_product: "whatsapp";
|
|
24
|
+
type: "text";
|
|
25
|
+
text: {
|
|
26
|
+
body: string;
|
|
27
|
+
preview_url: false;
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
threema: {
|
|
31
|
+
text: string;
|
|
32
|
+
};
|
|
33
|
+
viber: {
|
|
34
|
+
type: "text";
|
|
35
|
+
text: string;
|
|
36
|
+
};
|
|
37
|
+
teams: {
|
|
38
|
+
body: {
|
|
39
|
+
contentType: "html";
|
|
40
|
+
content: string;
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
"google-chat": {
|
|
44
|
+
text: string;
|
|
45
|
+
markupSyntax: "MARKUP_SYNTAX_CHAT";
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
export interface MessageBatch<T> {
|
|
49
|
+
messages: T[];
|
|
50
|
+
diagnostics: Diagnostic[];
|
|
51
|
+
}
|
|
52
|
+
/** Build JSON-ready bodies for a receiving platform without sending a message. */
|
|
53
|
+
export declare function toMessages<T extends ApiTarget>(markdown: string, target: T, options?: Options): MessageBatch<MessageBodies[T]>;
|