@atmo-dev/kit 0.18.1 → 0.19.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 (114) hide show
  1. package/dist/articles/ArticleCard.svelte +97 -0
  2. package/dist/articles/ArticleCard.svelte.d.ts +20 -0
  3. package/dist/articles/ArticleCodeBlock.svelte +121 -0
  4. package/dist/articles/ArticleCodeBlock.svelte.d.ts +11 -0
  5. package/dist/articles/ArticleEditor.svelte +340 -0
  6. package/dist/articles/ArticleEditor.svelte.d.ts +48 -0
  7. package/dist/articles/ArticleLinkEmbed.svelte +50 -0
  8. package/dist/articles/ArticleLinkEmbed.svelte.d.ts +5 -0
  9. package/dist/articles/ArticleView.svelte +128 -0
  10. package/dist/articles/ArticleView.svelte.d.ts +46 -0
  11. package/dist/articles/PublicationIcon.svelte +37 -0
  12. package/dist/articles/PublicationIcon.svelte.d.ts +10 -0
  13. package/dist/articles/article-link-embed.d.ts +36 -0
  14. package/dist/articles/article-link-embed.js +29 -0
  15. package/dist/articles/dates.d.ts +2 -0
  16. package/dist/articles/dates.js +12 -0
  17. package/dist/articles/images.d.ts +5 -0
  18. package/dist/articles/images.js +44 -0
  19. package/dist/articles/markdown.d.ts +15 -0
  20. package/dist/articles/markdown.js +52 -0
  21. package/dist/articles/paste.d.ts +11 -0
  22. package/dist/articles/paste.js +27 -0
  23. package/dist/articles/records.d.ts +166 -0
  24. package/dist/articles/records.js +423 -0
  25. package/dist/articles/styles.d.ts +4 -0
  26. package/dist/articles/styles.js +14 -0
  27. package/dist/articles-data.d.ts +8 -0
  28. package/dist/articles-data.js +8 -0
  29. package/dist/articles.d.ts +10 -0
  30. package/dist/articles.js +8 -0
  31. package/dist/components/ExternalEmbed.svelte +16 -2
  32. package/dist/components/ExternalEmbed.svelte.d.ts +6 -0
  33. package/dist/components/IconMaker.svelte +119 -0
  34. package/dist/components/IconMaker.svelte.d.ts +9 -0
  35. package/dist/components/IconRail.svelte +9 -6
  36. package/dist/components/IconRail.svelte.d.ts +2 -0
  37. package/dist/components/LinkEmbed.svelte +24 -21
  38. package/dist/components/LinkEmbed.svelte.d.ts +3 -2
  39. package/dist/components/PaletteApply.svelte +62 -0
  40. package/dist/components/PaletteApply.svelte.d.ts +9 -0
  41. package/dist/components/PalettePicker.svelte +92 -0
  42. package/dist/components/PalettePicker.svelte.d.ts +13 -0
  43. package/dist/components/PostComments.svelte +129 -0
  44. package/dist/components/PostComments.svelte.d.ts +15 -0
  45. package/dist/components/RichText.svelte +199 -9
  46. package/dist/components/RichText.svelte.d.ts +17 -0
  47. package/dist/components/RichTextEditor.svelte +155 -13
  48. package/dist/components/RichTextEditor.svelte.d.ts +26 -3
  49. package/dist/components/composer/ComposerPost.svelte +10 -0
  50. package/dist/components/composer/ComposerPost.svelte.d.ts +3 -1
  51. package/dist/components/composer/PostComposer.svelte +28 -2
  52. package/dist/components/composer/PostComposer.svelte.d.ts +12 -1
  53. package/dist/editor/EditorMenus.svelte +438 -0
  54. package/dist/editor/EditorMenus.svelte.d.ts +9 -0
  55. package/dist/editor/SlashOptions.svelte +45 -0
  56. package/dist/editor/SlashOptions.svelte.d.ts +12 -0
  57. package/dist/editor/article-commands.d.ts +44 -0
  58. package/dist/editor/article-commands.js +289 -0
  59. package/dist/editor/article-extensions.d.ts +8 -0
  60. package/dist/editor/article-extensions.js +315 -0
  61. package/dist/editor/article-menus.d.ts +2 -0
  62. package/dist/editor/article-menus.js +3 -0
  63. package/dist/editor/document.d.ts +5 -0
  64. package/dist/editor/document.js +163 -12
  65. package/dist/editor/image-node.d.ts +8 -0
  66. package/dist/editor/image-node.js +75 -0
  67. package/dist/editor/markdown-extensions.d.ts +1 -5
  68. package/dist/editor/markdown-extensions.js +37 -2
  69. package/dist/editor/slash-menu.svelte.d.ts +9 -0
  70. package/dist/editor/slash-menu.svelte.js +134 -0
  71. package/dist/embeds/PlayerLinkEmbed.svelte +9 -1
  72. package/dist/embeds/PostLinkEmbed.svelte +12 -29
  73. package/dist/embeds/PostLinkEmbed.svelte.d.ts +3 -0
  74. package/dist/embeds/ProfileLinkEmbed.svelte +12 -17
  75. package/dist/embeds/ProfileLinkEmbed.svelte.d.ts +3 -0
  76. package/dist/embeds/defaults.d.ts +4 -2
  77. package/dist/embeds/defaults.js +7 -3
  78. package/dist/embeds/load.d.ts +44 -0
  79. package/dist/embeds/load.js +139 -0
  80. package/dist/embeds/types.d.ts +18 -3
  81. package/dist/events/EventComments.svelte +4 -101
  82. package/dist/events/EventComments.svelte.d.ts +1 -1
  83. package/dist/events/EventView.svelte +5 -0
  84. package/dist/events/EventView.svelte.d.ts +3 -0
  85. package/dist/events/event-link-embed.d.ts +4 -1
  86. package/dist/events/event-link-embed.js +9 -1
  87. package/dist/events/event-view/EventLinkEmbed.svelte +8 -8
  88. package/dist/events/event-view/EventLinkEmbed.svelte.d.ts +3 -2
  89. package/dist/icon-maker/artwork.d.ts +11 -0
  90. package/dist/icon-maker/artwork.js +25 -0
  91. package/dist/icon-maker/index.d.ts +12 -0
  92. package/dist/icon-maker/index.js +73 -0
  93. package/dist/index.d.ts +10 -3
  94. package/dist/index.js +7 -1
  95. package/dist/palettes.d.ts +20 -0
  96. package/dist/palettes.js +37 -0
  97. package/dist/richtext/article-styles.d.ts +10 -0
  98. package/dist/richtext/article-styles.js +28 -0
  99. package/dist/richtext/document.d.ts +63 -1
  100. package/dist/richtext/document.js +54 -0
  101. package/dist/richtext/markdown.d.ts +19 -4
  102. package/dist/richtext/markdown.js +223 -18
  103. package/dist/richtext/serialize.d.ts +5 -1
  104. package/dist/richtext/serialize.js +163 -31
  105. package/dist/richtext/vendor/markdown-tree.d.ts +4 -3
  106. package/dist/richtext/vendor/markdown-tree.js +2584 -26
  107. package/dist/richtext.d.ts +7 -5
  108. package/dist/richtext.js +1 -1
  109. package/dist/social/external-embeds.d.ts +5 -0
  110. package/dist/social/external-embeds.js +6 -1
  111. package/dist/social/posts.d.ts +2 -0
  112. package/dist/social/posts.js +43 -2
  113. package/dist/social/profile.js +2 -1
  114. package/package.json +28 -2
