@luutuankiet/clipboard2markdown 0.2.2 โ†’ 0.4.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.
Files changed (37) hide show
  1. package/README.md +29 -2
  2. package/bin/cli.js +40 -34
  3. package/bin/diagrams.js +11 -2
  4. package/bin/excalidraw.js +118 -12
  5. package/package.json +10 -3
  6. package/src/clipboard-convert.js +71 -0
  7. package/src/diagrams/markdown.js +81 -18
  8. package/src/diagrams/retry.js +65 -0
  9. package/src/diagrams/share-link.js +6 -2
  10. package/src/excalidraw-png.js +217 -0
  11. package/vendor/excalidraw/VERSION +1 -0
  12. package/vendor/excalidraw/excalidraw.bundle.mjs +3384 -0
  13. package/vendor/excalidraw/fonts/Assistant/Assistant-Bold.woff2 +0 -0
  14. package/vendor/excalidraw/fonts/Assistant/Assistant-Medium.woff2 +0 -0
  15. package/vendor/excalidraw/fonts/Assistant/Assistant-Regular.woff2 +0 -0
  16. package/vendor/excalidraw/fonts/Assistant/Assistant-SemiBold.woff2 +0 -0
  17. package/vendor/excalidraw/fonts/Cascadia/CascadiaCode-Regular.woff2 +0 -0
  18. package/vendor/excalidraw/fonts/ComicShanns/ComicShanns-Regular-279a7b317d12eb88de06167bd672b4b4.woff2 +0 -0
  19. package/vendor/excalidraw/fonts/ComicShanns/ComicShanns-Regular-6e066e8de2ac57ea9283adb9c24d7f0c.woff2 +0 -0
  20. package/vendor/excalidraw/fonts/ComicShanns/ComicShanns-Regular-dc6a8806fa96795d7b3be5026f989a17.woff2 +0 -0
  21. package/vendor/excalidraw/fonts/ComicShanns/ComicShanns-Regular-fcb0fc02dcbee4c9846b3e2508668039.woff2 +0 -0
  22. package/vendor/excalidraw/fonts/Excalifont/Excalifont-Regular-349fac6ca4700ffec595a7150a0d1e1d.woff2 +0 -0
  23. package/vendor/excalidraw/fonts/Excalifont/Excalifont-Regular-3f2c5db56cc93c5a6873b1361d730c16.woff2 +0 -0
  24. package/vendor/excalidraw/fonts/Excalifont/Excalifont-Regular-41b173a47b57366892116a575a43e2b6.woff2 +0 -0
  25. package/vendor/excalidraw/fonts/Excalifont/Excalifont-Regular-623ccf21b21ef6b3a0d87738f77eb071.woff2 +0 -0
  26. package/vendor/excalidraw/fonts/Excalifont/Excalifont-Regular-a88b72a24fb54c9f94e3b5fdaa7481c9.woff2 +0 -0
  27. package/vendor/excalidraw/fonts/Excalifont/Excalifont-Regular-b9dcf9d2e50a1eaf42fc664b50a3fd0d.woff2 +0 -0
  28. package/vendor/excalidraw/fonts/Excalifont/Excalifont-Regular-be310b9bcd4f1a43f571c46df7809174.woff2 +0 -0
  29. package/vendor/excalidraw/fonts/Liberation/LiberationSans-Regular.woff2 +0 -0
  30. package/vendor/excalidraw/fonts/Lilita/Lilita-Regular-i7dPIFZ9Zz-WBtRtedDbYE98RXi4EwSsbg.woff2 +0 -0
  31. package/vendor/excalidraw/fonts/Lilita/Lilita-Regular-i7dPIFZ9Zz-WBtRtedDbYEF8RXi4EwQ.woff2 +0 -0
  32. package/vendor/excalidraw/fonts/Nunito/Nunito-Regular-XRXI3I6Li01BKofiOc5wtlZ2di8HDIkhdTA3j6zbXWjgevT5.woff2 +0 -0
  33. package/vendor/excalidraw/fonts/Nunito/Nunito-Regular-XRXI3I6Li01BKofiOc5wtlZ2di8HDIkhdTQ3j6zbXWjgeg.woff2 +0 -0
  34. package/vendor/excalidraw/fonts/Nunito/Nunito-Regular-XRXI3I6Li01BKofiOc5wtlZ2di8HDIkhdTk3j6zbXWjgevT5.woff2 +0 -0
  35. package/vendor/excalidraw/fonts/Nunito/Nunito-Regular-XRXI3I6Li01BKofiOc5wtlZ2di8HDIkhdTo3j6zbXWjgevT5.woff2 +0 -0
  36. package/vendor/excalidraw/fonts/Nunito/Nunito-Regular-XRXI3I6Li01BKofiOc5wtlZ2di8HDIkhdTs3j6zbXWjgevT5.woff2 +0 -0
  37. package/vendor/excalidraw/fonts/Virgil/Virgil-Regular.woff2 +0 -0
package/README.md CHANGED
@@ -24,7 +24,7 @@ Workflow platforms don't let you copy as Markdown โ€” you get rich text. But you
24
24
 
