@luutuankiet/clipboard2markdown 0.2.1 โ†’ 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.
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
@@ -37,13 +37,13 @@ For documents whose diagrams live as excalidraw.com share links (a screenshot pl
37
37
 
38
38
  Privacy: this mode sends one read-only `GET` per link to `json.excalidraw.com` (the backend named by the link) and decrypts the scene in your browser with the key in the link's `#` fragment. Nothing you paste is sent. The HTML โ†’ MD and MD โ†’ HTML modes make no network requests at all.
39
39
 
40
- **CLI for agents** (`bin/diagrams.js`, Node 20+, any OS):
40
+ **CLI for agents** (`c2m diagrams`, Node 20+, any OS):
41
41
 
42
42
  ```bash
43
- node bin/diagrams.js expand doc.md [--write] # same output as the page
44
- node bin/diagrams.js strip doc.md [--write]
45
- node bin/diagrams.js show '<share url>' # or a local .excalidraw file
46
- node bin/diagrams.js edit '<share url>' --ops '[{"op":"rename","node":"auth","label":"Identity"}]'
43
+ c2m diagrams expand doc.md [--write] # same output as the page
44
+ c2m diagrams strip doc.md [--write]
45
+ c2m diagrams show '<share url>' # or a local .excalidraw file
46
+ c2m diagrams edit '<share url>' --ops '[{"op":"rename","node":"auth","label":"Identity"}]'
47
47
  ```
48
48
 
49
49
  `edit` patches the existing canvas (rename, add, connect, disconnect, relabel-edge, remove), keeps every untouched element exactly as it was, uploads the result and prints exactly one new share link. The old link keeps working. Node ids are the ones `show` prints. Uploading exists only in the CLI.
@@ -66,14 +66,40 @@ The Mermaid is deliberately lossy: positions, colours, fonts and arrow curves ar
66
66
 
67
67
  ## Command Line
68
68
 
69
- The same converters ship as the `c2m` CLI:
69
+ Everything ships as one command, `clipboard2markdown`, also installed under the short name `c2m`. Run it without installing:
70
+
71
+ ```bash
72
+ npx -y @luutuankiet/clipboard2markdown --help
73
+ npx -y @luutuankiet/clipboard2markdown excalidraw diagram.json
74
+ npx -y @luutuankiet/clipboard2markdown diagrams show '<share url>'
75
+ ```
76
+
77
+ or install it once:
70
78
 
71
79
  ```bash
72
80
  npm install -g @luutuankiet/clipboard2markdown
73
81
  c2m --help
74
82
  ```
75
83
 
76
- `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.
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.
85
+
86
+ It handles Excalidraw share links in both directions:
87
+
88
+ - **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.
89
+ - **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.
90
+ - `--no-diagrams` skips both steps.
91
+
92
+ 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.
93
+
94
+ For a macro, a global install run by absolute path skips npx's registry lookup (about 1.7s per run):
95
+
96
+ ```bash
97
+ npm i -g @luutuankiet/clipboard2markdown@latest
98
+ # prints the line to put in the macro; launchers often don't load your PATH
99
+ echo "$(command -v node) $(npm root -g)/@luutuankiet/clipboard2markdown/bin/cli.js 2>/dev/null"
100
+ ```
101
+
102
+ 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`.
77
103
 
78
104
  ## Development
79
105
 
package/bin/cli.js CHANGED
@@ -5,20 +5,37 @@ import { tmpdir } from 'os';
5
5
  import { join } from 'path';
6
6
  import { fileURLToPath } from 'url';
7
7
 
8
- const HELP = `c2m - convert the clipboard between rich HTML and Markdown
8
+ const HELP = `clipboard2markdown (short name: c2m) - clipboard HTML <-> Markdown,
9
+ and Excalidraw diagrams for agents
9
10
 
10
11
  Usage:
11
- c2m HTML on the clipboard -> Markdown, plain text -> HTML
12
- c2m --to-md force HTML -> Markdown
13
- c2m --to-html force Markdown -> HTML
14
- c2m --help show this help
15
- 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.
16
25
 
17
26
  c2m excalidraw <file|-> JSON diagram -> excalidraw.com share link and
18
- Mermaid preview (any OS; see c2m excalidraw --help)
27
+ Mermaid preview (see c2m excalidraw --help)
28
+ c2m diagrams <command> existing share links <-> Mermaid: expand, strip,
29
+ show, edit (see c2m diagrams --help)
19
30
 
20
- Clipboard conversion needs macOS (it uses osascript).
31
+ Every command also runs without installing:
32
+ npx -y @luutuankiet/clipboard2markdown excalidraw diagram.json
33
+
34
+ Clipboard conversion needs macOS (it uses osascript); excalidraw and
35
+ diagrams run anywhere Node 20+ runs.
21
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).
22
39
  `;
23
40
 
24
41
  // Converters need jsdom's DOM globals; loaded lazily so commands that
@@ -108,20 +125,9 @@ pb.setStringForType(str, "public.utf8-plain-text");
108
125
  }
109
126
  }
110
127
 
111
- // --- core logic (exported for testing) ---
112
-
113
- /**
114
- * Determine conversion direction.
115
- * @param {'html'|'text'} clipboardType - what the pasteboard held
116
- * @param {boolean} forceToMd - --to-md flag was passed
117
- * @param {boolean} forceToHtml - --to-html flag was passed
118
- * @returns {'to-md'|'to-html'}
119
- */
120
- export function detectDirection(clipboardType, forceToMd, forceToHtml) {
121
- if (forceToMd) return 'to-md';
122
- if (forceToHtml) return 'to-html';
123
- return clipboardType === 'html' ? 'to-md' : 'to-html';
124
- }
128
+ // --- core logic lives in src/clipboard-convert.js; re-exported for tests ---
129
+
130
+ export { detectDirection } from '../src/clipboard-convert.js';
125
131
 
126
132
  // --- entry point (only runs when executed directly) ---
127
133
 
@@ -143,6 +149,15 @@ if (isEntryPoint()) {
143
149
  const { runExcalidraw } = await import('./excalidraw.js');
144
150
  process.exit(await runExcalidraw(args.slice(1), { stdout: process.stdout, stderr: process.stderr }));
145
151
  }
152
+ if (args[0] === 'diagrams') {
153
+ const { main } = await import('./diagrams.js');
154
+ try {
155
+ process.exit(await main(args.slice(1), { stdout: process.stdout, stderr: process.stderr }));
156
+ } catch (err) {
157
+ process.stderr.write('error: ' + (err && err.message ? err.message : err) + '\n');
158
+ process.exit(1);
159
+ }
160
+ }
146
161
 
147
162
  if (args.includes('--help') || args.includes('-h')) {
148
163
  process.stdout.write(HELP);
@@ -157,29 +172,36 @@ if (isEntryPoint()) {
157
172
  process.exit(2);
158
173
  }
159
174
 
160
- const forceToMd = args.includes('--to-md');
161
- 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
+ };
162
181
 
163
182
  try {
164
- const { convert, convertMdToHtml } = await loadConverters();
183
+ const { convertClipboard } = await import('../src/clipboard-convert.js');
184
+ const converters = await loadConverters();
165
185
  const clipboard = readClipboard();
166
186
  if (!clipboard.content || !clipboard.content.trim()) {
167
- console.error('c2m: empty clipboard');
187
+ notify('c2m: empty clipboard');
168
188
  process.exit(1);
169
189
  }
170
- const direction = detectDirection(clipboard.type, forceToMd, forceToHtml);
171
- if (direction === 'to-md') {
172
- const md = convert(clipboard.content);
173
- console.error(`c2m: ${clipboard.type} โ†’ md (${md.length} chars)`);
174
- writeTextToClipboard(md);
175
- } else {
176
- const html = convertMdToHtml(clipboard.content);
177
- console.error(`c2m: ${clipboard.type} โ†’ html (${html.length} chars)`);
178
- writeHtmlToClipboard(html);
179
- }
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);
180
202
  process.exit(0);
181
203
  } catch (e) {
182
- console.error(`c2m: ${e.message}`);
204
+ notify(`c2m: ${e.message}`);
183
205
  process.exit(2);
184
206
  }
185
207
  }
package/bin/diagrams.js CHANGED
@@ -1,5 +1,6 @@
1
1
  #!/usr/bin/env node
2
- // c2m-diagrams: Excalidraw share links <-> Mermaid, for agents.
2
+ // c2m diagrams: Excalidraw share links <-> Mermaid, for agents. Reached through
3
+ // bin/cli.js (`c2m diagrams ...`) and, for older installs, as `c2m-diagrams`.
3
4
  // Runs anywhere Node 20+ runs; no clipboard, no browser.
4
5
  //
5
6
  // stdout carries only the primary result (Markdown, Mermaid or the new link);
@@ -12,8 +13,9 @@ import {
12
13
  expandDiagramLinks, stripDiagramBlocks, loadScene, sceneToMermaid, editLink,
13
14
  parseShareLink, normalizeScene,
14
15
  } from '../src/diagrams/index.js';
16
+ import { retryingFetch } from '../src/diagrams/retry.js';
15
17
 
16
- const USAGE = `usage: c2m-diagrams <command> [options]
18
+ const USAGE = `usage: c2m diagrams <command> [options]
17
19
 
18
20
  expand <file.md> [--write] add a Mermaid reading copy under every excalidraw.com link
19
21
  strip <file.md> [--write] remove every generated block
@@ -29,7 +31,8 @@ ops: [{"op":"rename","node","label"}, {"op":"add","id","label","near","side"?},
29
31
  `;