@@ -1,7 +1,7 @@
1
1
  // The parser is bundled into the kit. Its packages' development builds need bundler setup in
2
2
  // every app (they import CommonJS), and this is the same production build everywhere.
3
3
  import { markdownTree } from './vendor/markdown-tree.js';
4
- import { safeWebUrl } from './document.js';
4
+ import { isCallout, safeWebUrl } from './document.js';
5
5
  import { applyRanges, detectLinks, hasFacets, resolveFacets } from './facets.js';
6
6
  import { parsePlainText } from './plain.js';
7
7
  // Deeper nesting is shown as literal source instead of recursing further.
@@ -13,17 +13,25 @@ const MAX_LENGTH = 10_000;
13
13
  const MAX_DELIMITERS = 1_000;
14
14
  const MAX_LINE_CONTAINERS = 8;
15
15
  const MAX_LIST_INDENT = 32;
16
+ // Articles are written by their author and are much longer than descriptions.
17
+ const MAX_ARTICLE_LENGTH = 200_000;
18
+ const MAX_ARTICLE_DELIMITERS = 20_000;
19
+ // Every row of a table gets a cell per column, so a few short lines under a wide header stand
20
+ // for far more cells than they have characters. Tables beyond this many cells in one document
21
+ // are shown as their source.
22
+ const MAX_TABLE_CELLS = 20_000;
16
23
  const CONTAINER_RE = /[ \t]*(?:>|(?:[-*+]|\d{1,9}[.)])(?=[ \t]|$))/y;