25
25
  - **๐Ÿ”„ Turndown.js Engine** โ€” Robust GFM support (tables, task lists, strikethrough)
26
26
  - **๐Ÿงน Platform Sanitizers** โ€” Pre-process quirky HTML before conversion
27
- - **๐Ÿ”’ Client-Side** โ€” Nothing you paste leaves your browser. The one exception to *any* network traffic is the diagrams mode (below), which downloads the Excalidraw scenes your pasted links point to, read-only
27
+ - **๐Ÿ”’ Client-Side** โ€” Nothing you paste leaves your machine. The only network traffic is read-only downloads of the Excalidraw scenes your links point to: on the page in the diagrams mode, and in the `c2m` clipboard command when it converts rich text to Markdown (turn that off with `--no-diagrams`)
28
28
  - **๐Ÿงญ Diagram round trip** โ€” Turn excalidraw.com share links into Mermaid that agents can read, and let agents edit a canvas by link (see below)
29
29
  - **๐Ÿงช Automated Testing** โ€” Fixture-based regression suite prevents breakage
30
30
  - **โšก Vite Build System** โ€” Hot reload dev server, optimized production builds
@@ -58,6 +58,15 @@ cat diagram.json | c2m excalidraw - # same, from stdin
58
58
 
59
59
  The Mermaid is deliberately lossy: positions, colours, fonts and arrow curves are dropped; boxes, labels, connections, line style and grouping are kept. The canvas stays the master copy.
60
60
 
61
+ **Diagrams as PNG** (`c2m excalidraw --png`, Node 20+, any OS): draws one or more diagrams locally, for pasting into a Google Doc. No browser is needed and the drawing makes no network request; the share link is uploaded as usual (skip it with `--no-upload`). Input is the JSON array above or a whole scene (`{"elements": [...]}`, such as a `.excalidraw` file).
62
+
63
+ ```bash
64
+ c2m excalidraw --png a.json b.json # PNGs in ~/.cache/c2m/renders/
65
+ c2m excalidraw --png out/ a.json # PNGs in out/
66
+ ```
67
+
68
+ Per diagram it writes `<name>.png` (scale 3, lowered until the image has at most 25,000,000 pixels, Google Docs' limit) and `<name>.review.png` (at most 1000 px on the long side), then prints an image line `![<name>](attach:<name>)`, an `[Edit this diagram](<share link>)` line and the images map entry `"<name>": "@file64:/abs/path/<name>.png"`. Review agents should read the Mermaid preview first and open the review PNG only when the preview is not enough. Each run first deletes PNGs older than 24 hours in the default cache folder; a folder you name is never cleaned. CJK text is not drawn, because the 13 MB CJK font is left out of the package; such diagrams get a warning.
69
+
61
70
  ## Demo
62
71
 
63
72
  **Live:** https://luutuankiet.github.io/clipboard2markdown/
@@ -81,7 +90,25 @@ npm install -g @luutuankiet/clipboard2markdown
81
90
  c2m --help
82
91
  ```
83
92
 
84
- With no subcommand, `c2m` reads the clipboard, converts it (HTML to Markdown, or Markdown to HTML), and writes the result back. Clipboard conversion needs macOS. `c2m excalidraw` and `c2m diagrams` run anywhere Node 20+ runs. `c2m-diagrams` still works as an alias of `c2m diagrams`.
93
+ With no subcommand, `c2m` reads the clipboard, converts it (HTML to Markdown, or Markdown to HTML), and writes the result back. Clipboard conversion needs macOS.
94
+
95
+ It handles Excalidraw share links in both directions:
96
+
97
+ - **Rich text โ†’ Markdown:** every excalidraw.com share link gets a Mermaid reading copy under it, as `c2m diagrams expand` produces. This downloads each scene read-only (one `GET` per link to `json.excalidraw.com`; the key in the link's `#` fragment is never sent). All links download at once, temporary failures are retried, and the step gives up after 6 seconds. A diagram that still cannot be fetched keeps its link with no block.
98
+ - **Markdown โ†’ HTML:** generated reading copies are removed first, so only the link reaches Slack or a chat box. Mermaid you wrote yourself is kept. No network.
99
+ - `--no-diagrams` skips both steps.
100
+
101
+ stdout stays empty on a clean run and gets one line when something needs attention, for example `c2m: 2 of 3 diagrams skipped (timeout, 404)`, or the empty-clipboard and conversion-failed messages. A keyboard macro can notify only when stdout is not empty. Exit codes: `0` converted (also when diagrams were skipped), `1` empty clipboard, `2` conversion failed.
102
+
103
+ For a macro, a global install run by absolute path skips npx's registry lookup (about 1.7s per run):
104
+
105
+ ```bash
106
+ npm i -g @luutuankiet/clipboard2markdown@latest
107
+ # prints the line to put in the macro; launchers often don't load your PATH
108
+ echo "$(command -v node) $(npm root -g)/@luutuankiet/clipboard2markdown/bin/cli.js 2>/dev/null"
109
+ ```
110
+
111
+ A global install belongs to one Node version: after switching or upgrading Node (nvm), reinstall and update the macro's path. `c2m excalidraw` and `c2m diagrams` run anywhere Node 20+ runs. `c2m-diagrams` still works as an alias of `c2m diagrams`.
85
112
 
86
113
  ## Development
87
114
 
package/bin/cli.js CHANGED
@@ -9,11 +9,19 @@ const HELP = `clipboard2markdown (short name: c2m) - clipboard HTML <-> Markdown
9
9
  and Excalidraw diagrams for agents
10
10
 
11
11
  Usage:
12
- c2m HTML on the clipboard -> Markdown, plain text -> HTML
13
- c2m --to-md force HTML -> Markdown
14
- c2m --to-html force Markdown -> HTML
15
- c2m --help show this help
16
- c2m --version print the version
12
+ c2m HTML on the clipboard -> Markdown, plain text -> HTML
13
+ c2m --to-md force HTML -> Markdown
14
+ c2m --to-html force Markdown -> HTML
15
+ c2m --no-diagrams skip the Excalidraw step below (no network at all)
16
+ c2m --help show this help
17
+ c2m --version print the version
18
+
19
+ Excalidraw share links: HTML -> Markdown adds a Mermaid reading copy under
20
+ every excalidraw.com link, downloading each scene read-only (one GET per link
21
+ to json.excalidraw.com; the key in the link's # fragment is never sent).
22
+ Markdown -> HTML removes generated reading copies first, so only the link is
23
+ pasted; Mermaid you wrote yourself is kept. A diagram that cannot be fetched
24
+ in a few seconds is skipped and its link left as written.
17
25
 
18
26
  c2m excalidraw <file|-> JSON diagram -> excalidraw.com share link and
19
27
  Mermaid preview (see c2m excalidraw --help)
@@ -26,6 +34,8 @@ Every command also runs without installing:
26
34
  Clipboard conversion needs macOS (it uses osascript); excalidraw and
27
35
  diagrams run anywhere Node 20+ runs.
28
36
  Exit codes: 0 converted, 1 empty clipboard, 2 conversion failed.
37
+ stdout is empty on a clean clipboard run; it gets one line when something
38
+ needs attention (skipped diagrams, empty clipboard, failed conversion).
29
39
  `;
30
40
 
31
41
  // Converters need jsdom's DOM globals; loaded lazily so commands that
@@ -115,20 +125,9 @@ pb.setStringForType(str, "public.utf8-plain-text");
115
125
  }
