mdto-messages 0.1.0 → 0.2.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/README.md CHANGED
@@ -1,22 +1,40 @@
1
1
  # mdto-messages
2
2
 
3
- [![npm](https://img.shields.io/npm/v/mdto-messages)](https://www.npmjs.com/package/mdto-messages)
3
+ [![npm version](https://img.shields.io/npm/v/mdto-messages)](https://www.npmjs.com/package/mdto-messages)
4
4
 
5
- Render Markdown as a messaging draft, HTML email fragment or provider API body. Conversion runs locally in Node and modern browsers.
5
+ Convert LLM Markdown to WhatsApp drafts, Telegram entities, Slack Block Kit, Discord messages, Teams and email HTML. One input can serve a chat composer, a bot or an email template. The TypeScript package runs offline in Node.js 20.19+ and in browser bundles.
6
6
 
7
7
  ```sh
8
8
  npm install mdto-messages
9
9
  ```
10
10
 
11
+ | Destination | Output for `**Ship** today` |
12
+ |---|---|
13
+ | WhatsApp / Google Chat draft | `*Ship* today` |
14
+ | Email HTML fragment | `<p><strong>Ship</strong> today</p>` |
15
+ | Telegram Bot API | Text `Ship today` and a bold entity at offset 0, length 4 |
16
+ | Plain text | `Ship today` |
17
+
18
+ ## Choose an operation
19
+
20
+ Use `convert(markdown, target)` for a **draft**. Targets are `plaintext`, `html`, `email`, `whatsapp`, `threema`, `signal`, `viber`, `google-chat`, `discord`, `telegram`, `teams` and `slack`. For email, use both the returned `text` and `html` to build a multipart message.
21
+
22
+ Use `toMessages(markdown, target)` for ordered **message bodies**. Supported targets are Telegram, Slack, Discord, WhatsApp, Google Chat, Teams, Viber and Threema. Your application supplies recipients, credentials and delivery. Long Telegram, Slack and Discord responses can span several bodies.
23
+
11
24
  ```js
12
25
  import { convert, toMessages } from "mdto-messages";
13
26
 
14
27
  const draft = convert("**Ship** today", "whatsapp");
15
28
  console.log(draft.text); // *Ship* today
16
29
 
17
- const { messages, diagnostics } = toMessages("**Ship** today", "telegram");
18
- console.log(messages[0].entities); // [{ type: "bold", offset: 0, length: 4 }]
30
+ const telegram = toMessages("😀 **Ship**", "telegram");
31
+ console.log(telegram.messages[0].entities);
32
+ // [{ type: "bold", offset: 3, length: 4 }]
33
+
34
+ const slack = toMessages("- Review\n- Ship", "slack");
35
+ console.log(slack.messages[0].blocks[0].type); // rich_text
19
36
  ```
20
37
 
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)
38
+ The default readable policy returns diagnostics when a target loses formatting. Use the strict policy when your application must reject such input. Each call accepts a bounded Markdown string, and clipboard access stays in the calling application.
39
+
40
+ [API reference](https://github.com/jmeinke/mdto-messages/blob/main/docs/api.md) · [Platform guides](https://github.com/jmeinke/mdto-messages/blob/main/docs/how-to.md) · [Changelog](https://github.com/jmeinke/mdto-messages/blob/main/CHANGELOG.md)
package/dist/flatten.d.ts CHANGED
@@ -11,6 +11,8 @@ interface Run {
11
11
  entities: Entity[];
12
12
  }
13
13
  export declare function flatten(root: DocumentNode): Run;
14
+ /** The exclusive end of the next UTF-16 chunk. A full scalar must fit. */
15
+ export declare function chunkEnd(text: string, start: number, limit: number): number;
14
16
  /** Each chunk is bounded in UTF-16 units and ends at a Unicode scalar boundary. */
15
17
  export declare function chunks(text: string, limit: number): {
16
18
  start: number;
package/dist/flatten.js CHANGED
@@ -90,19 +90,26 @@ export function flatten(root) {
90
90
  }),
91
91
  };
92
92
  }
93
+ /** The exclusive end of the next UTF-16 chunk. A full scalar must fit. */
94
+ export function chunkEnd(text, start, limit) {
95
+ if (limit <= 0)
96
+ return start;
97
+ let end = Math.min(text.length, start + limit);
98
+ if (end < text.length) {
99
+ if (/[\uDC00-\uDFFF]/.test(text[end]))
100
+ end--;
101
+ const preferred = text.lastIndexOf("\n", end - 1);
102
+ if (preferred >= start + limit / 2)
103
+ end = preferred + 1;
104
+ }
105
+ return end;
106
+ }
93
107
  /** Each chunk is bounded in UTF-16 units and ends at a Unicode scalar boundary. */
94
108
  export function chunks(text, limit) {
95
109
  const result = [];
96
110
  let start = 0;
97
111
  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
- }
112
+ const end = chunkEnd(text, start, limit);
106
113
  result.push({ start, text: text.slice(start, end) });
107
114
  start = end;
108
115
  }
package/dist/index.d.ts CHANGED
@@ -1,5 +1,4 @@
1
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";
2
+ export { convert } from "./render.js";
4
3
  export { toMessages, type ApiTarget, type MessageBodies, type MessageBatch, type TelegramBody, type DiscordBody, type SlackBody, type Entity, } from "./messages.js";
5
4
  export { diagnostics } from "./diagnostics.js";
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
1
  export { Target, ConversionError, } from "./api.js";
2
- export { convert, parseDocument } from "./render.js";
2
+ export { convert } from "./render.js";
3
3
  export { toMessages, } from "./messages.js";
4
4
  export { diagnostics } from "./diagnostics.js";
package/dist/slack.d.ts CHANGED
@@ -25,6 +25,8 @@ type Block = Section | List | {
25
25
  elements: SlackElement[];
26
26
  };
27
27
  export interface SlackBody {
28
+ text: string;
29
+ mrkdwn: false;
28
30
  blocks: {
29
31
  type: "rich_text";
30
32
  elements: Block[];
package/dist/slack.js CHANGED
@@ -1,4 +1,4 @@
1
- import { chunks, flatten } from "./flatten.js";
1
+ import { chunkEnd, flatten } from "./flatten.js";
2
2
  export function slackElements(text, offset, entities) {
3
3
  const relevant = entities.filter((e) => e.offset < offset + text.length && e.offset + e.length > offset);
4
4
  const boundaries = new Set([0, text.length]);
@@ -82,6 +82,23 @@ function listBlocks(node, indent) {
82
82
  return blocks;
83
83
  }
84
84
  const SLACK_LIMIT = 3000;
85
+ // Leave room under Slack’s recommended 4,000 characters for multipart labels.
86
+ const FALLBACK_LIMIT = 3900;
87
+ const fallbackEscapes = {
88
+ "&": "&amp;",
89
+ "<": "&lt;",
90
+ ">": "&gt;",
91
+ };
92
+ function fallbackText(text) {
93
+ let encoded = "";
94
+ for (const scalar of text) {
95
+ const escaped = fallbackEscapes[scalar] ?? scalar;
96
+ if (encoded.length + escaped.length > FALLBACK_LIMIT)
97
+ return encoded + "…";
98
+ encoded += escaped;
99
+ }
100
+ return encoded;
101
+ }
85
102
  function hasNativeStructure(node) {
86
103
  return (isList(node) ||
87
104
  node.kind === "pre" ||
@@ -101,18 +118,26 @@ function section(run, type, start = 0, text = run.text) {
101
118
  export function renderSlack(root) {
102
119
  const messages = [];
103
120
  let elements = [];
104
- let used = 0;
121
+ let fallback = "";
105
122
  let simplified = false;
106
123
  function flush() {
107
- if (elements.length)
108
- messages.push({ blocks: [{ type: "rich_text", elements }] });
124
+ if (elements.length) {
125
+ messages.push({
126
+ text: fallbackText(fallback),
127
+ mrkdwn: false,
128
+ blocks: [{ type: "rich_text", elements }],
129
+ });
130
+ }
109
131
  elements = [];
110
- used = 0;
132
+ fallback = "";
133
+ }
134
+ function available() {
135
+ return SLACK_LIMIT - fallback.length - (elements.length ? 2 : 0);
111
136
  }
112
- function append(blocks, length) {
113
- if (elements.length && used + 2 + length > SLACK_LIMIT)
137
+ function append(blocks, text) {
138
+ if (text.length > available())
114
139
  flush();
115
- used += (elements.length ? 2 : 0) + length;
140
+ fallback += (elements.length ? "\n\n" : "") + text;
116
141
  elements.push(...blocks);
117
142
  }
118
143
  function addBlock(node, run) {
@@ -120,7 +145,7 @@ export function renderSlack(root) {
120
145
  if (isList(node)) {
121
146
  const blocks = run.text.length <= SLACK_LIMIT && listBlocks(node, 0);
122
147
  if (blocks && blocks.length) {
123
- append(blocks, run.text.length);
148
+ append(blocks, run.text);
124
149
  return;
125
150
  }
126
151
  simplified = true;
@@ -144,18 +169,23 @@ export function renderSlack(root) {
144
169
  if (!run.text.length)
145
170
  return;
146
171
  if (run.text.length <= SLACK_LIMIT) {
147
- append([section(run, type)], run.text.length);
172
+ append([section(run, type)], run.text);
148
173
  return;
149
174
  }
150
- flush();
151
- for (const chunk of chunks(run.text, SLACK_LIMIT)) {
152
- append([section(run, type, chunk.start, chunk.text)], chunk.text.length);
153
- flush();
175
+ let start = 0;
176
+ while (start < run.text.length) {
177
+ const end = chunkEnd(run.text, start, available());
178
+ if (end > start) {
179
+ const text = run.text.slice(start, end);
180
+ append([section(run, type, start, text)], text);
181
+ start = end;
182
+ }
183
+ if (start < run.text.length)
184
+ flush();
154
185
  }
155
186
  }
156
- // Start a new body at an item boundary and preserve ordered list offsets.
187
+ // Keep whole list items together and preserve their original numbering.
157
188
  function longList(node) {
158
- flush();
159
189
  const slice = (from, to) => ({
160
190
  ...node,
161
191
  start: (node.start ?? 1) + from,
@@ -165,14 +195,15 @@ export function renderSlack(root) {
165
195
  function emit(end) {
166
196
  const part = slice(start, end);
167
197
  addBlock(part, flatten(part));
168
- flush();
169
198
  start = end;
170
199
  length = 0;
171
200
  }
172
201
  for (let i = 0; i < node.children.length; i++) {
173
202
  const itemLength = flatten(slice(i, i + 1)).text.length;
174
- if (i > start && length + 1 + itemLength > SLACK_LIMIT)
203
+ if (i > start && length + 1 + itemLength > available())
175
204
  emit(i);
205
+ if (i === start && itemLength <= SLACK_LIMIT && itemLength > available())
206
+ flush();
176
207
  length += (i > start ? 1 : 0) + itemLength;
177
208
  if (itemLength > SLACK_LIMIT)
178
209
  emit(i + 1);
@@ -188,5 +219,9 @@ export function renderSlack(root) {
188
219
  addBlock(node, run);
189
220
  }
190
221
  flush();
222
+ if (messages.length > 1) {
223
+ for (const [index, body] of messages.entries())
224
+ body.text += ` (${index + 1}/${messages.length})`;
225
+ }
191
226
  return { messages, simplified };
192
227
  }
package/package.json CHANGED
@@ -1,15 +1,11 @@
1
1
  {
2
2
  "name": "mdto-messages",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {
7
7
  "types": "./dist/index.d.ts",
8
8
  "import": "./dist/index.js"
9
- },
10
- "./buffer": {
11
- "types": "./dist/buffer.d.ts",
12
- "import": "./dist/buffer.js"
13
9
  }
14
10
  },
15
11
  "types": "./dist/index.d.ts",
@@ -21,7 +17,7 @@
21
17
  "docs/licenses/entities.txt"
22
18
  ],
23
19
  "scripts": {
24
- "build": "tsc -p tsconfig.json",
20
+ "build": "node -e \"require('node:fs').rmSync('dist', {recursive: true, force: true})\" && tsc -p tsconfig.json",
25
21
  "test": "npm run build && node --test",
26
22
  "prepack": "npm run build",
27
23
  "build:example": "npm run build && node scripts/build-browser-example.mjs"
@@ -36,7 +32,7 @@
36
32
  "typescript": "5.9.3"
37
33
  },
38
34
  "license": "MIT",
39
- "description": "Render Markdown for messaging apps, chat APIs and email with offline TypeScript conversion.",
35
+ "description": "Convert LLM Markdown to WhatsApp, Telegram entities, Slack Block Kit, Discord, Teams and email HTML.",
40
36
  "sideEffects": false,
41
37
  "engines": {
42
38
  "node": ">=20.19.0"
@@ -46,7 +42,7 @@
46
42
  "url": "git+https://github.com/jmeinke/mdto-messages.git"
47
43
  },
48
44
  "author": "Jerome Meinke (https://github.com/jmeinke)",
49
- "homepage": "https://github.com/jmeinke/mdto-messages#readme",
45
+ "homepage": "https://mdtochat.com",
50
46
  "bugs": {
51
47
  "url": "https://github.com/jmeinke/mdto-messages/issues"
52
48
  },
@@ -58,6 +54,16 @@
58
54
  "slack",
59
55
  "whatsapp",
60
56
  "google-chat",
61
- "email"
57
+ "email",
58
+ "discord",
59
+ "microsoft-teams",
60
+ "threema",
61
+ "viber",
62
+ "signal",
63
+ "block-kit",
64
+ "telegram-bot",
65
+ "telegram-entities",
66
+ "markdown-converter",
67
+ "html-email"
62
68
  ]
63
69
  }
package/dist/buffer.d.ts DELETED
@@ -1,10 +0,0 @@
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 DELETED
@@ -1,37 +0,0 @@
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
- }