30
32
 
31
33
  // Test hook: a directory standing in for json.excalidraw.com. GET reads
32
- // <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.
33
36
  function fileBackendFetch(dir) {
34
37
  return async function (url, init) {
35
38
  var method = ((init && init.method) || 'GET').toUpperCase();
@@ -47,6 +50,11 @@ function fileBackendFetch(dir) {
47
50
  return respond(200, Buffer.from(JSON.stringify({ id: id })));
48
51
  }
49
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
+ }
50
58
  if (!fs.existsSync(file)) return respond(404, Buffer.from('{}'));
51
59
  return respond(200, fs.readFileSync(file));
52
60
  };
@@ -92,8 +100,10 @@ export async function main(argv, io) {
92
100
  out = stripDiagramBlocks(md);
93
101
  } else {
94
102
  var report = {};
103
+ var retrying = retryingFetch(fetchImpl);
95
104
  out = await expandDiagramLinks(md, {
96
- loadScene: function (url) { return loadScene(url, { fetch: fetchImpl }); },
105
+ loadScene: function (url) { return loadScene(url, { fetch: retrying }); },
106
+ parallel: true,
97
107
  onProgress: function (p) { if (p.total) stderr.write('expanding ' + p.done + ' of ' + p.total + ' diagrams\n'); },
98
108
  report: report,
99
109
  });
package/bin/excalidraw.js CHANGED
@@ -105,7 +105,7 @@ WARNINGS
105
105
 
106
106
  NOT SUPPORTED
107
107
  images, frames, freedraw, Mermaid as input, editing an existing link (use
108
- c2m-diagrams edit for that), writing .excalidraw files.
108
+ c2m diagrams edit for that), writing .excalidraw files.
109
109
  `;
110
110
 
111
111
  function readInput(target, stdin) {
package/package.json CHANGED
@@ -1,9 +1,10 @@
1
1
  {
2
2
  "name": "@luutuankiet/clipboard2markdown",
3
- "version": "0.2.1",
3
+ "version": "0.3.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": {
7
+ "clipboard2markdown": "bin/cli.js",
7
8
  "c2m": "bin/cli.js",
8
9
  "c2m-diagrams": "bin/diagrams.js"
9
10
  },
@@ -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
  }
@@ -0,0 +1,65 @@
1
+ // Retry policy for scene downloads, used by the CLI paths only. The page calls
2
+ // loadScene with a plain fetch and never loads this file.
3
+ //
4
+ // Each attempt gets its own timeout. A timeout, a network error, HTTP 429 and
5
+ // HTTP 5xx are retried; anything else (404, other 4xx) is handed back at once,
6
+ // and decrypt / decompress failures happen after fetch so are never retried.
7
+
8
+ export const RETRY_POLICY = { attemptTimeoutMs: 3000, delaysMs: [250, 750], deadlineMs: 6000 };
9
+
10
+ const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
11
+
12
+ function failure(message, reason) {
13
+ var err = new Error(message);
14
+ err.reason = reason;
15
+ return err;
16
+ }
17
+
18
+ function attempt(fetchImpl, url, init, timeoutMs) {
19
+ var controller = typeof AbortController === 'function' ? new AbortController() : null;
20
+ var timer;
21
+ // Raced rather than trusted to the signal: a fetch that ignores abort must
22
+ // still lose to the clock.
23
+ var timeout = new Promise(function (_, reject) {
24
+ timer = setTimeout(function () {
25
+ if (controller) controller.abort();
26
+ reject(failure('timed out after ' + timeoutMs + 'ms', 'timeout'));
27
+ }, timeoutMs);
28
+ });
29
+ var request = Promise.resolve()
30
+ .then(function () { return fetchImpl(url, Object.assign({}, init, controller ? { signal: controller.signal } : {})); })
31
+ .catch(function (err) { throw err && err.reason ? err : failure('network error: ' + (err && err.message ? err.message : err), 'network'); });
32
+ return Promise.race([request, timeout]).finally(function () { clearTimeout(timer); });
33
+ }
34
+
35
+ const retryable = (status) => status === 429 || status >= 500;
36
+
37
+ // A fetch with the same signature that retries temporary failures.
38
+ export function retryingFetch(fetchImpl, policy) {
39
+ var p = Object.assign({}, RETRY_POLICY, policy);
40
+ return async function (url, init) {
41
+ var last;
42
+ for (var i = 0; i <= p.delaysMs.length; i++) {
43
+ if (i > 0) await wait(p.delaysMs[i - 1]);
44
+ try {
45
+ var res = await attempt(fetchImpl, url, init, p.attemptTimeoutMs);
46
+ if (!retryable(res.status)) return res;
47
+ last = res;
48
+ } catch (err) {
49
+ last = err;
50
+ }
51
+ }
52
+ if (last instanceof Error) throw last;
53
+ return last;
54
+ };
55
+ }
56
+
57
+ // Short reason for a failed download, for the one-line notice.
58
+ export function failureReason(err) {
59
+ if (err && err.reason) return err.reason;
60
+ var status = err && err.status;
61
+ if (status === 404) return '404';
62
+ if (status >= 500) return '5xx';
63
+ if (status) return String(status);
64
+ return 'error';
65
+ }
@@ -142,14 +142,18 @@ export async function loadScene(url, options) {
142
142
  if (!link) throw new Error('not an excalidraw.com share link');
143
143
  var res = await pickFetch(options)(link.backendBase + link.id, { method: 'GET' });
144
144
  if (!res.ok) {
145
- throw new Error(res.status === 404 ? 'scene not found (HTTP 404)' : 'HTTP ' + res.status);
145
+ var httpErr = new Error(res.status === 404 ? 'scene not found (HTTP 404)' : 'HTTP ' + res.status);
146
+ httpErr.status = res.status;
147
+ throw httpErr;
146
148
  }
147
149
  var bytes = new Uint8Array(await res.arrayBuffer());
148
150
  var data;
149
151
  try {
150
152
  data = await decodePayload(bytes, link.key);
151
153
  } catch (e) {
152
- throw new Error('could not decrypt scene (wrong key or corrupt payload)');
154
+ var decryptErr = new Error('could not decrypt scene (wrong key or corrupt payload)');
155
+ decryptErr.reason = 'decrypt';
156
+ throw decryptErr;
153
157
  }
154
158
  return normalizeScene(data);
155
159
  }