116
126
  }
117
127
 
118
- // --- core logic (exported for testing) ---
119
-
120
- /**
121
- * Determine conversion direction.
122
- * @param {'html'|'text'} clipboardType - what the pasteboard held
123
- * @param {boolean} forceToMd - --to-md flag was passed
124
- * @param {boolean} forceToHtml - --to-html flag was passed
125
- * @returns {'to-md'|'to-html'}
126
- */
127
- export function detectDirection(clipboardType, forceToMd, forceToHtml) {
128
- if (forceToMd) return 'to-md';
129
- if (forceToHtml) return 'to-html';
130
- return clipboardType === 'html' ? 'to-md' : 'to-html';
131
- }
128
+ // --- core logic lives in src/clipboard-convert.js; re-exported for tests ---
129
+
130
+ export { detectDirection } from '../src/clipboard-convert.js';
132
131
 
133
132
  // --- entry point (only runs when executed directly) ---
134
133
 
@@ -173,29 +172,36 @@ if (isEntryPoint()) {
173
172
  process.exit(2);
174
173
  }
175
174
 
176
- const forceToMd = args.includes('--to-md');
177
- const forceToHtml = args.includes('--to-html');
175
+ // stdout carries one line only when something needs attention, so a macro
176
+ // can notify on "stdout not empty"; stderr keeps the diagnostics
177
+ const notify = (line) => {
178
+ console.error(line);
179
+ process.stdout.write(line + '\n');
180
+ };
178
181
 
179
182
  try {
180
- const { convert, convertMdToHtml } = await loadConverters();
183
+ const { convertClipboard } = await import('../src/clipboard-convert.js');
184
+ const converters = await loadConverters();
181
185
  const clipboard = readClipboard();
182
186
  if (!clipboard.content || !clipboard.content.trim()) {
183
- console.error('c2m: empty clipboard');
187
+ notify('c2m: empty clipboard');
184
188
  process.exit(1);
185
189
  }
186
- const direction = detectDirection(clipboard.type, forceToMd, forceToHtml);
187
- if (direction === 'to-md') {
188
- const md = convert(clipboard.content);
189
- console.error(`c2m: ${clipboard.type} โ†’ md (${md.length} chars)`);
190
- writeTextToClipboard(md);
191
- } else {
192
- const html = convertMdToHtml(clipboard.content);
193
- console.error(`c2m: ${clipboard.type} โ†’ html (${html.length} chars)`);
194
- writeHtmlToClipboard(html);
195
- }
190
+ const result = await convertClipboard({
191
+ content: clipboard.content,
192
+ type: clipboard.type,
193
+ forceToMd: args.includes('--to-md'),
194
+ forceToHtml: args.includes('--to-html'),
195
+ diagrams: !args.includes('--no-diagrams'),
196
+ converters,
197
+ });
198
+ console.error(`c2m: ${clipboard.type} โ†’ ${result.kind} (${result.output.length} chars)`);
199
+ if (result.kind === 'md') writeTextToClipboard(result.output);
200
+ else writeHtmlToClipboard(result.output);
201
+ if (result.notice) notify(result.notice);
196
202
  process.exit(0);
197
203
  } catch (e) {
198
- console.error(`c2m: ${e.message}`);
204
+ notify(`c2m: ${e.message}`);
199
205
  process.exit(2);
200
206
  }