17
24
  const LIST_RE = /[ \t]*(?:[-*+]|\d{1,9}[.)])(?:[ \t]|$)/y;
18
25
  /** Whether Markdown parsing could be slow; checked in linear time. */
19
- export function exceedsMarkdownLimits(text) {
20
- if (text.length > MAX_LENGTH)
26
+ export function exceedsMarkdownLimits(text, article = false) {
27
+ if (text.length > (article ? MAX_ARTICLE_LENGTH : MAX_LENGTH))
21
28
  return true;
29
+ const maxDelimiters = article ? MAX_ARTICLE_DELIMITERS : MAX_DELIMITERS;
22
30
  let delimiters = 0;
23
31
  for (let index = 0; index < text.length; index++) {
24
32
  const char = text[index];
25
33
  if ((char === '*' || char === '_' || char === '~' || char === '[') &&
26
- ++delimiters > MAX_DELIMITERS)
34
+ ++delimiters > maxDelimiters)
27
35
  return true;
28
36
  }
29
37
  for (const line of text.split('\n')) {
@@ -91,21 +99,93 @@ function facetText(node, context) {
91
99
  }
92
100
  return applyRanges(value, local);
93
101
  }
102
+ /** Text left as written (a `$` that is no formula) joins the text around it. */
103
+ function joinText(nodes) {
104
+ const result = [];
105
+ for (const node of nodes) {
106
+ const previous = result.at(-1);
107
+ if (node.type === 'text' && previous?.type === 'text')
108
+ result[result.length - 1] = { type: 'text', value: previous.value + node.value };
109
+ else
110
+ result.push(node);
111
+ }
112
+ return result;
113
+ }
114
+ /** In an article a newline inside a paragraph is a space; only an explicit break breaks. */
115
+ function soften(nodes) {
116
+ return nodes.map((node) => node.type === 'text' && node.value.includes('\n')
117
+ ? { type: 'text', value: node.value.replace(/\n/g, ' ') }
118
+ : node);
119
+ }
120
+ function image(node, context) {
121
+ if (!context.article)
122
+ return undefined;
123
+ const definition = node.type === 'image' ? node : context.definitions.get(node.identifier);
124
+ const raw = definition?.url;
125
+ if (typeof raw !== 'string' || !raw)
126
+ return undefined;
127
+ const url = safeWebUrl(raw) ?? (context.allowImage?.(raw) ? raw : undefined);
128
+ if (!url)
129
+ return undefined;
130
+ return {
131
+ type: 'image',
132
+ url,
133
+ ...(node.alt ? { alt: node.alt } : {}),
134
+ ...(definition?.title ? { title: definition.title } : {})
135
+ };
136
+ }
137
+ export const FOOTNOTE_ID = /^[^\s[\]^]{1,100}$/;
138
+ /**
139
+ * `$…$` is a formula only when the dollars hug it and no digit follows, so that "$5 and $10"
140
+ * stays text. `$$…$$` in a line is always one.
141
+ */
142
+ function isInlineMath(raw, after) {
143
+ if (raw.startsWith('$$'))
144
+ return true;
145
+ const inner = raw.slice(1, -1);
146
+ return !!inner && !/^\s|\s$/.test(inner) && !/[0-9]/.test(after ?? '');
147
+ }
94
148
  function inline(nodes, context, depth, inLink = false) {
149
+ return phrases(nodes, context, depth, inLink, false);
150
+ }
151
+ /** With `images`, images that can be shown are returned as image blocks among the inline nodes. */
152
+ function phrases(nodes, context, depth, inLink, images) {
95
153
  const result = [];
96
154
  for (const node of nodes) {
97
155
  if (depth > MAX_DEPTH && node.type !== 'text') {
98
156
  result.push({ type: 'text', value: literal(node, context) });
99
157
  continue;
100
158
  }
159
+ const extra = node;
160
+ if (extra.type === 'highlight') {
161
+ result.push({
162
+ type: 'highlight',
163
+ children: inline(node.children, context, depth + 1, inLink)
164
+ });
165
+ continue;
166
+ }
167
+ if (extra.type === 'inlineMath') {
168
+ const raw = literal(node, context);
169
+ result.push(isInlineMath(raw, context.source[node.position?.end.offset ?? -1])
170
+ ? { type: 'math', value: extra.value ?? '' }
171
+ : { type: 'text', value: raw });
172
+ continue;
173
+ }
101
174
  switch (node.type) {
102
- case 'text':
103
- result.push(...(inLink
175
+ case 'footnoteReference':
176
+ result.push(FOOTNOTE_ID.test(node.identifier)
177
+ ? { type: 'footnoteReference', id: node.identifier }
178
+ : { type: 'text', value: literal(node, context) });
179
+ break;
180
+ case 'text': {
181
+ const text = inLink
104
182
  ? [{ type: 'text', value: node.value }]
105
183
  : context.detect
106
184
  ? detectLinks(node.value)
107
- : facetText(node, context)));
185
+ : facetText(node, context);
186
+ result.push(...text);
108
187
  break;
188
+ }
109
189
  case 'emphasis':
110
190
  case 'strong':
111
191
  case 'delete':
@@ -132,7 +212,12 @@ function inline(nodes, context, depth, inLink = false) {
132
212
  }
133
213
  case 'image':
134
214
  case 'imageReference': {
135
- // Images are never loaded; a safe image becomes a link labeled by its alt text.
215
+ const block = images && !inLink ? image(node, context) : undefined;
216
+ if (block) {
217
+ result.push(block);
218
+ break;
219
+ }
220
+ // Elsewhere images are never loaded; a safe image becomes a link labeled by its alt text.
136
221
  const url = safeWebUrl(node.type === 'image' ? node.url : context.definitions.get(node.identifier)?.url);
137
222
  const alt = node.alt ?? '';
138
223
  if (url && !inLink)
@@ -149,7 +234,83 @@ function inline(nodes, context, depth, inLink = false) {
149
234
  });
150
235
  }
151
236
  }
152
- return withBreaks(result);
237
+ return context.article ? joinText(soften(result)) : withBreaks(result);
238
+ }
239
+ /** A paragraph's text, with its images as blocks of their own between the pieces. */
240
+ function paragraph(nodes, context, depth) {
241
+ if (!context.article)
242
+ return [{ type: 'paragraph', children: inline(nodes, context, depth) }];
243
+ const result = [];
244
+ let run = [];
245
+ const blank = (node) => node?.type === 'break' || (node?.type === 'text' && !node.value.trim());
246
+ const flush = () => {
247
+ while (run.length && blank(run[0]))
248
+ run.shift();
249
+ while (run.length && blank(run.at(-1)))
250
+ run.pop();
251
+ const first = run[0];
252
+ if (first?.type === 'text')
253
+ run[0] = { type: 'text', value: first.value.trimStart() };
254
+ const last = run.at(-1);
255
+ if (last?.type === 'text')
256
+ run[run.length - 1] = { type: 'text', value: last.value.trimEnd() };
257
+ if (run.length)
258
+ result.push({ type: 'paragraph', children: run });
259
+ run = [];
260
+ };
261
+ for (const node of phrases(nodes, context, depth, false, true)) {
262
+ if (node.type === 'image') {
263
+ flush();
264
+ result.push(node);
265
+ }
266
+ else
267
+ run.push(node);
268
+ }
269
+ flush();
270
+ return result.length ? result : [{ type: 'paragraph', children: [] }];
271
+ }
272
+ const CALLOUT_MARK = /^\[!([A-Za-z]+)\][ \t]*(\n|$)/;
273
+ /**
274
+ * A quote whose first line is `[!NOTE]`, `[!TIP]`, `[!IMPORTANT]`, `[!WARNING]` or `[!CAUTION]`
275
+ * alone is a callout, as on GitHub. Returns its kind and the content without that line.
276
+ */
277
+ function calloutOf(node, context) {
278
+ const [first, ...rest] = node.children;
279
+ if (first?.type !== 'paragraph')
280
+ return undefined;
281
+ const [text, ...others] = first.children;
282
+ const start = text?.position?.start.offset;
283
+ if (text?.type !== 'text' || start === undefined)
284
+ return undefined;
285
+ const match = CALLOUT_MARK.exec(text.value);
286
+ const kind = match?.[1].toLowerCase();
287
+ // An escaped `\[!NOTE]` has the same value, and is no callout.
288
+ if (!match || !isCallout(kind) || !context.source.startsWith('[!', start))
289
+ return undefined;
290
+ const value = text.value.slice(match[0].length);
291
+ const line = context.source.indexOf('\n', start);
292
+ // The marker alone, then a hard break: the break goes with it.
293
+ if (!value && !match[2] && others.length && others[0].type !== 'break')
294
+ return undefined;
295
+ const remaining = value
296
+ ? [
297
+ {
298
+ ...text,
299
+ value,
300
+ position: {
301
+ ...text.position,
302
+ start: { ...text.position.start, offset: line < 0 ? start : line + 1 }
303
+ }
304
+ },
305
+ ...others
306
+ ]
307
+ : others[0]?.type === 'break'
308
+ ? others.slice(1)
309
+ : others;
310
+ return {
311
+ kind,
312
+ children: remaining.length ? [{ ...first, children: remaining }, ...rest] : rest
313
+ };
153
314
  }
154
315
  function blocks(nodes, context, depth) {
155
316
  const result = [];
@@ -161,10 +322,39 @@ function blocks(nodes, context, depth) {
161
322
  });
162
323
  continue;
163
324
  }
