@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.
Files changed (115) 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 +164 -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/mention-suggestions.svelte.js +18 -7
  112. package/dist/social/posts.d.ts +2 -0
  113. package/dist/social/posts.js +43 -2
  114. package/dist/social/profile.js +2 -1
  115. package/package.json +28 -2
package/dist/palettes.js CHANGED
@@ -28,3 +28,40 @@ export const accentColors = [
28
28
  'pink',
29
29
  'rose'
30
30
  ];
31
+ export const isBaseColor = (value) => baseColors.includes(value);
32
+ export const isAccentColor = (value) => accentColors.includes(value);
33
+ /**
34
+ * The palettes as sRGB, for records and other places that cannot name a palette: the page and
35
+ * text colors of a base (its 50 and 950), and the 700 of an accent, which the kit uses for links
36
+ * on a light page and which white text is readable on.
37
+ */
38
+ export const baseColorRgb = {
39
+ gray: { background: [249, 250, 251], foreground: [3, 7, 18] },
40
+ zinc: { background: [250, 250, 250], foreground: [9, 9, 11] },
41
+ neutral: { background: [250, 250, 250], foreground: [10, 10, 10] },
42
+ stone: { background: [250, 250, 249], foreground: [12, 10, 9] },
43
+ slate: { background: [248, 250, 252], foreground: [2, 6, 24] },
44
+ mist: { background: [249, 251, 251], foreground: [9, 11, 12] },
45
+ mauve: { background: [250, 250, 250], foreground: [12, 9, 12] },
46
+ olive: { background: [251, 251, 249], foreground: [12, 12, 9] },
47
+ taupe: { background: [251, 250, 249], foreground: [12, 10, 9] }
48
+ };
49
+ export const accentColorRgb = {
50
+ red: [193, 0, 7],
51
+ orange: [202, 53, 0],
52
+ amber: [187, 77, 0],
53
+ yellow: [166, 95, 0],
54
+ lime: [73, 125, 0],
55
+ green: [0, 130, 54],
56
+ emerald: [0, 122, 85],
57
+ teal: [0, 120, 111],
58
+ cyan: [0, 117, 149],
59
+ sky: [0, 105, 168],
60
+ blue: [20, 71, 230],
61
+ indigo: [67, 45, 215],
62
+ violet: [112, 8, 231],
63
+ purple: [130, 0, 219],
64
+ fuchsia: [168, 0, 183],
65
+ pink: [198, 0, 92],
66
+ rose: [199, 0, 54]
67
+ };
@@ -0,0 +1,10 @@
1
+ import { type IconNode } from '@sketchyicons/svelte';
2
+ import type { RichTextCallout } from './document.js';
3
+ /** `==marked==` text, the same where it is written and where it is read. */
4
+ export declare const highlightClass = "rounded-sm bg-accent-200 px-0.5 text-inherit dark:bg-[color-mix(in_srgb,var(--accent-500)_35%,transparent)]";
5
+ export declare const CALLOUT_LABELS: Record<RichTextCallout, string>;
6
+ export declare const CALLOUT_ICONS: Record<RichTextCallout, IconNode>;
7
+ /** The box of a callout. */
8
+ export declare function calloutClass(kind: RichTextCallout): string;
9
+ /** The line naming the kind of callout. */
10
+ export declare function calloutLabelClass(kind: RichTextCallout): string;
@@ -0,0 +1,28 @@
1
+ import { Info, Lightbulb, MessageSquareWarning, OctagonAlert, TriangleAlert } from '@sketchyicons/svelte';
2
+ /** `==marked==` text, the same where it is written and where it is read. */
3
+ export const highlightClass = 'rounded-sm bg-accent-200 px-0.5 text-inherit dark:bg-[color-mix(in_srgb,var(--accent-500)_35%,transparent)]';
4
+ export const CALLOUT_LABELS = {
5
+ note: 'Note',
6
+ tip: 'Tip',
7
+ important: 'Important',
8
+ warning: 'Warning',
9
+ caution: 'Caution'
10
+ };
11
+ export const CALLOUT_ICONS = {
12
+ note: Info,
13
+ tip: Lightbulb,
14
+ important: MessageSquareWarning,
15
+ warning: TriangleAlert,
16
+ caution: OctagonAlert
17
+ };
18
+ const alarming = (kind) => kind === 'warning' || kind === 'caution';
19
+ /** The box of a callout. */
20
+ export function calloutClass(kind) {
21
+ return `space-y-3 rounded-ui px-4 py-3 ${alarming(kind)
22
+ ? 'bg-[color-mix(in_srgb,var(--color-red-500)_9%,transparent)]'
23
+ : 'bg-[color-mix(in_srgb,var(--accent-500)_9%,transparent)]'}`;
24
+ }
25
+ /** The line naming the kind of callout. */
26
+ export function calloutLabelClass(kind) {
27
+ return `flex items-center gap-1.5 font-sans text-sm leading-normal font-semibold ${alarming(kind) ? 'text-red-700 dark:text-red-400' : 'text-accent-700 dark:text-accent-300'}`;
28
+ }
@@ -18,21 +18,55 @@ export type RichTextBlock = {
18
18
  type: 'heading';
19
19
  depth: 1 | 2 | 3 | 4 | 5 | 6;
20
20
  children: RichTextInline[];
21
- } | {
21
+ }
22
+ /** Article Markdown only: with `callout`, a quote that began with `[!NOTE]` or the like. */
23
+ | {
22
24
  type: 'blockquote';
23
25
  children: RichTextBlock[];
26
+ callout?: RichTextCallout;
24
27
  } | {
25
28
  type: 'list';
26
29
  ordered: boolean;
27
30
  start?: number;
28
31
  items: RichTextBlock[][];
32
+ /** Article Markdown only: per item, whether a task is done; `null` for an ordinary item. */
33
+ checked?: (boolean | null)[];
29
34
  } | {
30
35
  type: 'codeBlock';
31
36
  value: string;
32
37
  lang?: string;
33
38
  } | {
34
39
  type: 'thematicBreak';
40
+ }
41
+ /** Article Markdown only. `url` is HTTP(S) or a destination the app allowed. */
42
+ | {
43
+ type: 'image';
44
+ url: string;
45
+ alt?: string;
46
+ title?: string;
47
+ }
48
+ /** Article Markdown only. The first row is the header; every row has a cell per column. */
49
+ | {
50
+ type: 'table';
51
+ align: RichTextAlign[];
52
+ rows: RichTextInline[][][];
53
+ }
54
+ /** Article Markdown only. TeX source of a displayed formula. */
55
+ | {
56
+ type: 'math';
57
+ value: string;
58
+ }
59
+ /** Article Markdown only. Shown at the end of the text, not where it was written. */
60
+ | {
61
+ type: 'footnoteDefinition';
62
+ id: string;
63
+ children: RichTextBlock[];
35
64
  };