201
207
  }
package/bin/diagrams.js CHANGED
@@ -13,6 +13,7 @@ import {
13
13
  expandDiagramLinks, stripDiagramBlocks, loadScene, sceneToMermaid, editLink,
14
14
  parseShareLink, normalizeScene,
15
15
  } from '../src/diagrams/index.js';
16
+ import { retryingFetch } from '../src/diagrams/retry.js';
16
17
 
17
18
  const USAGE = `usage: c2m diagrams <command> [options]
18
19
 
@@ -30,7 +31,8 @@ ops: [{"op":"rename","node","label"}, {"op":"add","id","label","near","side"?},
30
31
  `;
31
32
 
32
33
  // Test hook: a directory standing in for json.excalidraw.com. GET reads
33
- // <dir>/<id>, POST writes a new file. Never set this outside tests.
34
+ // <dir>/<id>, POST writes a new file. A file <dir>/<id>.fail holding a status
35
+ // answers one GET with that status and is consumed. Never set this outside tests.
34
36
  function fileBackendFetch(dir) {
35
37
  return async function (url, init) {
36
38
  var method = ((init && init.method) || 'GET').toUpperCase();
@@ -48,6 +50,11 @@ function fileBackendFetch(dir) {
48
50
  return respond(200, Buffer.from(JSON.stringify({ id: id })));
49
51
  }
50
52
  var file = path.join(dir, url.split('/').pop());
53
+ if (fs.existsSync(file + '.fail')) {
54
+ var status = Number(fs.readFileSync(file + '.fail', 'utf8'));
55
+ fs.unlinkSync(file + '.fail');
56
+ return respond(status, Buffer.from('{}'));
57
+ }
51
58
  if (!fs.existsSync(file)) return respond(404, Buffer.from('{}'));
52
59
  return respond(200, fs.readFileSync(file));
53
60
  };
@@ -93,8 +100,10 @@ export async function main(argv, io) {
93
100
  out = stripDiagramBlocks(md);
94
101
  } else {
95
102
  var report = {};
103
+ var retrying = retryingFetch(fetchImpl);
96
104
  out = await expandDiagramLinks(md, {
97
- loadScene: function (url) { return loadScene(url, { fetch: fetchImpl }); },
105
+ loadScene: function (url) { return loadScene(url, { fetch: retrying }); },
106
+ parallel: true,
98
107
  onProgress: function (p) { if (p.total) stderr.write('expanding ' + p.done + ' of ' + p.total + ' diagrams\n'); },
99
108
  report: report,
100
109
  });
package/bin/excalidraw.js CHANGED
@@ -4,7 +4,8 @@
4
4
  // The help text below is the agent's whole manual for this command; keep it in
5
5
  // step with src/diagrams/skeleton.js.
6
6
 
7
- import { readFileSync } from 'fs';
7
+ import { mkdirSync, readFileSync, writeFileSync } from 'fs';
8
+ import { basename, resolve } from 'path';
8
9
  import { buildSkeleton } from '../src/diagrams/skeleton.js';
9
10
  import { saveScene } from '../src/diagrams/share-link.js';
10
11
 
@@ -14,6 +15,8 @@ Usage:
14
15
  c2m excalidraw <file.json> read the diagram from a file
15
16
  c2m excalidraw - read the diagram from stdin
16
17
  c2m excalidraw --no-upload ... check only: print preview and warnings, no link
18
+ c2m excalidraw --png [<dir>] <file.json>...
19
+ also draw each diagram as PNG (see PNG below)
17
20
  c2m excalidraw --help show this help
18
21
 
19
22
  OUTPUT (stdout, in this order)
@@ -103,6 +106,30 @@ WARNINGS
103
106
  dropped unsupported type, or an MCP-only command such as
104
107
  cameraUpdate, delete, restoreCheckpoint
105
108
 
109
+ PNG (for pasting into a Google Doc)
110
+ c2m excalidraw --png [<dir>] a.json b.json ...
111
+ Draws each diagram locally (no browser; the drawing itself makes no network
112
+ request) and uploads it for its share link as above. Each input is either
113
+ the element array described here or a whole scene ({"elements": [...]},
114
+ e.g. a .excalidraw file). For each input <name>.json it writes:
115
+ <name>.png for the document: scale 3, lowered until the image
116
+ has at most 25,000,000 pixels (Google Docs' limit)
117
+ <name>.review.png for a quick look: at most 1000 px on the long side
118
+ <dir> defaults to ~/.cache/c2m/renders/ ($XDG_CACHE_HOME/c2m/renders/);
119
+ each run first deletes PNGs there older than 24 h. A <dir> you name is
120
+ never cleaned. --png=<dir> also works.
121
+ Then, per diagram:
122
+ ![<name>](attach:<name>) the image line for the document
123
+ [Edit this diagram](<share link>) omitted with --no-upload
124
+ "<name>": "@file64:/abs/path/<name>.png" the entry for the images map
125
+ review: /abs/path/<name>.review.png
126
+ warning: ... as above, if any
127
+ To check a drawing, read its Mermaid preview first (rerun without --png, or
128
+ c2m diagrams show <file> for a whole scene); open the review PNG only when
129
+ the preview cannot answer the question.
130
+ Text uses Excalidraw's fonts; CJK text is not drawn (the CJK font is not
131
+ shipped) and gets a warning.
132
+
106
133
  NOT SUPPORTED
107
134
  images, frames, freedraw, Mermaid as input, editing an existing link (use
108
135
  c2m diagrams edit for that), writing .excalidraw files.
@@ -129,32 +156,59 @@ async function upload(scene, fetchImpl) {
129
156
  }
130
157
  }
131
158
 
132
- // Returns the exit code. `io.fetch` and `io.stdin` exist for tests.
159
+ // A whole scene ({elements: [...]}, as in a .excalidraw file) is drawn as is;
160
+ // anything else goes through the skeleton builder.
161
+ function toScene(input) {
162
+ if (input && !Array.isArray(input) && Array.isArray(input.elements)) {
163
+ return { scene: input, preview: null, warnings: [] };
164
+ }
165
+ return buildSkeleton(input);
166
+ }
167
+
168
+ function parseJson(text) {
169
+ try {
170
+ return JSON.parse(text);
171
+ } catch (e) {
172
+ throw new Error('input is not valid JSON: ' + e.message);
173
+ }
174
+ }
175
+
176
+ function sceneName(target) {
177
+ return target === '-' ? 'diagram' : basename(target).replace(/\.(json|excalidraw)$/i, '');
178
+ }
179
+
180
+ // `--png` takes an optional folder: the next argument is the folder unless it
181
+ // looks like an input (-, *.json, *.excalidraw) or an option.
182
+ function looksLikeInput(a) {
183
+ return a === '-' || a.startsWith('--') || /\.(json|excalidraw)$/i.test(a);
184
+ }
185
+
186
+ // Returns the exit code. `io.fetch`, `io.stdin` and `io.env` exist for tests.
133
187
  export async function runExcalidraw(argv, io) {
134
188
  var stdout = io.stdout, stderr = io.stderr;
135
189
  var noUpload = false;
190
+ var png = false, pngDir = null;
136
191
  var targets = [];
137
192
  for (var i = 0; i < argv.length; i++) {
138
193
  var a = argv[i];
139
194
  if (a === '--help' || a === '-h') { stdout.write(EXCALIDRAW_HELP); return 0; }
140
195
  if (a === '--no-upload') noUpload = true;
196
+ else if (a === '--png') {
197
+ png = true;
198
+ if (i + 1 < argv.length && !looksLikeInput(argv[i + 1])) pngDir = argv[++i];
199
+ } else if (a.startsWith('--png=')) { png = true; pngDir = a.slice(6); }
141
200
  else if (a.startsWith('--')) { stderr.write('c2m excalidraw: unknown option ' + a + '\n'); return 1; }
142
201
  else targets.push(a);
143
202
  }
144
- if (targets.length !== 1) {
145
- stderr.write('c2m excalidraw: expected one <file.json> or - for stdin. See c2m excalidraw --help.\n');
203
+ if (png ? targets.length < 1 : targets.length !== 1) {
204
+ stderr.write('c2m excalidraw: expected ' + (png ? 'one or more <file.json>' : 'one <file.json>') +
205
+ ' or - for stdin. See c2m excalidraw --help.\n');
146
206
  return 1;
147
207
  }
148
208
 
149
209
  try {
150
- var text = readInput(targets[0], io.stdin);
151
- var input;
152
- try {
153
- input = JSON.parse(text);
154
- } catch (e) {
155
- throw new Error('input is not valid JSON: ' + e.message);
156
- }
157
- var built = buildSkeleton(input);
210
+ if (png) return await runPng(targets, { dir: pngDir, noUpload: noUpload }, io);
211
+ var built = buildSkeleton(parseJson(readInput(targets[0], io.stdin)));
158
212
  var url = noUpload ? null : await upload(built.scene, io.fetch);
159
213
  if (url) stdout.write(url + '\n');
160
214
  stdout.write('```mermaid\n' + built.preview + '\n```\n');
@@ -165,3 +219,55 @@ export async function runExcalidraw(argv, io) {
165
219
  return 1;
166
220
  }
167
221
  }
222
+
223
+ async function runPng(targets, opts, io) {
224
+ var stdout = io.stdout;
225
+ var names = targets.map(sceneName);
226
+ var dup = names.find(function (n, k) { return names.indexOf(n) !== k; });
227
+ if (dup) throw new Error('two inputs are both named "' + dup + '"; their PNGs would overwrite each other');
228
+ if (targets.filter(function (t) { return t === '-'; }).length > 1) throw new Error('stdin (-) can be read only once');
229
+
230
+ // Read and build every input before drawing anything, so a typo in the
231
+ // last file costs no renders.
232
+ var items = targets.map(function (t, k) {
233
+ var built = toScene(parseJson(readInput(t, io.stdin)));
234
+ return { name: names[k], scene: built.scene, warnings: built.warnings };
235
+ });
236
+
237
+ var render = await import('../src/excalidraw-png.js');
238
+ var dir;
239
+ if (opts.dir) dir = resolve(opts.dir);
240
+ else {
241
+ dir = render.defaultRenderDir(io.env);
242
+ render.cleanRenderCache(dir);
243
+ }
244
+ mkdirSync(dir, { recursive: true });
245
+
246
+ var blocks = [];
247
+ for (var k = 0; k < items.length; k++) {
248
+ var it = items[k];
249
+ var out;
250
+ try {
251
+ out = await render.renderScene(it.scene);
252
+ } catch (e) {
253
+ throw new Error(it.name + ': ' + (e && e.message ? e.message : e));
254
+ }
255
+ var file = resolve(dir, it.name + '.png');
256
+ var reviewFile = resolve(dir, it.name + '.review.png');
257
+ writeFileSync(file, out.png);
258
+ writeFileSync(reviewFile, out.review);
259
+ var url = opts.noUpload ? null : await upload(it.scene, io.fetch);
260
+ var warnings = it.warnings.slice();
261
+ if (out.scale < 3) warnings.push('drawn at scale ' + out.scale + ' to stay under 25,000,000 pixels');
262
+ if (render.hasCjkText(it.scene)) warnings.push('CJK text is not drawn: the CJK font is not shipped');
263
+ var lines = ['![' + it.name + '](attach:' + it.name + ')'];
264
+ if (url) lines.push('[Edit this diagram](' + url + ')');
265
+ lines.push(JSON.stringify(it.name) + ': ' + JSON.stringify('@file64:' + file));
266
+ lines.push('review: ' + reviewFile);
267
+ warnings.forEach(function (w) { lines.push('warning: ' + w); });
268
+ blocks.push(lines.join('\n') + '\n');
269
+ }
270
+ stdout.write(blocks.join('\n'));
271
+ stdout.write('\nCheck a drawing by its Mermaid preview first (c2m excalidraw <file> --no-upload, or c2m diagrams show <file> for a whole scene); open the review PNG only if that is not enough.\n');
272
+ return 0;
273
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@luutuankiet/clipboard2markdown",
3
- "version": "0.2.2",
3
+ "version": "0.4.0",
4
4
  "description": "Convert rich clipboard HTML from Jira, Confluence, Slack and Google apps into clean Markdown, and back.",
5
5
  "type": "module",
6
6
  "bin": {
@@ -11,6 +11,7 @@
11
11
  "files": [
12
12
  "bin",
13
13
  "src",
14
+ "vendor",
14
15
  "README.md",
15
16
  "LICENSE"
16
17
  ],
@@ -18,7 +19,9 @@
18
19
  "dev": "vite",
19
20
  "build": "vite build",
20
21
  "preview": "vite preview",
21
- "test": "vitest run"
22
+ "test": "vitest run",
23
+ "prepare": "node scripts/build-excalidraw-bundle.mjs",
24
+ "build:excalidraw": "node scripts/build-excalidraw-bundle.mjs"
22
25
  },
23
26
  "repository": {
24
27
  "type": "git",
@@ -47,15 +50,19 @@
47
50
  "node": ">=20.0.0"
48
51
  },
49
52
  "devDependencies": {
53
+ "@excalidraw/excalidraw": "0.18.1",
54
+ "esbuild": "^0.28.2",
50
55
  "mermaid": "^11.0.0",
51
56
  "vite": "^7.3.1",
52
57
  "vitest": "^4.0.18"
53
58
  },
54
59
  "dependencies": {
60
+ "@resvg/resvg-js": "^2.6.2",
55
61
  "jsdom": "^28.0.0",
56
62
  "marked": "^15.0.0",
57
63
  "pako": "^2.1.0",
58
64
  "turndown": "^7.2.2",
59
- "turndown-plugin-gfm": "^1.0.2"
65
+ "turndown-plugin-gfm": "^1.0.2",
66
+ "wawoff2": "^2.0.1"
60
67
  }
61
68
  }
@@ -0,0 +1,71 @@
1
+ // What the clipboard command does to the clipboard's content, without the
2
+ // clipboard: bin/cli.js keeps only pasteboard I/O, output streams and exit codes.
3
+ //
4
+ // Rich text -> Markdown, then a Mermaid reading copy under every excalidraw.com
5
+ // share link. Markdown -> HTML, with every generated reading copy removed first
6
+ // so only the link reaches the HTML. `diagrams: false` skips both steps and
7
+ // makes no request.
8
+
9
+ /**
10
+ * Determine conversion direction.
11
+ * @param {'html'|'text'} clipboardType - what the pasteboard held
12
+ * @param {boolean} forceToMd - --to-md flag was passed
13
+ * @param {boolean} forceToHtml - --to-html flag was passed
14
+ * @returns {'to-md'|'to-html'}
15
+ */
16
+ export function detectDirection(clipboardType, forceToMd, forceToHtml) {
17
+ if (forceToMd) return 'to-md';
18
+ if (forceToHtml) return 'to-html';
19
+ return clipboardType === 'html' ? 'to-md' : 'to-html';
20
+ }
21
+
22
+ /**
23
+ * @param {object} input
24
+ * @param {string} input.content - clipboard content
25
+ * @param {'html'|'text'} input.type - what the pasteboard held
26
+ * @param {boolean} [input.forceToMd]
27
+ * @param {boolean} [input.forceToHtml]
28
+ * @param {boolean} [input.diagrams=true] - expand / strip Excalidraw reading copies
29
+ * @param {Function} [input.fetch] - injectable for tests; defaults to globalThis.fetch
30
+ * @param {{convert: Function, convertMdToHtml: Function}} input.converters
31
+ * @returns {Promise<{output: string, kind: 'md'|'html', notice: string}>}
32
+ * notice is one line when something needs attention, else ''
33
+ */
34
+ export async function convertClipboard(input) {
35
+ var direction = detectDirection(input.type, input.forceToMd, input.forceToHtml);
36
+ var diagrams = input.diagrams !== false;
37
+ var converters = input.converters;
38
+
39
+ if (direction === 'to-html') {
40
+ var md = input.content;
41
+ if (diagrams) {
42
+ // Lazy, so --no-diagrams never loads the diagrams code
43
+ var { stripDiagramBlocks } = await import('./diagrams/markdown.js');
44
+ md = stripDiagramBlocks(md);
45
+ }
46
+ return { output: converters.convertMdToHtml(md), kind: 'html', notice: '' };
47
+ }
48
+
49
+ var markdown = converters.convert(input.content);
50
+ if (!diagrams) return { output: markdown, kind: 'md', notice: '' };
51
+
52
+ var [{ expandDiagramLinks }, { loadScene }, { retryingFetch, RETRY_POLICY }] = await Promise.all([
53
+ import('./diagrams/markdown.js'), import('./diagrams/share-link.js'), import('./diagrams/retry.js'),
54
+ ]);
55
+ var fetchImpl = retryingFetch(input.fetch || globalThis.fetch);
56
+ var report = {};
57
+ var output = await expandDiagramLinks(markdown, {
58
+ loadScene: function (url) { return loadScene(url, { fetch: fetchImpl }); },
59
+ parallel: true,
60
+ deadlineMs: RETRY_POLICY.deadlineMs,
61
+ onFailure: 'omit',
62
+ report: report,
63
+ });
64
+ return { output: output, kind: 'md', notice: skippedNotice(report) };
65
+ }
66
+
67
+ export function skippedNotice(report) {
68
+ if (!report.failed) return '';
69
+ var reasons = [...new Set(report.skipped.map(function (s) { return s.reason; }))];
70
+ return 'c2m: ' + report.failed + ' of ' + report.total + ' diagrams skipped (' + reasons.join(', ') + ')';
71
+ }
@@ -16,15 +16,24 @@
16
16
  // returns the original.
17
17
 
18
18
  import { parseShareLink, SHARE_LINK_PATTERN } from './share-link.js';
19
- import { sceneToMermaid, headerLine } from './scene-to-mermaid.js';
19
+ import { sceneToMermaid, headerLine, GENERATED_MARKER } from './scene-to-mermaid.js';
20
+ import { failureReason } from './retry.js';
20
21
 
21
22
  const BLOCK_PATTERN = /(?:\r?\n){0,2}[ \t]*<!-- excalidraw-mermaid:begin ([A-Za-z0-9_-]+) -->[\s\S]*?<!-- excalidraw-mermaid:end \1 -->/g;
23
+ // A generated block whose begin/end comments were lost (rich text drops HTML
24
+ // comments): a fence whose first line is our header. Any other fence is the
25
+ // author's and is never touched.
26
+ const escapeRe = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
27
+ const HEADER_FENCE_PATTERN = new RegExp(
28
+ '(?:\\r?\\n){0,2}^[ \\t]*(`{3,}|~{3,})[ \\t]*(?:mermaid)?[ \\t]*\\r?\\n' +
29
+ '[ \\t]*%% generated from [^\\n;]*; ' + escapeRe(GENERATED_MARKER) + '[^\\n]*\\n' +
30
+ '(?:[^\\n]*\\n)*?[ \\t]*\\1[ \\t]*\\r?$', 'gm');
22
31
  const LIST_MARKER = /^(\s*)([-*+]|\d+[.)])(\s+)/;
23
32
  const FENCE = /^\s*(```|~~~)/;
24
33
  const HEADING = /^\s{0,3}#{1,6}\s/;
25
34
 
26
35
  export function stripDiagramBlocks(markdown) {
27
- return String(markdown).replace(BLOCK_PATTERN, '');
36
+ return String(markdown).replace(BLOCK_PATTERN, '').replace(HEADER_FENCE_PATTERN, '');
28
37
  }
29
38
 
30
39
  // Line table with offsets so insertion works on the original string untouched.
@@ -114,39 +123,93 @@ export function findDiagramLinks(markdown) {
114
123
  }
115
124
 
116
125
  // Replaces any existing generated blocks, then inserts fresh ones. One failing
117
- // link produces a visible error block and never stops the others.
126
+ // link never stops the others.
127
+ //
128
+ // options.loadScene url -> scene (required)
129
+ // options.parallel download every link at once instead of one by one
130
+ // options.deadlineMs give up on links not loaded by then (reason "timeout")
131
+ // options.onFailure "block" (default): a visible "could not load" block;
132
+ // "omit": leave the link as written with no block
133
+ // options.report filled with { total, done, failed, outline, skipped },
134
+ // skipped = [{ url, reason }] for every failed link
118
135
  export async function expandDiagramLinks(markdown, options) {
119
- var loadScene = options && options.loadScene;
136
+ options = options || {};
137
+ var loadScene = options.loadScene;
120
138
  if (typeof loadScene !== 'function') throw new Error('expandDiagramLinks needs options.loadScene');
121
- var onProgress = (options && options.onProgress) || function () {};
139
+ var onProgress = options.onProgress || function () {};
140
+ var omitFailures = options.onFailure === 'omit';
122
141
  var text = stripDiagramBlocks(markdown);
123
142
  var links = findDiagramLinks(text);
124
143
  var stats = { total: links.length, done: 0, failed: 0, outline: 0 };
144
+ var skipped = [];
125
145
  onProgress(Object.assign({}, stats));
126
146
 
127
- var blocks = [];
128
- for (var i = 0; i < links.length; i++) {
129
- var item = links[i];
130
- var mermaid;
131
- try {
132
- var scene = await loadScene(item.link.url);
133
- var result = sceneToMermaid(scene, { sourceUrl: item.link.url });
134
- if (result.kind === 'outline') stats.outline++;
135
- mermaid = result.text;
136
- } catch (err) {
147
+ function render(item, scene) {
148
+ var result = sceneToMermaid(scene, { sourceUrl: item.link.url });
149
+ if (result.kind === 'outline') stats.outline++;
150
+ return result.text;
151
+ }
152
+ function settle(item, outcome) {
153
+ var mermaid = null;
154
+ if (outcome.ok) {
155
+ try {
156
+ mermaid = render(item, outcome.scene);
157
+ } catch (err) {
158
+ outcome = { ok: false, err: err };
159
+ }
160
+ }
161
+ if (!outcome.ok) {
162
+ var err = outcome.err;
137
163
  stats.failed++;
138
- mermaid = headerLine(item.link.url) + '\n%% could not load: ' + String(err && err.message ? err.message : err).replace(/\s+/g, ' ');
164
+ skipped.push({ url: item.link.url, reason: failureReason(err) });
165
+ if (!omitFailures) {
166
+ mermaid = headerLine(item.link.url) + '\n%% could not load: ' + String(err && err.message ? err.message : err).replace(/\s+/g, ' ');
167
+ }
139
168
  }
140
- blocks.push({ offset: item.offset, text: '\n\n' + renderBlock(item.link.id, mermaid, item.indent) });
141
169
  stats.done++;
142
170
  onProgress(Object.assign({}, stats));
171
+ return mermaid;
143
172
  }
173
+ function load(item) {
174
+ return Promise.resolve()
175
+ .then(function () { return loadScene(item.link.url); })
176
+ .then(function (scene) { return { ok: true, scene: scene }; }, function (err) { return { ok: false, err: err }; });
177
+ }
178
+
179
+ var timer;
180
+ var deadline = options.deadlineMs == null ? null : new Promise(function (resolve) {
181
+ timer = setTimeout(function () {
182
+ var err = new Error('no answer within ' + options.deadlineMs + 'ms');
183
+ err.reason = 'timeout';
184
+ resolve({ ok: false, err: err });
185
+ }, options.deadlineMs);
186
+ });
187
+ var race = function (p) { return deadline ? Promise.race([p, deadline]) : p; };
188
+
189
+ var mermaids = [];
190
+ var run = function (item, idx) {
191
+ return race(load(item)).then(function (outcome) { mermaids[idx] = settle(item, outcome); });
192
+ };
193
+ try {
194
+ if (options.parallel) {
195
+ await Promise.all(links.map(run));
196
+ } else {
197
+ for (var i = 0; i < links.length; i++) await run(links[i], i);
198
+ }
199
+ } finally {
200
+ clearTimeout(timer);
201
+ }
202
+
203
+ var blocks = [];
204
+ links.forEach(function (item, idx) {
205
+ if (mermaids[idx] != null) blocks.push({ offset: item.offset, text: '\n\n' + renderBlock(item.link.id, mermaids[idx], item.indent) });
206
+ });
144
207
 
145
208
  // Insert back to front so earlier offsets stay valid; same-offset blocks keep order.
146
209
  var out = text;
147
210
  for (var k = blocks.length - 1; k >= 0; k--) {
148
211
  out = out.slice(0, blocks[k].offset) + blocks[k].text + out.slice(blocks[k].offset);
149
212
  }
150
- if (options && options.report) Object.assign(options.report, stats);
213
+ if (options.report) Object.assign(options.report, stats, { skipped: skipped });
151
214
  return out;
152
215
  }