@atmo-dev/kit 0.18.1 → 0.19.1
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/dist/articles/ArticleCard.svelte +97 -0
- package/dist/articles/ArticleCard.svelte.d.ts +20 -0
- package/dist/articles/ArticleCodeBlock.svelte +121 -0
- package/dist/articles/ArticleCodeBlock.svelte.d.ts +11 -0
- package/dist/articles/ArticleEditor.svelte +340 -0
- package/dist/articles/ArticleEditor.svelte.d.ts +48 -0
- package/dist/articles/ArticleLinkEmbed.svelte +50 -0
- package/dist/articles/ArticleLinkEmbed.svelte.d.ts +5 -0
- package/dist/articles/ArticleView.svelte +128 -0
- package/dist/articles/ArticleView.svelte.d.ts +46 -0
- package/dist/articles/PublicationIcon.svelte +37 -0
- package/dist/articles/PublicationIcon.svelte.d.ts +10 -0
- package/dist/articles/article-link-embed.d.ts +36 -0
- package/dist/articles/article-link-embed.js +29 -0
- package/dist/articles/dates.d.ts +2 -0
- package/dist/articles/dates.js +12 -0
- package/dist/articles/images.d.ts +5 -0
- package/dist/articles/images.js +44 -0
- package/dist/articles/markdown.d.ts +15 -0
- package/dist/articles/markdown.js +52 -0
- package/dist/articles/paste.d.ts +11 -0
- package/dist/articles/paste.js +27 -0
- package/dist/articles/records.d.ts +166 -0
- package/dist/articles/records.js +423 -0
- package/dist/articles/styles.d.ts +4 -0
- package/dist/articles/styles.js +14 -0
- package/dist/articles-data.d.ts +8 -0
- package/dist/articles-data.js +8 -0
- package/dist/articles.d.ts +10 -0
- package/dist/articles.js +8 -0
- package/dist/components/ExternalEmbed.svelte +16 -2
- package/dist/components/ExternalEmbed.svelte.d.ts +6 -0
- package/dist/components/IconMaker.svelte +119 -0
- package/dist/components/IconMaker.svelte.d.ts +9 -0
- package/dist/components/IconRail.svelte +9 -6
- package/dist/components/IconRail.svelte.d.ts +2 -0
- package/dist/components/LinkEmbed.svelte +24 -21
- package/dist/components/LinkEmbed.svelte.d.ts +3 -2
- package/dist/components/PaletteApply.svelte +62 -0
- package/dist/components/PaletteApply.svelte.d.ts +9 -0
- package/dist/components/PalettePicker.svelte +92 -0
- package/dist/components/PalettePicker.svelte.d.ts +13 -0
- package/dist/components/PostComments.svelte +129 -0
- package/dist/components/PostComments.svelte.d.ts +15 -0
- package/dist/components/RichText.svelte +199 -9
- package/dist/components/RichText.svelte.d.ts +17 -0
- package/dist/components/RichTextEditor.svelte +164 -13
- package/dist/components/RichTextEditor.svelte.d.ts +26 -3
- package/dist/components/composer/ComposerPost.svelte +10 -0
- package/dist/components/composer/ComposerPost.svelte.d.ts +3 -1
- package/dist/components/composer/PostComposer.svelte +28 -2
- package/dist/components/composer/PostComposer.svelte.d.ts +12 -1
- package/dist/editor/EditorMenus.svelte +438 -0
- package/dist/editor/EditorMenus.svelte.d.ts +9 -0
- package/dist/editor/SlashOptions.svelte +45 -0
- package/dist/editor/SlashOptions.svelte.d.ts +12 -0
- package/dist/editor/article-commands.d.ts +44 -0
- package/dist/editor/article-commands.js +289 -0
- package/dist/editor/article-extensions.d.ts +8 -0
- package/dist/editor/article-extensions.js +315 -0
- package/dist/editor/article-menus.d.ts +2 -0
- package/dist/editor/article-menus.js +3 -0
- package/dist/editor/document.d.ts +5 -0
- package/dist/editor/document.js +163 -12
- package/dist/editor/image-node.d.ts +8 -0
- package/dist/editor/image-node.js +75 -0
- package/dist/editor/markdown-extensions.d.ts +1 -5
- package/dist/editor/markdown-extensions.js +37 -2
- package/dist/editor/slash-menu.svelte.d.ts +9 -0
- package/dist/editor/slash-menu.svelte.js +134 -0
- package/dist/embeds/PlayerLinkEmbed.svelte +9 -1
- package/dist/embeds/PostLinkEmbed.svelte +12 -29
- package/dist/embeds/PostLinkEmbed.svelte.d.ts +3 -0
- package/dist/embeds/ProfileLinkEmbed.svelte +12 -17
- package/dist/embeds/ProfileLinkEmbed.svelte.d.ts +3 -0
- package/dist/embeds/defaults.d.ts +4 -2
- package/dist/embeds/defaults.js +7 -3
- package/dist/embeds/load.d.ts +44 -0
- package/dist/embeds/load.js +139 -0
- package/dist/embeds/types.d.ts +18 -3
- package/dist/events/EventComments.svelte +4 -101
- package/dist/events/EventComments.svelte.d.ts +1 -1
- package/dist/events/EventView.svelte +5 -0
- package/dist/events/EventView.svelte.d.ts +3 -0
- package/dist/events/event-link-embed.d.ts +4 -1
- package/dist/events/event-link-embed.js +9 -1
- package/dist/events/event-view/EventLinkEmbed.svelte +8 -8
- package/dist/events/event-view/EventLinkEmbed.svelte.d.ts +3 -2
- package/dist/icon-maker/artwork.d.ts +11 -0
- package/dist/icon-maker/artwork.js +25 -0
- package/dist/icon-maker/index.d.ts +12 -0
- package/dist/icon-maker/index.js +73 -0
- package/dist/index.d.ts +10 -3
- package/dist/index.js +7 -1
- package/dist/palettes.d.ts +20 -0
- package/dist/palettes.js +37 -0
- package/dist/richtext/article-styles.d.ts +10 -0
- package/dist/richtext/article-styles.js +28 -0
- package/dist/richtext/document.d.ts +63 -1
- package/dist/richtext/document.js +54 -0
- package/dist/richtext/markdown.d.ts +19 -4
- package/dist/richtext/markdown.js +223 -18
- package/dist/richtext/serialize.d.ts +5 -1
- package/dist/richtext/serialize.js +163 -31
- package/dist/richtext/vendor/markdown-tree.d.ts +4 -3
- package/dist/richtext/vendor/markdown-tree.js +2584 -26
- package/dist/richtext.d.ts +7 -5
- package/dist/richtext.js +1 -1
- package/dist/social/external-embeds.d.ts +5 -0
- package/dist/social/external-embeds.js +6 -1
- package/dist/social/mention-suggestions.svelte.js +18 -7
- package/dist/social/posts.d.ts +2 -0
- package/dist/social/posts.js +43 -2
- package/dist/social/profile.js +2 -1
- package/package.json +28 -2
|
@@ -1,23 +1,26 @@
|
|
|
1
1
|
import { detectTokens } from './detect.js';
|
|
2
|
-
import { safeWebUrl } from './document.js';
|
|
3
|
-
import { exceedsMarkdownLimits, parseMarkdown } from './markdown.js';
|
|
2
|
+
import { isCallout, safeWebUrl } from './document.js';
|
|
3
|
+
import { exceedsMarkdownLimits, FOOTNOTE_ID, parseMarkdown } from './markdown.js';
|
|
4
4
|
const MAX_DEPTH = 32;
|
|
5
|
-
const STYLES = ['strong', 'emphasis', 'delete'];
|
|
5
|
+
const STYLES = ['strong', 'emphasis', 'delete', 'highlight'];
|
|
6
6
|
function sameMarks(a, b) {
|
|
7
7
|
return (!a.br === !b.br &&
|
|
8
8
|
!a.strong === !b.strong &&
|
|
9
9
|
!a.emphasis === !b.emphasis &&
|
|
10
10
|
!a.delete === !b.delete &&
|
|
11
|
+
!a.highlight === !b.highlight &&
|
|
11
12
|
!a.code === !b.code &&
|
|
13
|
+
!a.math === !b.math &&
|
|
14
|
+
a.ref === b.ref &&
|
|
12
15
|
a.link === b.link);
|
|
13
16
|
}
|
|
14
17
|
function merge(runs) {
|
|
15
18
|
const result = [];
|
|
16
19
|
for (const run of runs) {
|
|
17
|
-
if (!run.br && !run.text)
|
|
20
|
+
if (!run.br && !run.ref && !run.text)
|
|
18
21
|
continue;
|
|
19
22
|
const previous = result.at(-1);
|
|
20
|
-
if (previous && !run.br && !previous.br && sameMarks(previous, run))
|
|
23
|
+
if (previous && !run.br && !previous.br && !run.ref && sameMarks(previous, run))
|
|
21
24
|
previous.text += run.text;
|
|
22
25
|
else
|
|
23
26
|
result.push({ ...run });
|
|
@@ -28,13 +31,15 @@ function plainText(nodes, depth = 0) {
|
|
|
28
31
|
if (depth > MAX_DEPTH)
|
|
29
32
|
return '';
|
|
30
33
|
return nodes
|
|
31
|
-
.map((node) => node.type === 'text' || node.type === 'code'
|
|
34
|
+
.map((node) => node.type === 'text' || node.type === 'code' || node.type === 'math'
|
|
32
35
|
? node.value
|
|
33
36
|
: node.type === 'break'
|
|
34
37
|
? '\n'
|
|
35
38
|
: node.type === 'mention' || node.type === 'tag'
|
|
36
39
|
? node.text
|
|
37
|
-
:
|
|
40
|
+
: node.type === 'footnoteReference'
|
|
41
|
+
? ''
|
|
42
|
+
: plainText(node.children, depth + 1))
|
|
38
43
|
.join('');
|
|
39
44
|
}
|
|
40
45
|
/** A link written as its own destination is plain text again once it is serialized. */
|
|
@@ -60,6 +65,11 @@ function flatten(nodes, marks, depth = 0) {
|
|
|
60
65
|
return [{ text: '', br: true }];
|
|
61
66
|
case 'code':
|
|
62
67
|
return [{ ...marks, code: true, text: node.value.replace(/\n/g, ' ') }];
|
|
68
|
+
case 'math':
|
|
69
|
+
return [{ ...marks, math: true, text: node.value.replace(/\n/g, ' ') }];
|
|
70
|
+
case 'footnoteReference':
|
|
71
|
+
// Without the surrounding formatting, which then closes and opens around it.
|
|
72
|
+
return FOOTNOTE_ID.test(node.id) ? [{ text: '', ref: node.id }] : [];
|
|
63
73
|
case 'mention':
|
|
64
74
|
case 'tag':
|
|
65
75
|
return [{ ...marks, text: node.text.replace(/\n/g, ' ') }];
|
|
@@ -68,6 +78,7 @@ function flatten(nodes, marks, depth = 0) {
|
|
|
68
78
|
case 'strong':
|
|
69
79
|
case 'emphasis':
|
|
70
80
|
case 'delete':
|
|
81
|
+
case 'highlight':
|
|
71
82
|
return flatten(node.children, { ...marks, [node.type]: true }, depth + 1);
|
|
72
83
|
default:
|
|
73
84
|
return [];
|
|
@@ -82,7 +93,7 @@ function hoistWhitespace(input) {
|
|
|
82
93
|
let runs = merge(input);
|
|
83
94
|
const result = [];
|
|
84
95
|
runs.forEach((run, index) => {
|
|
85
|
-
if (run.br || run.code || !STYLES.some((style) => run[style]))
|
|
96
|
+
if (run.br || run.code || run.math || run.ref || !STYLES.some((style) => run[style]))
|
|
86
97
|
return void result.push(run);
|
|
87
98
|
const previous = runs[index - 1];
|
|
88
99
|
const next = runs[index + 1];
|
|
@@ -108,7 +119,7 @@ function hoistWhitespace(input) {
|
|
|
108
119
|
for (;;) {
|
|
109
120
|
const trimmed = [];
|
|
110
121
|
runs.forEach((run, index) => {
|
|
111
|
-
if (run.br || run.code)
|
|
122
|
+
if (run.br || run.code || run.math || run.ref)
|
|
112
123
|
return void trimmed.push(run);
|
|
113
124
|
let text = run.text;
|
|
114
125
|
const previous = runs[index - 1];
|
|
@@ -152,6 +163,15 @@ function paragraphs(nodes) {
|
|
|
152
163
|
result.push(lines);
|
|
153
164
|
return result;
|
|
154
165
|
}
|
|
166
|
+
function cellRuns(cell) {
|
|
167
|
+
return headingRuns(paragraphs(cell).flat());
|
|
168
|
+
}
|
|
169
|
+
/** Per item whether it is a task and done; undefined for a list without tasks. */
|
|
170
|
+
function tasks(block) {
|
|
171
|
+
return block.checked?.some((value) => typeof value === 'boolean')
|
|
172
|
+
? block.items.map((_, index) => block.checked[index] ?? null)
|
|
173
|
+
: undefined;
|
|
174
|
+
}
|
|
155
175
|
function shapeOf(blocks, depth = 0) {
|
|
156
176
|
if (depth > MAX_DEPTH)
|
|
157
177
|
return [];
|
|
@@ -164,18 +184,38 @@ function shapeOf(blocks, depth = 0) {
|
|
|
164
184
|
return [{ h: block.depth, runs: headingRuns(lines) }];
|
|
165
185
|
}
|
|
166
186
|
case 'blockquote':
|
|
167
|
-
return [{ q: shapeOf(block.children, depth + 1) }];
|
|
187
|
+
return [{ q: shapeOf(block.children, depth + 1), co: block.callout ?? false }];
|
|
168
188
|
case 'list':
|
|
169
189
|
return [
|
|
170
190
|
{
|
|
171
191
|
l: block.ordered ? (block.start ?? 1) : false,
|
|
192
|
+
k: tasks(block) ?? false,
|
|
172
193
|
items: block.items.map((item) => shapeOf(item, depth + 1))
|
|
173
194
|
}
|
|
174
195
|
];
|
|
196
|
+
case 'math':
|
|
197
|
+
return [{ m: block.value }];
|
|
198
|
+
case 'footnoteDefinition':
|
|
199
|
+
return [{ f: block.id, c: shapeOf(block.children, depth + 1) }];
|
|
175
200
|
case 'codeBlock':
|
|
176
201
|
return [{ c: block.value, lang: block.lang ?? '' }];
|
|
177
202
|
case 'thematicBreak':
|
|
178
203
|
return [{ hr: true }];
|
|
204
|
+
case 'image':
|
|
205
|
+
return [
|
|
206
|
+
{
|
|
207
|
+
img: safeWebUrl(block.url) ?? block.url,
|
|
208
|
+
alt: block.alt ?? '',
|
|
209
|
+
title: block.title ?? ''
|
|
210
|
+
}
|
|
211
|
+
];
|
|
212
|
+
case 'table':
|
|
213
|
+
return [
|
|
214
|
+
{
|
|
215
|
+
t: block.align.map((align) => align ?? ''),
|
|
216
|
+
rows: block.rows.map((row) => row.map(cellRuns))
|
|
217
|
+
}
|
|
218
|
+
];
|
|
179
219
|
default:
|
|
180
220
|
return [];
|
|
181
221
|
}
|
|
@@ -192,7 +232,7 @@ function sameShape(a, b) {
|
|
|
192
232
|
const ALNUM = /[\p{L}\p{N}]/u;
|
|
193
233
|
const ENTITY = /^&(?:[A-Za-z][A-Za-z0-9]{0,31}|#[0-9]{1,7}|#[xX][0-9A-Fa-f]{1,6});/;
|
|
194
234
|
/** Backslash-escape only what Markdown would otherwise read as syntax. */
|
|
195
|
-
function escapeText(text, inLink) {
|
|
235
|
+
function escapeText(text, inLink, article = false) {
|
|
196
236
|
let out = '';
|
|
197
237
|
for (let index = 0; index < text.length; index++) {
|
|
198
238
|
const char = text[index];
|
|
@@ -218,6 +258,15 @@ function escapeText(text, inLink) {
|
|
|
218
258
|
case '~':
|
|
219
259
|
escape = before === '~' || after === '~';
|
|
220
260
|
break;
|
|
261
|
+
case '=':
|
|
262
|
+
// Exactly two could open or close a highlight, unless spaces surround them.
|
|
263
|
+
escape =
|
|
264
|
+
article &&
|
|
265
|
+
after === '=' &&
|
|
266
|
+
before !== '=' &&
|
|
267
|
+
text[index + 2] !== '=' &&
|
|
268
|
+
!(before !== undefined && /\s/.test(before) && /\s/.test(text[index + 2] ?? 'x'));
|
|
269
|
+
break;
|
|
221
270
|
case '`':
|
|
222
271
|
case '[':
|
|
223
272
|
escape = true;
|
|
@@ -240,11 +289,11 @@ function escapeAll(text) {
|
|
|
240
289
|
return text.replace(/[!-/:-@[-`{-~]/g, '\\$&');
|
|
241
290
|
}
|
|
242
291
|
/** URLs keep their own characters so that they are still found as links afterwards. */
|
|
243
|
-
function textMarkdown(text, inLink, aggressive) {
|
|
292
|
+
function textMarkdown(text, inLink, aggressive, article) {
|
|
244
293
|
if (aggressive)
|
|
245
294
|
return escapeAll(text);
|
|
246
295
|
if (inLink)
|
|
247
|
-
return escapeText(text, true);
|
|
296
|
+
return escapeText(text, true, article);
|
|
248
297
|
// Text next to a URL is escaped on its own, without the URL as a neighbor.
|
|
249
298
|
let out = '';
|
|
250
299
|
let chunk = '';
|
|
@@ -253,7 +302,7 @@ function textMarkdown(text, inLink, aggressive) {
|
|
|
253
302
|
chunk += token.raw;
|
|
254
303
|
continue;
|
|
255
304
|
}
|
|
256
|
-
out += escapeText(chunk, false);
|
|
305
|
+
out += escapeText(chunk, false, article);
|
|
257
306
|
chunk = '';
|
|
258
307
|
out +=
|
|
259
308
|
escapeText(token.raw, false) === token.raw
|
|
@@ -262,7 +311,7 @@ function textMarkdown(text, inLink, aggressive) {
|
|
|
262
311
|
? escapeText(token.raw, false)
|
|
263
312
|
: `<${token.raw}>`;
|
|
264
313
|
}
|
|
265
|
-
return out + escapeText(chunk, false);
|
|
314
|
+
return out + escapeText(chunk, false, article);
|
|
266
315
|
}
|
|
267
316
|
function codeSpan(value) {
|
|
268
317
|
let fence = '`';
|
|
@@ -273,6 +322,11 @@ function codeSpan(value) {
|
|
|
273
322
|
: '';
|
|
274
323
|
return value ? `${fence}${pad}${value}${pad}${fence}` : '';
|
|
275
324
|
}
|
|
325
|
+
/** A formula holding a dollar is written between two. */
|
|
326
|
+
function mathSpan(value) {
|
|
327
|
+
const fence = value.includes('$') ? '$$' : '$';
|
|
328
|
+
return value ? `${fence}${value}${fence}` : '';
|
|
329
|
+
}
|
|
276
330
|
function linkDestination(url) {
|
|
277
331
|
return /[\s()<>\\]/.test(url) ? `<${url.replace(/[<>\\]/g, '\\$&')}>` : url;
|
|
278
332
|
}
|
|
@@ -295,7 +349,7 @@ const PUNCTUATION = /^[\p{P}\p{S}]$/u;
|
|
|
295
349
|
function reference(char) {
|
|
296
350
|
return `&#x${char.codePointAt(0).toString(16)};`;
|
|
297
351
|
}
|
|
298
|
-
function lineMarkdown(runs, aggressive) {
|
|
352
|
+
function lineMarkdown(runs, aggressive, article = false) {
|
|
299
353
|
let out = '';
|
|
300
354
|
const stack = [];
|
|
301
355
|
// Set when formatting has just closed in a way that needs a non-word character after it.
|
|
@@ -324,7 +378,13 @@ function lineMarkdown(runs, aggressive) {
|
|
|
324
378
|
wanted.some((open) => open.mark === stack[keep].mark && open.url === stack[keep].url))
|
|
325
379
|
keep++;
|
|
326
380
|
close(keep);
|
|
327
|
-
let piece = run.
|
|
381
|
+
let piece = run.ref
|
|
382
|
+
? `[^${run.ref}]`
|
|
383
|
+
: run.math
|
|
384
|
+
? mathSpan(run.text)
|
|
385
|
+
: run.code
|
|
386
|
+
? codeSpan(run.text)
|
|
387
|
+
: textMarkdown(run.text, !!run.link, aggressive, article);
|
|
328
388
|
const first = Array.from(piece)[0] ?? '';
|
|
329
389
|
for (const open of wanted) {
|
|
330
390
|
if (stack.some((item) => item.mark === open.mark))
|
|
@@ -339,7 +399,13 @@ function lineMarkdown(runs, aggressive) {
|
|
|
339
399
|
if (WORD.test(before) && PUNCTUATION.test(first))
|
|
340
400
|
out = out.slice(0, -before.length) + reference(before);
|
|
341
401
|
// Next to an asterisk, underscores keep the two delimiter runs apart.
|
|
342
|
-
const char = open.mark === 'delete'
|
|
402
|
+
const char = open.mark === 'delete'
|
|
403
|
+
? '~'
|
|
404
|
+
: open.mark === 'highlight'
|
|
405
|
+
? '='
|
|
406
|
+
: out.endsWith('*')
|
|
407
|
+
? '_'
|
|
408
|
+
: '*';
|
|
343
409
|
open.close = char.repeat(open.mark === 'emphasis' ? 1 : 2);
|
|
344
410
|
out += open.close;
|
|
345
411
|
}
|
|
@@ -372,7 +438,46 @@ function isTight(items) {
|
|
|
372
438
|
(!item[1] || (item[1].type === 'list' && isTight(item[1].items))) &&
|
|
373
439
|
(item[0]?.type !== 'paragraph' || paragraphs(item[0].children).length <= 1));
|
|
374
440
|
}
|
|
375
|
-
|
|
441
|
+
const ALIGN = { left: ':--', center: ':-:', right: '--:' };
|
|
442
|
+
const alignMark = (align) => (align && ALIGN[align]) || '---';
|
|
443
|
+
/**
|
|
444
|
+
* In a table a pipe ends the cell unless a backslash escapes it, also inside code, where the
|
|
445
|
+
* backslash is otherwise content. Code cannot say "an odd number of backslashes, then a pipe",
|
|
446
|
+
* so such a pipe is written as text between two pieces of code.
|
|
447
|
+
*/
|
|
448
|
+
/** A pipe after an odd number of backslashes. */
|
|
449
|
+
const ODD_PIPE = /(?<=(?<!\\)(?:\\\\)*\\)(\|)/;
|
|
450
|
+
function cellSafe(runs) {
|
|
451
|
+
return runs.flatMap((run) => {
|
|
452
|
+
if (!run.code || !ODD_PIPE.test(run.text))
|
|
453
|
+
return [run];
|
|
454
|
+
return run.text
|
|
455
|
+
.split(ODD_PIPE)
|
|
456
|
+
.map((text) => (text === '|' ? { ...run, code: undefined, text } : { ...run, text }))
|
|
457
|
+
.filter((piece) => piece.text);
|
|
458
|
+
});
|
|
459
|
+
}
|
|
460
|
+
function tableMarkdown(block, aggressive, article) {
|
|
461
|
+
const width = Math.max(1, block.align.length, ...block.rows.map((row) => row.length));
|
|
462
|
+
const line = (cells) => `| ${cells.join(' | ')} |`;
|
|
463
|
+
const row = (cells) => line(Array.from({ length: width }, (_, index) =>
|
|
464
|
+
// Escape each pipe that is not escaped yet: one after an even number of backslashes.
|
|
465
|
+
lineMarkdown(cellSafe(cellRuns(cells[index] ?? [])), aggressive, article).replace(/(\\*)\|/g, (match, slashes) => (slashes.length % 2 ? match : `${slashes}\\|`))));
|
|
466
|
+
const [header = [], ...body] = block.rows;
|
|
467
|
+
return [
|
|
468
|
+
row(header),
|
|
469
|
+
line(Array.from({ length: width }, (_, index) => alignMark(block.align[index]))),
|
|
470
|
+
...body.map(row)
|
|
471
|
+
].join('\n');
|
|
472
|
+
}
|
|
473
|
+
function imageMarkdown(block) {
|
|
474
|
+
const title = block.title
|
|
475
|
+
? ` "${block.title.replace(/["\\]/g, '\\$&').replace(/\n+/g, ' ')}"`
|
|
476
|
+
: '';
|
|
477
|
+
const alt = escapeText((block.alt ?? '').replace(/\n+/g, ' '), true);
|
|
478
|
+
return `}${title})`;
|
|
479
|
+
}
|
|
480
|
+
function blocksMarkdown(blocks, aggressive, depth, inList = false, article = false) {
|
|
376
481
|
if (depth > MAX_DEPTH)
|
|
377
482
|
return [];
|
|
378
483
|
const result = [];
|
|
@@ -382,18 +487,23 @@ function blocksMarkdown(blocks, aggressive, depth, inList = false) {
|
|
|
382
487
|
switch (block.type) {
|
|
383
488
|
case 'paragraph':
|
|
384
489
|
for (const lines of paragraphs(block.children))
|
|
385
|
-
result.push(lines
|
|
490
|
+
result.push(lines
|
|
491
|
+
.map((line) => lineMarkdown(line, aggressive, article))
|
|
492
|
+
.join(article ? '\\\n' : '\n'));
|
|
386
493
|
break;
|
|
387
494
|
case 'heading': {
|
|
388
|
-
const text = lineMarkdown(headingRuns(paragraphs(block.children).flat()), aggressive);
|
|
495
|
+
const text = lineMarkdown(headingRuns(paragraphs(block.children).flat()), aggressive, article);
|
|
389
496
|
const depthMark = '#'.repeat(Math.min(6, Math.max(1, Math.trunc(block.depth) || 1)));
|
|
390
497
|
// A trailing run of # would be read as the optional closing sequence.
|
|
391
498
|
result.push(text ? `${depthMark} ${text.replace(/(^|\s)(#+)$/, '$1\\$2')}` : depthMark);
|
|
392
499
|
break;
|
|
393
500
|
}
|
|
394
501
|
case 'blockquote': {
|
|
395
|
-
const inner = blocksMarkdown(block.children, aggressive, depth + 1).join('\n\n');
|
|
396
|
-
|
|
502
|
+
const inner = blocksMarkdown(block.children, aggressive, depth + 1, false, article).join('\n\n');
|
|
503
|
+
// A callout opens with its marker; other blocks than text follow after an empty line.
|
|
504
|
+
const mark = article && isCallout(block.callout) ? `[!${block.callout.toUpperCase()}]` : undefined;
|
|
505
|
+
const gap = block.children[0]?.type === 'paragraph' ? '\n' : '\n\n';
|
|
506
|
+
result.push(indent(mark ? (inner ? mark + gap + inner : mark) : inner, '> ', '> '));
|
|
397
507
|
break;
|
|
398
508
|
}
|
|
399
509
|
case 'list': {
|
|
@@ -403,13 +513,16 @@ function blocksMarkdown(blocks, aggressive, depth, inList = false) {
|
|
|
403
513
|
alternate: previousList?.ordered === block.ordered && !previousList.alternate
|
|
404
514
|
};
|
|
405
515
|
const tight = isTight(block.items);
|
|
516
|
+
const checked = tasks(block);
|
|
406
517
|
const items = block.items.map((item, index) => {
|
|
407
518
|
const marker = block.ordered
|
|
408
519
|
? `${(block.start ?? 1) + index}${list.alternate ? ')' : '.'}`
|
|
409
520
|
: list.alternate
|
|
410
521
|
? '*'
|
|
411
522
|
: '-';
|
|
412
|
-
const
|
|
523
|
+
const task = checked?.[index];
|
|
524
|
+
const inner = (typeof task === 'boolean' ? (task ? '[x] ' : '[ ] ') : '') +
|
|
525
|
+
blocksMarkdown(item, aggressive, depth + 1, true, article).join(tight ? '\n' : '\n\n');
|
|
413
526
|
return inner ? indent(inner, `${marker} `, ' '.repeat(marker.length + 1)) : marker;
|
|
414
527
|
});
|
|
415
528
|
result.push(items.join(tight ? '\n' : '\n\n'));
|
|
@@ -427,6 +540,22 @@ function blocksMarkdown(blocks, aggressive, depth, inList = false) {
|
|
|
427
540
|
// `- ---` would itself be a thematic break.
|
|
428
541
|
result.push(inList ? '***' : '---');
|
|
429
542
|
break;
|
|
543
|
+
case 'image':
|
|
544
|
+
result.push(imageMarkdown(block));
|
|
545
|
+
break;
|
|
546
|
+
case 'math':
|
|
547
|
+
result.push(`$$\n${block.value}\n$$`);
|
|
548
|
+
break;
|
|
549
|
+
case 'footnoteDefinition': {
|
|
550
|
+
if (!FOOTNOTE_ID.test(block.id))
|
|
551
|
+
break;
|
|
552
|
+
const inner = blocksMarkdown(block.children, aggressive, depth + 1, false, article).join('\n\n');
|
|
553
|
+
result.push(indent(inner, `[^${block.id}]: `, ' '));
|
|
554
|
+
break;
|
|
555
|
+
}
|
|
556
|
+
case 'table':
|
|
557
|
+
result.push(tableMarkdown(block, aggressive, article));
|
|
558
|
+
break;
|
|
430
559
|
}
|
|
431
560
|
previousList = list;
|
|
432
561
|
}
|
|
@@ -439,22 +568,25 @@ function blocksMarkdown(blocks, aggressive, depth, inList = false) {
|
|
|
439
568
|
* their text; a link labeled with its own URL is written as that URL. Output that fails to
|
|
440
569
|
* read back (unusual punctuation next to formatting) is escaped more heavily instead.
|
|
441
570
|
*/
|
|
442
|
-
export function richTextToMarkdown(content) {
|
|
571
|
+
export function richTextToMarkdown(content, options = {}) {
|
|
572
|
+
const article = !!options.article;
|
|
443
573
|
const blocks = Array.isArray(content.children) ? content.children : [];
|
|
444
|
-
const
|
|
445
|
-
|
|
574
|
+
const write = (part, aggressive) => blocksMarkdown(part, aggressive, 0, false, article).join('\n\n');
|
|
575
|
+
const minimal = write(blocks, false);
|
|
576
|
+
if (exceedsMarkdownLimits(minimal, article) || reads(minimal, blocks, article))
|
|
446
577
|
return minimal;
|
|
447
578
|
// Find the blocks that did not survive and escape only those.
|
|
448
579
|
return blocks
|
|
449
580
|
.flatMap((block) => {
|
|
450
|
-
const text =
|
|
451
|
-
return reads(text, [block]) ? text :
|
|
581
|
+
const text = write([block], false);
|
|
582
|
+
return reads(text, [block], article) ? text : write([block], true);
|
|
452
583
|
})
|
|
453
584
|
.filter(Boolean)
|
|
454
585
|
.join('\n\n');
|
|
455
586
|
}
|
|
456
|
-
function reads(markdown, blocks) {
|
|
457
|
-
|
|
587
|
+
function reads(markdown, blocks, article) {
|
|
588
|
+
const parsed = parseMarkdown(markdown, article ? { article, allowImage: () => true } : {});
|
|
589
|
+
return sameShape(parsed.children, blocks);
|
|
458
590
|
}
|
|
459
591
|
/** Whether two documents render the same, ignoring how links, mentions and tags were found. */
|
|
460
592
|
export function sameRichText(a, b) {
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* CommonMark plus ~~strikethrough
|
|
3
|
-
*
|
|
2
|
+
* CommonMark plus ~~strikethrough~~ and, with `article`, GFM tables, footnotes and task lists,
|
|
3
|
+
* `$math$` and `==highlights==` (as `highlight` nodes), as an mdast `Root` with source
|
|
4
|
+
* positions. The parser is bundled here, so apps need no bundler configuration for it.
|
|
4
5
|
*/
|
|
5
|
-
export function markdownTree(text: string): unknown;
|
|
6
|
+
export function markdownTree(text: string, options?: { article?: boolean }): unknown;
|