65
+ export type RichTextAlign = 'left' | 'center' | 'right' | null;
66
+ /** The kinds of callout, as GitHub has them: `> [!NOTE]` and so on. */
67
+ export declare const CALLOUTS: readonly ["note", "tip", "important", "warning", "caution"];
68
+ export type RichTextCallout = (typeof CALLOUTS)[number];
69
+ export declare function isCallout(value: unknown): value is RichTextCallout;
36
70
  export type RichTextInline = {
37
71
  type: 'text';
38
72
  value: string;
@@ -41,9 +75,24 @@ export type RichTextInline = {
41
75
  } | {
42
76
  type: 'strong' | 'emphasis' | 'delete';
43
77
  children: RichTextInline[];
78
+ }
79
+ /** Article Markdown only: `==marked==` text. */
80
+ | {
81
+ type: 'highlight';
82
+ children: RichTextInline[];
44
83
  } | {
45
84
  type: 'code';
46
85
  value: string;
86
+ }
87
+ /** Article Markdown only. TeX source of a formula in the line. */
88
+ | {
89
+ type: 'math';
90
+ value: string;
91
+ }
92
+ /** Article Markdown only. Refers to the `footnoteDefinition` with the same id. */
93
+ | {
94
+ type: 'footnoteReference';
95
+ id: string;
47
96
  } | {
48
97
  type: 'link';
49
98
  url: string;
@@ -85,11 +134,24 @@ export declare function lineBreaks(children: unknown): Record<string, unknown>[]
85
134
  * an embed. Labeled links and links inside a sentence stay ordinary links.
86
135
  */
87
136
  export declare function paragraphParts(children: unknown, isEmbed?: (url: string) => boolean): ParagraphPart[];
137
+ /** The parts of a plain-text document: its lines, with those shown as embeds on their own. */
138
+ export declare function plainTextParts(document: {
139
+ children?: unknown;
140
+ } | null | undefined, isEmbed?: (url: string) => boolean): ParagraphPart[];
88
141
  /** Blank lines next to an embed are already expressed by the space around it. */
89
142
  export declare function trimBreaks(children: Record<string, unknown>[]): Record<string, unknown>[];
90
143
  /** Documents from the parser nest far less; deeper (tampered) nodes are cut off. */
91
144
  export declare const MAX_RENDER_DEPTH = 32;
92
145
  /** Text content of any node, including unknown ones. */
93
146
  export declare function inlineText(node: Record<string, unknown>, depth?: number): string;
147
+ export type Footnotes = {
148
+ /** Ids in the order their notes are shown: by first reference, then the unreferenced. */
149
+ order: string[];
150
+ definitions: Map<string, Record<string, unknown>>;
151
+ /** The first reference to each note, where its way back leads. */
152
+ references: Map<string, Record<string, unknown>>;
153
+ };
154
+ /** The footnotes of a document, for renderers: definitions are shown together at the end. */
155
+ export declare function collectFootnotes(content: unknown): Footnotes;
94
156
  /** Readable text without Markdown syntax, for meta descriptions, previews and search. */
95
157
  export declare function richTextToPlainText(content: RichTextDocument): string;
@@ -3,6 +3,11 @@ import { socialHashtagUrl, socialProfileUrl } from '../social/urls.js';
3
3
  import { safeHttpUrl as safeWebUrl } from '../urls.js';
4
4
  // Backwards-compatible export for existing richtext and social consumers.
5
5
  export { safeWebUrl };
6
+ /** The kinds of callout, as GitHub has them: `> [!NOTE]` and so on. */
7
+ export const CALLOUTS = ['note', 'tip', 'important', 'warning', 'caution'];
8
+ export function isCallout(value) {
9
+ return CALLOUTS.includes(value);
10
+ }
6
11
  /** Only app-relative paths or HTTP(S), including for app-owned mention callbacks. */
7
12
  export function mentionHref(actor, options) {
8
13
  if (typeof actor !== 'string' || !isActorIdentifier(actor))
@@ -111,6 +116,10 @@ export function paragraphParts(children, isEmbed) {
111
116
  }
112
117
  return parts;
113
118
  }
119
+ /** The parts of a plain-text document: its lines, with those shown as embeds on their own. */
120
+ export function plainTextParts(document, isEmbed) {
121
+ return nodes(document?.children).flatMap((block) => paragraphParts(lineBreaks(block.children), isEmbed));
122
+ }
114
123
  /** Blank lines next to an embed are already expressed by the space around it. */
115
124
  export function trimBreaks(children) {
116
125
  let start = 0;
@@ -153,7 +162,20 @@ function blockText(node, depth) {
153
162
  case 'thematicBreak':
154
163
  return '';
155
164
  case 'codeBlock':
165
+ case 'math':
156
166
  return string(node.value);
167
+ case 'footnoteDefinition':
168
+ return blocksText(node.children, '\n\n', depth + 1);
169
+ case 'image':
170
+ return string(node.alt);
171
+ case 'table':
172
+ return (Array.isArray(node.rows) ? node.rows : [])
173
+ .map((row) => (Array.isArray(row) ? row : [])
174
+ .map((cell) => nodes(cell)
175
+ .map((child) => inlineText(child, depth + 1))
176
+ .join(''))
177
+ .join('\t'))
178
+ .join('\n');
157
179
  default:
158
180
  return inlineText(node, depth);
159
181
  }
@@ -164,6 +186,38 @@ function blocksText(value, separator = '\n\n', depth = 0) {
164
186
  .filter(Boolean)
165
187
  .join(separator);
166
188
  }
189
+ /** The footnotes of a document, for renderers: definitions are shown together at the end. */
190
+ export function collectFootnotes(content) {
191
+ const definitions = new Map();
192
+ const referenced = [];
193
+ const references = new Map();
194
+ const walk = (value, depth) => {
195
+ if (depth > MAX_RENDER_DEPTH + 4 || value === null || typeof value !== 'object')
196
+ return;
197
+ if (Array.isArray(value))
198
+ return value.forEach((item) => walk(item, depth + 1));
199
+ const node = value;
200
+ const id = string(node.id);
201
+ if (node.type === 'footnoteDefinition' && id && !definitions.has(id))
202
+ definitions.set(id, node);
203
+ if (node.type === 'footnoteReference' && id && !referenced.includes(id)) {
204
+ referenced.push(id);
205
+ references.set(id, node);
206
+ }
207
+ walk(node.children, depth + 1);
208
+ walk(node.items, depth + 1);
209
+ walk(node.rows, depth + 1);
210
+ };
211
+ walk(content?.children, 0);
212
+ const order = referenced.filter((id) => definitions.has(id));
213
+ for (const id of definitions.keys())
214
+ if (!order.includes(id))
215
+ order.push(id);
216
+ for (const id of references.keys())
217
+ if (!definitions.has(id))
218
+ references.delete(id);
219
+ return { order, definitions, references };
220
+ }
167
221
  /** Readable text without Markdown syntax, for meta descriptions, previews and search. */
168
222
  export function richTextToPlainText(content) {
169
223
  return blocksText(content?.children);
@@ -1,10 +1,25 @@
1
1
  import { type RichTextDocument } from './document.js';
2
2
  import { type PlainTextOptions } from './plain.js';
3
+ export type MarkdownOptions = PlainTextOptions & {
4
+ /**
5
+ * Standard Markdown for long-form writing: a single newline wraps instead of breaking the
6
+ * line, images and tables are kept, and far longer text is parsed.
7
+ */
8
+ article?: boolean;
9
+ /**
10
+ * With `article`, image destinations to keep besides HTTP(S), e.g. an app's own scheme for
11
+ * uploaded files. Such a destination is kept as written; rendering it is up to the app.
12
+ */
13
+ allowImage?: (url: string) => boolean;
14
+ };
3
15
  /** Whether Markdown parsing could be slow; checked in linear time. */
4
- export declare function exceedsMarkdownLimits(text: string): boolean;
16
+ export declare function exceedsMarkdownLimits(text: string, article?: boolean): boolean;
17
+ export declare const FOOTNOTE_ID: RegExp;
5
18
  /**
6
19
  * CommonMark plus ~~strikethrough~~, with facets applied to the parsed text. Raw HTML is shown
7
- * literally, images become links and only HTTP(S) destinations are kept. Text that exceeds
8
- * the parser's limits is returned as a plain-text document.
20
+ * literally, images become links and only HTTP(S) destinations are kept. With `article`,
21
+ * images, tables, footnotes, task lists, `$math$`, `==highlights==` and callouts (`> [!NOTE]`)
22
+ * are kept and newlines inside a paragraph wrap. Text that exceeds the parser's limits is
23
+ * returned as a plain-text document.
9
24
  */
10
- export declare function parseMarkdown(text: string, options?: PlainTextOptions): RichTextDocument;
25
+ export declare function parseMarkdown(text: string, options?: MarkdownOptions): RichTextDocument;
@@ -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;