325
+ if (node.type === 'math') {
326
+ result.push({ type: 'math', value: node.value ?? '' });
327
+ continue;
328
+ }
164
329
  switch (node.type) {
330
+ case 'footnoteDefinition':
331
+ if (FOOTNOTE_ID.test(node.identifier))
332
+ result.push({
333
+ type: 'footnoteDefinition',
334
+ id: node.identifier,
335
+ children: blocks(node.children, context, depth + 1)
336
+ });
337
+ break;
165
338
  case 'paragraph':
166
- result.push({ type: 'paragraph', children: inline(node.children, context, depth) });
339
+ result.push(...paragraph(node.children, context, depth));
340
+ break;
341
+ case 'table': {
342
+ const width = Math.max(1, ...node.children.map((row) => row.children.length));
343
+ context.cells -= width * node.children.length;
344
+ if (context.cells < 0) {
345
+ result.push({
346
+ type: 'paragraph',
347
+ children: withBreaks([{ type: 'text', value: literal(node, context) }])
348
+ });
349
+ break;
350
+ }
351
+ result.push({
352
+ type: 'table',
353
+ align: Array.from({ length: width }, (_, index) => node.align?.[index] ?? null),
354
+ rows: node.children.map((row) => Array.from({ length: width }, (_, index) => inline(row.children[index]?.children ?? [], context, depth + 1)))
355
+ });
167
356
  break;
357
+ }
168
358
  case 'heading':
169
359
  result.push({
170
360
  type: 'heading',
@@ -172,15 +362,24 @@ function blocks(nodes, context, depth) {
172
362
  children: inline(node.children, context, depth)
173
363
  });
174
364
  break;
175
- case 'blockquote':
176
- result.push({ type: 'blockquote', children: blocks(node.children, context, depth + 1) });
365
+ case 'blockquote': {
366
+ const callout = context.article ? calloutOf(node, context) : undefined;
367
+ result.push({
368
+ type: 'blockquote',
369
+ children: blocks(callout?.children ?? node.children, context, depth + 1),
370
+ ...(callout ? { callout: callout.kind } : {})
371
+ });
177
372
  break;
373
+ }
178
374
  case 'list':
179
375
  result.push({
180
376
  type: 'list',
181
377
  ordered: !!node.ordered,
182
378
  ...(node.ordered && typeof node.start === 'number' ? { start: node.start } : {}),
183
- items: node.children.map((item) => blocks(item.children, context, depth + 1))
379
+ items: node.children.map((item) => blocks(item.children, context, depth + 1)),
380
+ ...(node.children.some((item) => typeof item.checked === 'boolean')
381
+ ? { checked: node.children.map((item) => item.checked ?? null) }
382
+ : {})
184
383
  });
185
384
  break;
186
385
  case 'code':
@@ -218,20 +417,26 @@ function collectDefinitions(node, definitions, depth = 0) {
218
417
  }
219
418
  /**
220
419
  * CommonMark plus ~~strikethrough~~, with facets applied to the parsed text. Raw HTML is shown
221
- * literally, images become links and only HTTP(S) destinations are kept. Text that exceeds
222
- * the parser's limits is returned as a plain-text document.
420
+ * literally, images become links and only HTTP(S) destinations are kept. With `article`,
421
+ * images, tables, footnotes, task lists, `$math$`, `==highlights==` and callouts (`> [!NOTE]`)
422
+ * are kept and newlines inside a paragraph wrap. Text that exceeds the parser's limits is
423
+ * returned as a plain-text document.
223
424
  */
224
425
  export function parseMarkdown(text, options = {}) {
225
- if (exceedsMarkdownLimits(text))
426
+ const article = !!options.article;
427
+ if (exceedsMarkdownLimits(text, article))
226
428
  return parsePlainText(text, options);
227
- const tree = markdownTree(text);
429
+ const tree = markdownTree(text, { article });
228
430
  const definitions = new Map();
229
431
  collectDefinitions(tree, definitions);
230
432
  const context = {
231
433
  source: text,
232
434
  ranges: resolveFacets(text, options.facets),
233
435
  detect: !hasFacets(options.facets) && !!options.detectLinks,
234
- definitions
436
+ definitions,
437
+ article,
438
+ allowImage: options.allowImage,
439
+ cells: MAX_TABLE_CELLS
235
440
  };
236
441
  return { version: 1, markdown: true, children: blocks(tree.children, context, 0) };
237
442
  }
@@ -1,4 +1,8 @@
1
1
  import { type RichTextDocument } from './document.js';
2
+ export type MarkdownWriteOptions = {
3
+ /** Write article Markdown, as `parseMarkdown` reads with `article`: line breaks are explicit. */
4
+ article?: boolean;
5
+ };
2
6
  /**
3
7
  * A document as Markdown that `parseRichText(text, { markdown: true })` reads back as the same
4
8
  * document. Only characters that would otherwise be syntax are escaped, so plain links,
@@ -6,6 +10,6 @@ import { type RichTextDocument } from './document.js';
6
10
  * their text; a link labeled with its own URL is written as that URL. Output that fails to
7
11
  * read back (unusual punctuation next to formatting) is escaped more heavily instead.
8
12
  */
9
- export declare function richTextToMarkdown(content: RichTextDocument): string;
13
+ export declare function richTextToMarkdown(content: RichTextDocument, options?: MarkdownWriteOptions): string;
10
14
  /** Whether two documents render the same, ignoring how links, mentions and tags were found. */
11
15
  export declare function sameRichText(a: RichTextDocument, b: RichTextDocument): boolean;