@atmo-dev/kit 0.14.0 → 0.15.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/components/ExternalEmbed.svelte +30 -29
- package/dist/components/ExternalEmbed.svelte.d.ts +8 -0
- package/dist/components/InlineEditor.svelte +15 -95
- package/dist/components/InlineEditor.svelte.d.ts +1 -1
- package/dist/components/LinkCard.svelte +54 -25
- package/dist/components/LinkCard.svelte.d.ts +2 -0
- package/dist/components/LinkEmbed.svelte +75 -0
- package/dist/components/LinkEmbed.svelte.d.ts +13 -0
- package/dist/components/PostContent.svelte +12 -3
- package/dist/components/PostContent.svelte.d.ts +3 -0
- package/dist/components/RichText.svelte +47 -5
- package/dist/components/RichText.svelte.d.ts +5 -2
- package/dist/components/RichTextEditor.svelte +272 -0
- package/dist/components/RichTextEditor.svelte.d.ts +44 -0
- package/dist/components/composer/AltText.svelte +63 -0
- package/dist/components/composer/AltText.svelte.d.ts +11 -0
- package/dist/components/composer/CharRing.svelte +39 -0
- package/dist/components/composer/CharRing.svelte.d.ts +7 -0
- package/dist/components/composer/ComposerPost.svelte +321 -0
- package/dist/components/composer/ComposerPost.svelte.d.ts +29 -0
- package/dist/components/composer/Drafts.svelte +82 -0
- package/dist/components/composer/Drafts.svelte.d.ts +9 -0
- package/dist/components/composer/EmojiPicker.svelte +55 -0
- package/dist/components/composer/EmojiPicker.svelte.d.ts +7 -0
- package/dist/components/composer/GifPicker.svelte +118 -0
- package/dist/components/composer/GifPicker.svelte.d.ts +11 -0
- package/dist/components/composer/PostComposer.svelte +599 -0
- package/dist/components/composer/PostComposer.svelte.d.ts +71 -0
- package/dist/components/composer/PostComposerDialog.svelte +75 -0
- package/dist/components/composer/PostComposerDialog.svelte.d.ts +10 -0
- package/dist/components/composer/ReplySettings.svelte +150 -0
- package/dist/components/composer/ReplySettings.svelte.d.ts +18 -0
- package/dist/components/login/search.d.ts +29 -1
- package/dist/components/login/search.js +31 -3
- package/dist/editor/EditorLinkEmbed.svelte +35 -0
- package/dist/editor/EditorLinkEmbed.svelte.d.ts +12 -0
- package/dist/editor/detect-plugin.d.ts +5 -0
- package/dist/editor/detect-plugin.js +51 -0
- package/dist/editor/document.d.ts +17 -0
- package/dist/editor/document.js +269 -0
- package/dist/editor/link-embed-node.d.ts +17 -0
- package/dist/editor/link-embed-node.js +96 -0
- package/dist/editor/markdown-extensions.d.ts +6 -0
- package/dist/editor/markdown-extensions.js +42 -0
- package/dist/editor/markdown.d.ts +3 -0
- package/dist/editor/markdown.js +4 -0
- package/dist/embeds/PlayerLinkEmbed.svelte +9 -0
- package/dist/embeds/PlayerLinkEmbed.svelte.d.ts +7 -0
- package/dist/{events/event-view → embeds}/PostLinkEmbed.svelte +7 -7
- package/dist/{events/event-view → embeds}/PostLinkEmbed.svelte.d.ts +1 -1
- package/dist/{events/event-view → embeds}/ProfileLinkEmbed.svelte +7 -7
- package/dist/{events/event-view → embeds}/ProfileLinkEmbed.svelte.d.ts +1 -1
- package/dist/embeds/context.d.ts +10 -0
- package/dist/embeds/context.js +20 -0
- package/dist/embeds/defaults.d.ts +27 -0
- package/dist/embeds/defaults.js +38 -0
- package/dist/embeds/resolve.d.ts +9 -0
- package/dist/embeds/resolve.js +27 -0
- package/dist/embeds/types.d.ts +39 -0
- package/dist/embeds/types.js +1 -0
- package/dist/events/EventEditor.svelte +29 -6
- package/dist/events/EventView.svelte +21 -38
- package/dist/events/PostToBlueskyModal.svelte +12 -4
- package/dist/events/event-link-embed.d.ts +22 -0
- package/dist/events/event-link-embed.js +15 -0
- package/dist/events/event-view/EventLinkEmbed.svelte +12 -30
- package/dist/events/event-view/EventLinkEmbed.svelte.d.ts +3 -12
- package/dist/events.d.ts +2 -0
- package/dist/events.js +1 -0
- package/dist/index.d.ts +16 -2
- package/dist/index.js +10 -2
- package/dist/richtext/detect.js +1 -1
- package/dist/richtext/document.d.ts +10 -0
- package/dist/richtext/document.js +44 -4
- package/dist/richtext/markdown.js +9 -8
- package/dist/richtext/serialize.d.ts +11 -0
- package/dist/richtext/serialize.js +462 -0
- package/dist/richtext/vendor/markdown-tree.d.ts +5 -0
- package/dist/richtext/vendor/markdown-tree.js +11032 -0
- package/dist/richtext.d.ts +1 -0
- package/dist/richtext.js +1 -0
- package/dist/social/MentionOptions.svelte +30 -0
- package/dist/social/MentionOptions.svelte.d.ts +12 -0
- package/dist/social/composer.d.ts +143 -0
- package/dist/social/composer.js +141 -0
- package/dist/social/{mention-suggestions.d.ts → mention-suggestions.svelte.d.ts} +1 -1
- package/dist/social/{mention-suggestions.js → mention-suggestions.svelte.js} +36 -46
- package/dist/social/richtext.js +2 -0
- package/dist/social.d.ts +2 -0
- package/dist/social.js +1 -0
- package/dist/styles.d.ts +5 -0
- package/dist/styles.js +5 -0
- package/package.json +8 -4
- package/dist/components/PostComposer.svelte +0 -41
- package/dist/components/PostComposer.svelte.d.ts +0 -16
|
@@ -0,0 +1,462 @@
|
|
|
1
|
+
import { detectTokens } from './detect.js';
|
|
2
|
+
import { safeWebUrl } from './document.js';
|
|
3
|
+
import { exceedsMarkdownLimits, parseMarkdown } from './markdown.js';
|
|
4
|
+
const MAX_DEPTH = 32;
|
|
5
|
+
const STYLES = ['strong', 'emphasis', 'delete'];
|
|
6
|
+
function sameMarks(a, b) {
|
|
7
|
+
return (!a.br === !b.br &&
|
|
8
|
+
!a.strong === !b.strong &&
|
|
9
|
+
!a.emphasis === !b.emphasis &&
|
|
10
|
+
!a.delete === !b.delete &&
|
|
11
|
+
!a.code === !b.code &&
|
|
12
|
+
a.link === b.link);
|
|
13
|
+
}
|
|
14
|
+
function merge(runs) {
|
|
15
|
+
const result = [];
|
|
16
|
+
for (const run of runs) {
|
|
17
|
+
if (!run.br && !run.text)
|
|
18
|
+
continue;
|
|
19
|
+
const previous = result.at(-1);
|
|
20
|
+
if (previous && !run.br && !previous.br && sameMarks(previous, run))
|
|
21
|
+
previous.text += run.text;
|
|
22
|
+
else
|
|
23
|
+
result.push({ ...run });
|
|
24
|
+
}
|
|
25
|
+
return result;
|
|
26
|
+
}
|
|
27
|
+
function plainText(nodes, depth = 0) {
|
|
28
|
+
if (depth > MAX_DEPTH)
|
|
29
|
+
return '';
|
|
30
|
+
return nodes
|
|
31
|
+
.map((node) => node.type === 'text' || node.type === 'code'
|
|
32
|
+
? node.value
|
|
33
|
+
: node.type === 'break'
|
|
34
|
+
? '\n'
|
|
35
|
+
: node.type === 'mention' || node.type === 'tag'
|
|
36
|
+
? node.text
|
|
37
|
+
: plainText(node.children, depth + 1))
|
|
38
|
+
.join('');
|
|
39
|
+
}
|
|
40
|
+
/** A link written as its own destination is plain text again once it is serialized. */
|
|
41
|
+
function isAutolink(node) {
|
|
42
|
+
if (node.children.some((child) => child.type !== 'text'))
|
|
43
|
+
return false;
|
|
44
|
+
const label = plainText(node.children);
|
|
45
|
+
return label === node.url || (!/\s/.test(label) && safeWebUrl(label) === node.url);
|
|
46
|
+
}
|
|
47
|
+
function flatten(nodes, marks, depth = 0) {
|
|
48
|
+
if (depth > MAX_DEPTH)
|
|
49
|
+
return [];
|
|
50
|
+
return nodes.flatMap((node) => {
|
|
51
|
+
switch (node.type) {
|
|
52
|
+
case 'text':
|
|
53
|
+
return node.value
|
|
54
|
+
.split('\n')
|
|
55
|
+
.flatMap((text, index) => [
|
|
56
|
+
...(index ? [{ text: '', br: true }] : []),
|
|
57
|
+
{ ...marks, text }
|
|
58
|
+
]);
|
|
59
|
+
case 'break':
|
|
60
|
+
return [{ text: '', br: true }];
|
|
61
|
+
case 'code':
|
|
62
|
+
return [{ ...marks, code: true, text: node.value.replace(/\n/g, ' ') }];
|
|
63
|
+
case 'mention':
|
|
64
|
+
case 'tag':
|
|
65
|
+
return [{ ...marks, text: node.text.replace(/\n/g, ' ') }];
|
|
66
|
+
case 'link':
|
|
67
|
+
return flatten(node.children, marks.link || isAutolink(node) ? marks : { ...marks, link: node.url }, depth + 1);
|
|
68
|
+
case 'strong':
|
|
69
|
+
case 'emphasis':
|
|
70
|
+
case 'delete':
|
|
71
|
+
return flatten(node.children, { ...marks, [node.type]: true }, depth + 1);
|
|
72
|
+
default:
|
|
73
|
+
return [];
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Markdown cannot put whitespace directly inside `**` or at the edges of a line, so it moves
|
|
79
|
+
* out of the formatting and is dropped at line edges. Rendering is unaffected.
|
|
80
|
+
*/
|
|
81
|
+
function hoistWhitespace(input) {
|
|
82
|
+
let runs = merge(input);
|
|
83
|
+
const result = [];
|
|
84
|
+
runs.forEach((run, index) => {
|
|
85
|
+
if (run.br || run.code || !STYLES.some((style) => run[style]))
|
|
86
|
+
return void result.push(run);
|
|
87
|
+
const previous = runs[index - 1];
|
|
88
|
+
const next = runs[index + 1];
|
|
89
|
+
const keep = (...neighbors) => {
|
|
90
|
+
const piece = { text: '', link: run.link };
|
|
91
|
+
for (const style of STYLES)
|
|
92
|
+
if (run[style] &&
|
|
93
|
+
neighbors.every((neighbor) => neighbor && !neighbor.br && neighbor[style]))
|
|
94
|
+
piece[style] = true;
|
|
95
|
+
return piece;
|
|
96
|
+
};
|
|
97
|
+
const [, leading, core, trailing] = /^(\s*)([\s\S]*?)(\s*)$/.exec(run.text);
|
|
98
|
+
if (!core)
|
|
99
|
+
return void result.push({ ...keep(previous, next), text: run.text });
|
|
100
|
+
if (leading)
|
|
101
|
+
result.push({ ...keep(previous), text: leading });
|
|
102
|
+
result.push({ ...run, text: core });
|
|
103
|
+
if (trailing)
|
|
104
|
+
result.push({ ...keep(next), text: trailing });
|
|
105
|
+
});
|
|
106
|
+
runs = merge(result);
|
|
107
|
+
// Drop whitespace at the start and end of every line, until nothing is left to drop.
|
|
108
|
+
for (;;) {
|
|
109
|
+
const trimmed = [];
|
|
110
|
+
runs.forEach((run, index) => {
|
|
111
|
+
if (run.br || run.code)
|
|
112
|
+
return void trimmed.push(run);
|
|
113
|
+
let text = run.text;
|
|
114
|
+
const previous = runs[index - 1];
|
|
115
|
+
const next = runs[index + 1];
|
|
116
|
+
if (!previous || previous.br)
|
|
117
|
+
text = text.replace(/^\s+/, '');
|
|
118
|
+
if (!next || next.br)
|
|
119
|
+
text = text.replace(/\s+$/, '');
|
|
120
|
+
if (text)
|
|
121
|
+
trimmed.push({ ...run, text });
|
|
122
|
+
});
|
|
123
|
+
const merged = merge(trimmed);
|
|
124
|
+
if (merged.length === runs.length &&
|
|
125
|
+
merged.every((run, index) => run.text === runs[index].text))
|
|
126
|
+
return merged;
|
|
127
|
+
runs = merged;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
/** Lines of one paragraph. An empty line separates paragraphs. */
|
|
131
|
+
function paragraphs(nodes) {
|
|
132
|
+
const result = [];
|
|
133
|
+
let lines = [];
|
|
134
|
+
let line = [];
|
|
135
|
+
const endLine = () => {
|
|
136
|
+
if (line.length)
|
|
137
|
+
lines.push(line);
|
|
138
|
+
else if (lines.length) {
|
|
139
|
+
result.push(lines);
|
|
140
|
+
lines = [];
|
|
141
|
+
}
|
|
142
|
+
line = [];
|
|
143
|
+
};
|
|
144
|
+
for (const run of hoistWhitespace(flatten(nodes, {}))) {
|
|
145
|
+
if (run.br)
|
|
146
|
+
endLine();
|
|
147
|
+
else
|
|
148
|
+
line.push(run);
|
|
149
|
+
}
|
|
150
|
+
endLine();
|
|
151
|
+
if (lines.length)
|
|
152
|
+
result.push(lines);
|
|
153
|
+
return result;
|
|
154
|
+
}
|
|
155
|
+
function shapeOf(blocks, depth = 0) {
|
|
156
|
+
if (depth > MAX_DEPTH)
|
|
157
|
+
return [];
|
|
158
|
+
return blocks.flatMap((block) => {
|
|
159
|
+
switch (block.type) {
|
|
160
|
+
case 'paragraph':
|
|
161
|
+
return paragraphs(block.children).map((lines) => ({ p: lines }));
|
|
162
|
+
case 'heading': {
|
|
163
|
+
const lines = paragraphs(block.children).flat();
|
|
164
|
+
return [{ h: block.depth, runs: headingRuns(lines) }];
|
|
165
|
+
}
|
|
166
|
+
case 'blockquote':
|
|
167
|
+
return [{ q: shapeOf(block.children, depth + 1) }];
|
|
168
|
+
case 'list':
|
|
169
|
+
return [
|
|
170
|
+
{
|
|
171
|
+
l: block.ordered ? (block.start ?? 1) : false,
|
|
172
|
+
items: block.items.map((item) => shapeOf(item, depth + 1))
|
|
173
|
+
}
|
|
174
|
+
];
|
|
175
|
+
case 'codeBlock':
|
|
176
|
+
return [{ c: block.value, lang: block.lang ?? '' }];
|
|
177
|
+
case 'thematicBreak':
|
|
178
|
+
return [{ hr: true }];
|
|
179
|
+
default:
|
|
180
|
+
return [];
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
function headingRuns(lines) {
|
|
185
|
+
return merge(lines.flatMap((line, index) => [...(index ? [{ text: ' ' }] : []), ...line]));
|
|
186
|
+
}
|
|
187
|
+
function sameShape(a, b) {
|
|
188
|
+
const key = (_, value) => (value === false ? undefined : value);
|
|
189
|
+
return JSON.stringify(shapeOf(a), key) === JSON.stringify(shapeOf(b), key);
|
|
190
|
+
}
|
|
191
|
+
// --- Inline -----------------------------------------------------------------------------
|
|
192
|
+
const ALNUM = /[\p{L}\p{N}]/u;
|
|
193
|
+
const ENTITY = /^&(?:[A-Za-z][A-Za-z0-9]{0,31}|#[0-9]{1,7}|#[xX][0-9A-Fa-f]{1,6});/;
|
|
194
|
+
/** Backslash-escape only what Markdown would otherwise read as syntax. */
|
|
195
|
+
function escapeText(text, inLink) {
|
|
196
|
+
let out = '';
|
|
197
|
+
for (let index = 0; index < text.length; index++) {
|
|
198
|
+
const char = text[index];
|
|
199
|
+
const before = text[index - 1];
|
|
200
|
+
const after = text[index + 1];
|
|
201
|
+
let escape = false;
|
|
202
|
+
switch (char) {
|
|
203
|
+
case '\\':
|
|
204
|
+
escape = after === undefined || /[!-/:-@[-`{-~]/.test(after);
|
|
205
|
+
break;
|
|
206
|
+
case '*':
|
|
207
|
+
escape = !(before !== undefined &&
|
|
208
|
+
after !== undefined &&
|
|
209
|
+
/\s/.test(before) &&
|
|
210
|
+
/\s/.test(after));
|
|
211
|
+
break;
|
|
212
|
+
case '_':
|
|
213
|
+
escape = !(before !== undefined &&
|
|
214
|
+
after !== undefined &&
|
|
215
|
+
ALNUM.test(before) &&
|
|
216
|
+
ALNUM.test(after));
|
|
217
|
+
break;
|
|
218
|
+
case '~':
|
|
219
|
+
escape = before === '~' || after === '~';
|
|
220
|
+
break;
|
|
221
|
+
case '`':
|
|
222
|
+
case '[':
|
|
223
|
+
escape = true;
|
|
224
|
+
break;
|
|
225
|
+
case ']':
|
|
226
|
+
escape = inLink;
|
|
227
|
+
break;
|
|
228
|
+
case '<':
|
|
229
|
+
escape = after !== undefined && /[A-Za-z/!?]/.test(after);
|
|
230
|
+
break;
|
|
231
|
+
case '&':
|
|
232
|
+
escape = ENTITY.test(text.slice(index));
|
|
233
|
+
break;
|
|
234
|
+
}
|
|
235
|
+
out += escape ? `\\${char}` : char;
|
|
236
|
+
}
|
|
237
|
+
return out;
|
|
238
|
+
}
|
|
239
|
+
function escapeAll(text) {
|
|
240
|
+
return text.replace(/[!-/:-@[-`{-~]/g, '\\$&');
|
|
241
|
+
}
|
|
242
|
+
/** URLs keep their own characters so that they are still found as links afterwards. */
|
|
243
|
+
function textMarkdown(text, inLink, aggressive) {
|
|
244
|
+
if (aggressive)
|
|
245
|
+
return escapeAll(text);
|
|
246
|
+
if (inLink)
|
|
247
|
+
return escapeText(text, true);
|
|
248
|
+
// Text next to a URL is escaped on its own, without the URL as a neighbor.
|
|
249
|
+
let out = '';
|
|
250
|
+
let chunk = '';
|
|
251
|
+
for (const token of detectTokens(text)) {
|
|
252
|
+
if (token.type !== 'autolink') {
|
|
253
|
+
chunk += token.raw;
|
|
254
|
+
continue;
|
|
255
|
+
}
|
|
256
|
+
out += escapeText(chunk, false);
|
|
257
|
+
chunk = '';
|
|
258
|
+
out +=
|
|
259
|
+
escapeText(token.raw, false) === token.raw
|
|
260
|
+
? token.raw
|
|
261
|
+
: /[<>\s]/.test(token.raw)
|
|
262
|
+
? escapeText(token.raw, false)
|
|
263
|
+
: `<${token.raw}>`;
|
|
264
|
+
}
|
|
265
|
+
return out + escapeText(chunk, false);
|
|
266
|
+
}
|
|
267
|
+
function codeSpan(value) {
|
|
268
|
+
let fence = '`';
|
|
269
|
+
while (value.includes(fence))
|
|
270
|
+
fence += '`';
|
|
271
|
+
const pad = value.startsWith('`') || value.endsWith('`') || (/^ .* $/.test(value) && value.trim())
|
|
272
|
+
? ' '
|
|
273
|
+
: '';
|
|
274
|
+
return value ? `${fence}${pad}${value}${pad}${fence}` : '';
|
|
275
|
+
}
|
|
276
|
+
function linkDestination(url) {
|
|
277
|
+
return /[\s()<>\\]/.test(url) ? `<${url.replace(/[<>\\]/g, '\\$&')}>` : url;
|
|
278
|
+
}
|
|
279
|
+
/** Escape text that would start a block when it begins a line. */
|
|
280
|
+
function escapeLineStart(line, plain) {
|
|
281
|
+
if (/^(?:=+|-+)\s*$/.test(plain) || /^([-_*])(?:\s*\1){2,}\s*$/.test(plain))
|
|
282
|
+
return line.startsWith('\\') ? line : `\\${line}`;
|
|
283
|
+
if (/^#{1,6}(?:\s|$)/.test(line) || /^>/.test(line) || /^[-+*](?:\s|$)/.test(line))
|
|
284
|
+
return `\\${line}`;
|
|
285
|
+
const ordered = /^(\d{1,9})([.)])(?=\s|$)/.exec(line);
|
|
286
|
+
if (ordered)
|
|
287
|
+
return `${ordered[1]}\\${line.slice(ordered[1].length)}`;
|
|
288
|
+
return line;
|
|
289
|
+
}
|
|
290
|
+
// Parsers disagree on whether symbols outside ASCII, such as emoji, are punctuation, so they
|
|
291
|
+
// count as both.
|
|
292
|
+
const WORD = /^[^\s\p{P}!-/:-@[-`{-~]$/u;
|
|
293
|
+
const PUNCTUATION = /^[\p{P}\p{S}]$/u;
|
|
294
|
+
/** A character reference reads as the same character, but counts as punctuation next to `**`. */
|
|
295
|
+
function reference(char) {
|
|
296
|
+
return `&#x${char.codePointAt(0).toString(16)};`;
|
|
297
|
+
}
|
|
298
|
+
function lineMarkdown(runs, aggressive) {
|
|
299
|
+
let out = '';
|
|
300
|
+
const stack = [];
|
|
301
|
+
// Set when formatting has just closed in a way that needs a non-word character after it.
|
|
302
|
+
let boundary = false;
|
|
303
|
+
const lastChar = () => Array.from(out.slice(-2)).at(-1) ?? '';
|
|
304
|
+
const close = (count) => {
|
|
305
|
+
while (stack.length > count) {
|
|
306
|
+
const open = stack.pop();
|
|
307
|
+
// `**Hi!**there` does not close: after punctuation, a closing run must not be followed
|
|
308
|
+
// by a word character. Underscores never close before one.
|
|
309
|
+
if (open.mark !== 'link' && (PUNCTUATION.test(lastChar()) || open.close.startsWith('_')))
|
|
310
|
+
boundary = true;
|
|
311
|
+
out += open.close;
|
|
312
|
+
}
|
|
313
|
+
};
|
|
314
|
+
for (const run of runs) {
|
|
315
|
+
const wanted = [];
|
|
316
|
+
if (run.link)
|
|
317
|
+
wanted.push({ mark: 'link', close: `](${linkDestination(run.link)})`, url: run.link });
|
|
318
|
+
for (const style of STYLES)
|
|
319
|
+
if (run[style])
|
|
320
|
+
wanted.push({ mark: style, close: '' });
|
|
321
|
+
// Keep what continues from the previous run, close the rest.
|
|
322
|
+
let keep = 0;
|
|
323
|
+
while (keep < stack.length &&
|
|
324
|
+
wanted.some((open) => open.mark === stack[keep].mark && open.url === stack[keep].url))
|
|
325
|
+
keep++;
|
|
326
|
+
close(keep);
|
|
327
|
+
let piece = run.code ? codeSpan(run.text) : textMarkdown(run.text, !!run.link, aggressive);
|
|
328
|
+
const first = Array.from(piece)[0] ?? '';
|
|
329
|
+
for (const open of wanted) {
|
|
330
|
+
if (stack.some((item) => item.mark === open.mark))
|
|
331
|
+
continue;
|
|
332
|
+
boundary = false;
|
|
333
|
+
if (open.mark === 'link')
|
|
334
|
+
out += '[';
|
|
335
|
+
else {
|
|
336
|
+
// `a**(b)**` does not open: before punctuation, an opening run must not follow a
|
|
337
|
+
// word character.
|
|
338
|
+
const before = lastChar();
|
|
339
|
+
if (WORD.test(before) && PUNCTUATION.test(first))
|
|
340
|
+
out = out.slice(0, -before.length) + reference(before);
|
|
341
|
+
// Next to an asterisk, underscores keep the two delimiter runs apart.
|
|
342
|
+
const char = open.mark === 'delete' ? '~' : out.endsWith('*') ? '_' : '*';
|
|
343
|
+
open.close = char.repeat(open.mark === 'emphasis' ? 1 : 2);
|
|
344
|
+
out += open.close;
|
|
345
|
+
}
|
|
346
|
+
stack.push(open);
|
|
347
|
+
}
|
|
348
|
+
if (boundary && WORD.test(first))
|
|
349
|
+
piece = reference(first) + piece.slice(first.length);
|
|
350
|
+
boundary = false;
|
|
351
|
+
out += piece;
|
|
352
|
+
}
|
|
353
|
+
close(0);
|
|
354
|
+
const first = runs[0];
|
|
355
|
+
const plain = runs.every((run) => !run.code && !run.link && !STYLES.some((style) => run[style]))
|
|
356
|
+
? runs.map((run) => run.text).join('')
|
|
357
|
+
: '';
|
|
358
|
+
return first && !first.code && !first.link && !STYLES.some((style) => first[style]) && !aggressive
|
|
359
|
+
? escapeLineStart(out, plain)
|
|
360
|
+
: out;
|
|
361
|
+
}
|
|
362
|
+
// --- Blocks -----------------------------------------------------------------------------
|
|
363
|
+
function indent(text, first, rest) {
|
|
364
|
+
return text
|
|
365
|
+
.split('\n')
|
|
366
|
+
.map((line, index) => (index ? (line ? rest + line : rest.trimEnd()) : first + line))
|
|
367
|
+
.join('\n');
|
|
368
|
+
}
|
|
369
|
+
function isTight(items) {
|
|
370
|
+
return items.every((item) => item.length <= 2 &&
|
|
371
|
+
(!item[0] || item[0].type === 'paragraph') &&
|
|
372
|
+
(!item[1] || (item[1].type === 'list' && isTight(item[1].items))) &&
|
|
373
|
+
(item[0]?.type !== 'paragraph' || paragraphs(item[0].children).length <= 1));
|
|
374
|
+
}
|
|
375
|
+
function blocksMarkdown(blocks, aggressive, depth, inList = false) {
|
|
376
|
+
if (depth > MAX_DEPTH)
|
|
377
|
+
return [];
|
|
378
|
+
const result = [];
|
|
379
|
+
let previousList;
|
|
380
|
+
for (const block of blocks) {
|
|
381
|
+
let list;
|
|
382
|
+
switch (block.type) {
|
|
383
|
+
case 'paragraph':
|
|
384
|
+
for (const lines of paragraphs(block.children))
|
|
385
|
+
result.push(lines.map((line) => lineMarkdown(line, aggressive)).join('\n'));
|
|
386
|
+
break;
|
|
387
|
+
case 'heading': {
|
|
388
|
+
const text = lineMarkdown(headingRuns(paragraphs(block.children).flat()), aggressive);
|
|
389
|
+
const depthMark = '#'.repeat(Math.min(6, Math.max(1, Math.trunc(block.depth) || 1)));
|
|
390
|
+
// A trailing run of # would be read as the optional closing sequence.
|
|
391
|
+
result.push(text ? `${depthMark} ${text.replace(/(^|\s)(#+)$/, '$1\\$2')}` : depthMark);
|
|
392
|
+
break;
|
|
393
|
+
}
|
|
394
|
+
case 'blockquote': {
|
|
395
|
+
const inner = blocksMarkdown(block.children, aggressive, depth + 1).join('\n\n');
|
|
396
|
+
result.push(indent(inner, '> ', '> '));
|
|
397
|
+
break;
|
|
398
|
+
}
|
|
399
|
+
case 'list': {
|
|
400
|
+
// Two lists in a row need different markers, or they would read as one list.
|
|
401
|
+
list = {
|
|
402
|
+
ordered: block.ordered,
|
|
403
|
+
alternate: previousList?.ordered === block.ordered && !previousList.alternate
|
|
404
|
+
};
|
|
405
|
+
const tight = isTight(block.items);
|
|
406
|
+
const items = block.items.map((item, index) => {
|
|
407
|
+
const marker = block.ordered
|
|
408
|
+
? `${(block.start ?? 1) + index}${list.alternate ? ')' : '.'}`
|
|
409
|
+
: list.alternate
|
|
410
|
+
? '*'
|
|
411
|
+
: '-';
|
|
412
|
+
const inner = blocksMarkdown(item, aggressive, depth + 1, true).join(tight ? '\n' : '\n\n');
|
|
413
|
+
return inner ? indent(inner, `${marker} `, ' '.repeat(marker.length + 1)) : marker;
|
|
414
|
+
});
|
|
415
|
+
result.push(items.join(tight ? '\n' : '\n\n'));
|
|
416
|
+
break;
|
|
417
|
+
}
|
|
418
|
+
case 'codeBlock': {
|
|
419
|
+
let fence = '```';
|
|
420
|
+
while (block.value.includes(fence))
|
|
421
|
+
fence += '`';
|
|
422
|
+
const lang = (block.lang ?? '').replace(/[`\s]/g, '');
|
|
423
|
+
result.push(`${fence}${lang}\n${block.value ? `${block.value}\n` : ''}${fence}`);
|
|
424
|
+
break;
|
|
425
|
+
}
|
|
426
|
+
case 'thematicBreak':
|
|
427
|
+
// `- ---` would itself be a thematic break.
|
|
428
|
+
result.push(inList ? '***' : '---');
|
|
429
|
+
break;
|
|
430
|
+
}
|
|
431
|
+
previousList = list;
|
|
432
|
+
}
|
|
433
|
+
return result;
|
|
434
|
+
}
|
|
435
|
+
/**
|
|
436
|
+
* A document as Markdown that `parseRichText(text, { markdown: true })` reads back as the same
|
|
437
|
+
* document. Only characters that would otherwise be syntax are escaped, so plain links,
|
|
438
|
+
* mentions and hashtags stay detectable in the source. Mentions and hashtags are written as
|
|
439
|
+
* their text; a link labeled with its own URL is written as that URL. Output that fails to
|
|
440
|
+
* read back (unusual punctuation next to formatting) is escaped more heavily instead.
|
|
441
|
+
*/
|
|
442
|
+
export function richTextToMarkdown(content) {
|
|
443
|
+
const blocks = Array.isArray(content.children) ? content.children : [];
|
|
444
|
+
const minimal = blocksMarkdown(blocks, false, 0).join('\n\n');
|
|
445
|
+
if (exceedsMarkdownLimits(minimal) || reads(minimal, blocks))
|
|
446
|
+
return minimal;
|
|
447
|
+
// Find the blocks that did not survive and escape only those.
|
|
448
|
+
return blocks
|
|
449
|
+
.flatMap((block) => {
|
|
450
|
+
const text = blocksMarkdown([block], false, 0).join('\n\n');
|
|
451
|
+
return reads(text, [block]) ? text : blocksMarkdown([block], true, 0).join('\n\n');
|
|
452
|
+
})
|
|
453
|
+
.filter(Boolean)
|
|
454
|
+
.join('\n\n');
|
|
455
|
+
}
|
|
456
|
+
function reads(markdown, blocks) {
|
|
457
|
+
return sameShape(parseMarkdown(markdown).children, blocks);
|
|
458
|
+
}
|
|
459
|
+
/** Whether two documents render the same, ignoring how links, mentions and tags were found. */
|
|
460
|
+
export function sameRichText(a, b) {
|
|
461
|
+
return sameShape(a.children, b.children);
|
|
462
|
+
}
|