@diegopluna/narvi 0.0.2 → 0.2.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 (313) hide show
  1. package/README.md +116 -44
  2. package/dist/base.css +96 -0
  3. package/dist/color.d.ts +1 -1
  4. package/dist/color.js +1 -1
  5. package/dist/components/activity/ActivityContent.svelte +3 -15
  6. package/dist/components/activity/ActivityStep.svelte +12 -12
  7. package/dist/components/activity/ActivityThought.svelte +4 -4
  8. package/dist/components/activity/ActivityTrigger.svelte +5 -6
  9. package/dist/components/app-shell/AppShellBar.svelte +89 -0
  10. package/dist/components/app-shell/AppShellBar.svelte.d.ts +20 -0
  11. package/dist/components/app-shell/AppShellHeader.svelte +17 -17
  12. package/dist/components/app-shell/AppShellHeader.svelte.d.ts +2 -2
  13. package/dist/components/app-shell/AppShellMain.svelte +4 -20
  14. package/dist/components/app-shell/AppShellPanel.svelte +21 -19
  15. package/dist/components/app-shell/AppShellPanes.svelte +58 -0
  16. package/dist/components/app-shell/AppShellPanes.svelte.d.ts +11 -0
  17. package/dist/components/app-shell/AppShellRail.svelte +85 -0
  18. package/dist/components/app-shell/AppShellRail.svelte.d.ts +11 -0
  19. package/dist/components/app-shell/AppShellRoot.svelte +50 -24
  20. package/dist/components/app-shell/AppShellRoot.svelte.d.ts +8 -6
  21. package/dist/components/app-shell/AppShellSheet.svelte +95 -0
  22. package/dist/components/app-shell/AppShellSheet.svelte.d.ts +17 -0
  23. package/dist/components/app-shell/AppShellSidebar.svelte +41 -65
  24. package/dist/components/app-shell/AppShellSidebar.svelte.d.ts +1 -5
  25. package/dist/components/app-shell/AppShellToggle.svelte +62 -23
  26. package/dist/components/app-shell/AppShellToggle.svelte.d.ts +2 -0
  27. package/dist/components/app-shell/context.d.ts +6 -0
  28. package/dist/components/app-shell/index.d.ts +9 -1
  29. package/dist/components/app-shell/index.js +5 -1
  30. package/dist/components/approval/Approval.svelte +99 -23
  31. package/dist/components/approval/Approval.svelte.d.ts +7 -0
  32. package/dist/components/avatar/AvatarFallback.svelte +1 -2
  33. package/dist/components/avatar/AvatarRoot.svelte +2 -2
  34. package/dist/components/badge/Badge.svelte +5 -5
  35. package/dist/components/button/Button.svelte +227 -51
  36. package/dist/components/button/Button.svelte.d.ts +38 -2
  37. package/dist/components/card/Card.svelte +19 -33
  38. package/dist/components/change-stats/ChangeStats.svelte +1 -2
  39. package/dist/components/changes-summary/ChangesSummary.svelte +14 -15
  40. package/dist/components/checkbox/Checkbox.svelte +31 -25
  41. package/dist/components/checkbox/Checkbox.svelte.d.ts +5 -0
  42. package/dist/components/code/Code.svelte +1 -2
  43. package/dist/components/collapsible/CollapsibleContent.svelte +38 -0
  44. package/dist/components/collapsible/CollapsibleContent.svelte.d.ts +11 -0
  45. package/dist/components/collapsible/CollapsibleRoot.svelte +44 -0
  46. package/dist/components/collapsible/CollapsibleRoot.svelte.d.ts +11 -0
  47. package/dist/components/collapsible/CollapsibleTrigger.svelte +83 -0
  48. package/dist/components/collapsible/CollapsibleTrigger.svelte.d.ts +13 -0
  49. package/dist/components/collapsible/context.d.ts +5 -0
  50. package/dist/components/collapsible/context.js +11 -0
  51. package/dist/components/collapsible/index.d.ts +7 -0
  52. package/dist/components/collapsible/index.js +4 -0
  53. package/dist/components/command/CommandItem.svelte +1 -1
  54. package/dist/components/command/styles.d.ts +3 -2
  55. package/dist/components/command/styles.js +15 -12
  56. package/dist/components/composer/ComposerAttachButton.svelte +1 -1
  57. package/dist/components/composer/ComposerAttachment.svelte +13 -18
  58. package/dist/components/composer/ComposerModelPicker.svelte +24 -28
  59. package/dist/components/composer/ComposerRoot.svelte +15 -13
  60. package/dist/components/composer/ComposerSubmit.svelte +12 -20
  61. package/dist/components/composer/ComposerTextarea.svelte +1 -1
  62. package/dist/components/composer/ComposerToggle.svelte +16 -18
  63. package/dist/components/composer/ComposerTray.svelte +78 -0
  64. package/dist/components/composer/ComposerTray.svelte.d.ts +15 -0
  65. package/dist/components/composer/index.d.ts +3 -1
  66. package/dist/components/composer/index.js +3 -2
  67. package/dist/components/conversation/ConversationContent.svelte +3 -2
  68. package/dist/components/conversation/ConversationEmpty.svelte +1 -1
  69. package/dist/components/conversation/ConversationScrollButton.svelte +9 -11
  70. package/dist/components/dialog/ConfirmDialog.svelte +1 -1
  71. package/dist/components/dialog/DialogContent.svelte +5 -1
  72. package/dist/components/dialog/styles.d.ts +2 -26
  73. package/dist/components/dialog/styles.js +12 -49
  74. package/dist/components/divider/Divider.svelte +3 -3
  75. package/dist/components/empty-state/EmptyState.svelte +4 -5
  76. package/dist/components/field/Field.svelte +4 -4
  77. package/dist/components/frame/Frame.svelte +6 -7
  78. package/dist/components/frame/Frame.svelte.d.ts +1 -1
  79. package/dist/components/grid/Grid.svelte +111 -0
  80. package/dist/components/grid/Grid.svelte.d.ts +27 -0
  81. package/dist/components/heading/Heading.svelte +2 -2
  82. package/dist/components/hover-card/HoverCardContent.svelte +50 -0
  83. package/dist/components/hover-card/HoverCardContent.svelte.d.ts +10 -0
  84. package/dist/components/hover-card/HoverCardRoot.svelte +22 -0
  85. package/dist/components/hover-card/HoverCardRoot.svelte.d.ts +5 -0
  86. package/dist/components/hover-card/index.d.ts +7 -0
  87. package/dist/components/hover-card/index.js +7 -0
  88. package/dist/components/kbd/Kbd.svelte +12 -7
  89. package/dist/components/link/Link.svelte +9 -7
  90. package/dist/components/link/Link.svelte.d.ts +4 -1
  91. package/dist/components/markdown/Markdown.svelte +714 -0
  92. package/dist/components/markdown/Markdown.svelte.d.ts +80 -0
  93. package/dist/components/markdown/MarkdownBlock.svelte +231 -0
  94. package/dist/components/markdown/MarkdownBlock.svelte.d.ts +11 -0
  95. package/dist/components/markdown/MarkdownBlocks.svelte +115 -0
  96. package/dist/components/markdown/MarkdownBlocks.svelte.d.ts +15 -0
  97. package/dist/components/markdown/MarkdownCode.svelte +583 -0
  98. package/dist/components/markdown/MarkdownCode.svelte.d.ts +23 -0
  99. package/dist/components/markdown/MarkdownDiagram.svelte +310 -0
  100. package/dist/components/markdown/MarkdownDiagram.svelte.d.ts +6 -0
  101. package/dist/components/markdown/MarkdownFile.svelte +134 -0
  102. package/dist/components/markdown/MarkdownFile.svelte.d.ts +19 -0
  103. package/dist/components/markdown/MarkdownFootnotes.svelte +61 -0
  104. package/dist/components/markdown/MarkdownFootnotes.svelte.d.ts +8 -0
  105. package/dist/components/markdown/MarkdownFresh.svelte +69 -0
  106. package/dist/components/markdown/MarkdownFresh.svelte.d.ts +6 -0
  107. package/dist/components/markdown/MarkdownHtml.svelte +72 -0
  108. package/dist/components/markdown/MarkdownHtml.svelte.d.ts +7 -0
  109. package/dist/components/markdown/MarkdownImage.svelte +150 -0
  110. package/dist/components/markdown/MarkdownImage.svelte.d.ts +8 -0
  111. package/dist/components/markdown/MarkdownInlines.svelte +225 -0
  112. package/dist/components/markdown/MarkdownInlines.svelte.d.ts +7 -0
  113. package/dist/components/markdown/MarkdownLink.svelte +120 -0
  114. package/dist/components/markdown/MarkdownLink.svelte.d.ts +25 -0
  115. package/dist/components/markdown/MarkdownMath.svelte +138 -0
  116. package/dist/components/markdown/MarkdownMath.svelte.d.ts +7 -0
  117. package/dist/components/markdown/MarkdownTable.svelte +265 -0
  118. package/dist/components/markdown/MarkdownTable.svelte.d.ts +17 -0
  119. package/dist/components/markdown/caret.d.ts +14 -0
  120. package/dist/components/markdown/caret.js +67 -0
  121. package/dist/components/markdown/context.d.ts +93 -0
  122. package/dist/components/markdown/context.js +128 -0
  123. package/dist/components/markdown/copy.d.ts +13 -0
  124. package/dist/components/markdown/copy.js +74 -0
  125. package/dist/components/markdown/hidden.d.ts +12 -0
  126. package/dist/components/markdown/hidden.js +31 -0
  127. package/dist/components/markdown/kept.d.ts +32 -0
  128. package/dist/components/markdown/kept.js +71 -0
  129. package/dist/components/markdown/labels.d.ts +46 -0
  130. package/dist/components/markdown/labels.js +35 -0
  131. package/dist/components/markdown/save.d.ts +11 -0
  132. package/dist/components/markdown/save.js +58 -0
  133. package/dist/components/markdown/scrollable.d.ts +24 -0
  134. package/dist/components/markdown/scrollable.js +152 -0
  135. package/dist/components/markdown/static.d.ts +37 -0
  136. package/dist/components/markdown/static.js +233 -0
  137. package/dist/components/markdown/styles.d.ts +328 -0
  138. package/dist/components/markdown/styles.js +302 -0
  139. package/dist/components/menu/MenuRadioItem.svelte +3 -3
  140. package/dist/components/menu/index.d.ts +23 -0
  141. package/dist/components/menu/index.js +3 -1
  142. package/dist/components/message/MessageAction.svelte +24 -5
  143. package/dist/components/message/MessageAction.svelte.d.ts +9 -0
  144. package/dist/components/message/MessageActions.svelte +2 -3
  145. package/dist/components/message/MessageContent.svelte +2 -3
  146. package/dist/components/message/MessageCopy.svelte +66 -8
  147. package/dist/components/message/MessageCopy.svelte.d.ts +17 -1
  148. package/dist/components/message/MessageHeader.svelte +3 -3
  149. package/dist/components/message/MessagePending.svelte +1 -1
  150. package/dist/components/message/MessageRoot.svelte +3 -4
  151. package/dist/components/message/MessageTime.svelte +3 -4
  152. package/dist/components/notice/Notice.svelte +14 -10
  153. package/dist/components/plan/Plan.svelte +25 -21
  154. package/dist/components/progress/Progress.svelte +34 -38
  155. package/dist/components/questionnaire/Questionnaire.svelte +19 -22
  156. package/dist/components/queued-message/QueuedMessage.svelte +14 -7
  157. package/dist/components/rail/RailItem.svelte +43 -40
  158. package/dist/components/rail/RailItem.svelte.d.ts +3 -1
  159. package/dist/components/rail/RailRoot.svelte +18 -4
  160. package/dist/components/rail/RailRoot.svelte.d.ts +1 -1
  161. package/dist/components/rail/RailSeparator.svelte +17 -0
  162. package/dist/components/rail/RailSeparator.svelte.d.ts +18 -0
  163. package/dist/components/rail/index.d.ts +2 -1
  164. package/dist/components/rail/index.js +2 -1
  165. package/dist/components/reasoning/ReasoningContent.svelte +4 -8
  166. package/dist/components/reasoning/ReasoningTrigger.svelte +4 -5
  167. package/dist/components/resize-handle/ResizeHandle.svelte +4 -7
  168. package/dist/components/reveal/Reveal.svelte +89 -0
  169. package/dist/components/reveal/Reveal.svelte.d.ts +17 -0
  170. package/dist/components/reveal/revealed.d.ts +38 -0
  171. package/dist/components/reveal/revealed.js +79 -0
  172. package/dist/components/segmented/SegmentedControl.svelte +11 -16
  173. package/dist/components/select/Select.svelte +1 -1
  174. package/dist/components/separator/Separator.svelte +1 -1
  175. package/dist/components/settings-row/SettingsRow.svelte +5 -5
  176. package/dist/components/sheet/SheetContent.svelte +63 -0
  177. package/dist/components/sheet/SheetContent.svelte.d.ts +18 -0
  178. package/dist/components/sheet/index.d.ts +12 -0
  179. package/dist/components/sheet/index.js +12 -0
  180. package/dist/components/sheet/styles.d.ts +51 -0
  181. package/dist/components/sheet/styles.js +65 -0
  182. package/dist/components/shimmer/Shimmer.svelte +41 -26
  183. package/dist/components/sidebar/SidebarContent.svelte +1 -1
  184. package/dist/components/sidebar/SidebarFolder.svelte +142 -0
  185. package/dist/components/sidebar/SidebarFolder.svelte.d.ts +19 -0
  186. package/dist/components/sidebar/SidebarFooter.svelte +1 -1
  187. package/dist/components/sidebar/SidebarGroup.svelte +41 -35
  188. package/dist/components/sidebar/SidebarGroup.svelte.d.ts +3 -3
  189. package/dist/components/sidebar/SidebarHeader.svelte +12 -6
  190. package/dist/components/sidebar/SidebarHeader.svelte.d.ts +4 -1
  191. package/dist/components/sidebar/SidebarItem.svelte +34 -38
  192. package/dist/components/sidebar/SidebarRoot.svelte +62 -0
  193. package/dist/components/sidebar/SidebarRoot.svelte.d.ts +14 -0
  194. package/dist/components/sidebar/index.d.ts +5 -1
  195. package/dist/components/sidebar/index.js +3 -1
  196. package/dist/components/skeleton/Skeleton.svelte +26 -15
  197. package/dist/components/slider/Slider.svelte +200 -0
  198. package/dist/components/slider/Slider.svelte.d.ts +55 -0
  199. package/dist/components/spinner/Spinner.svelte +4 -11
  200. package/dist/components/stack/Stack.svelte +37 -44
  201. package/dist/components/stack/Stack.svelte.d.ts +12 -6
  202. package/dist/components/status-indicator/StatusIndicator.svelte +1 -1
  203. package/dist/components/suggestions/SuggestionsItem.svelte +11 -13
  204. package/dist/components/switch/Switch.svelte +16 -25
  205. package/dist/components/tab-strip/TabStrip.svelte +340 -0
  206. package/dist/components/tab-strip/TabStrip.svelte.d.ts +31 -0
  207. package/dist/components/tabs/Tabs.svelte +15 -14
  208. package/dist/components/tabs/Tabs.svelte.d.ts +4 -0
  209. package/dist/components/text/Text.svelte +64 -14
  210. package/dist/components/text/Text.svelte.d.ts +13 -3
  211. package/dist/components/toast/Toaster.svelte +12 -10
  212. package/dist/components/toggle/Toggle.svelte +40 -0
  213. package/dist/components/toggle/Toggle.svelte.d.ts +15 -0
  214. package/dist/components/toggle-group/ToggleGroupItem.svelte +37 -0
  215. package/dist/components/toggle-group/ToggleGroupItem.svelte.d.ts +12 -0
  216. package/dist/components/toggle-group/ToggleGroupRoot.svelte +95 -0
  217. package/dist/components/toggle-group/ToggleGroupRoot.svelte.d.ts +33 -0
  218. package/dist/components/toggle-group/context.d.ts +6 -0
  219. package/dist/components/toggle-group/context.js +11 -0
  220. package/dist/components/toggle-group/index.d.ts +5 -0
  221. package/dist/components/toggle-group/index.js +3 -0
  222. package/dist/components/tool/ToolContent.svelte +3 -8
  223. package/dist/components/tool/ToolSection.svelte +4 -5
  224. package/dist/components/tool/ToolTrigger.svelte +14 -15
  225. package/dist/components/tooltip/TooltipContent.svelte +9 -6
  226. package/dist/components/work-fold/WorkFold.svelte +17 -34
  227. package/dist/components/work-fold/WorkFold.svelte.d.ts +1 -1
  228. package/dist/index.d.ts +38 -10
  229. package/dist/index.js +38 -12
  230. package/dist/internal/Chevron.svelte +2 -3
  231. package/dist/internal/Code.svelte +3 -4
  232. package/dist/internal/Collapse.svelte +6 -5
  233. package/dist/internal/Collapse.svelte.d.ts +2 -1
  234. package/dist/internal/Glyph.svelte +16 -0
  235. package/dist/internal/Glyph.svelte.d.ts +6 -0
  236. package/dist/internal/Tile.svelte +17 -37
  237. package/dist/internal/control.d.ts +4 -4
  238. package/dist/internal/control.js +21 -18
  239. package/dist/internal/layer.d.ts +3 -5
  240. package/dist/internal/layer.js +14 -18
  241. package/dist/internal/layout.d.ts +13 -0
  242. package/dist/internal/layout.js +453 -0
  243. package/dist/internal/markers.stylex.d.ts +8 -0
  244. package/dist/internal/markers.stylex.js +4 -0
  245. package/dist/internal/plugin-order.d.ts +8 -0
  246. package/dist/internal/plugin-order.js +16 -0
  247. package/dist/internal/responsive.d.ts +45 -0
  248. package/dist/internal/responsive.js +42 -0
  249. package/dist/internal/roving.js +5 -2
  250. package/dist/internal/shared.d.ts +8 -1
  251. package/dist/internal/shared.js +21 -5
  252. package/dist/internal/sx.d.ts +31 -4
  253. package/dist/internal/sx.js +17 -0
  254. package/dist/internal/toggle.d.ts +73 -0
  255. package/dist/internal/toggle.js +83 -0
  256. package/dist/internal/visible.d.ts +32 -0
  257. package/dist/internal/visible.js +44 -0
  258. package/dist/markdown/ast.d.ts +209 -0
  259. package/dist/markdown/ast.js +10 -0
  260. package/dist/markdown/blocks.d.ts +177 -0
  261. package/dist/markdown/blocks.js +1489 -0
  262. package/dist/markdown/code-info.d.ts +10 -0
  263. package/dist/markdown/code-info.js +16 -0
  264. package/dist/markdown/common.d.ts +25 -0
  265. package/dist/markdown/common.js +107 -0
  266. package/dist/markdown/diagrams.d.ts +2 -0
  267. package/dist/markdown/diagrams.js +18 -0
  268. package/dist/markdown/file-paths.d.ts +29 -0
  269. package/dist/markdown/file-paths.js +80 -0
  270. package/dist/markdown/files.d.ts +16 -0
  271. package/dist/markdown/files.js +133 -0
  272. package/dist/markdown/highlight.d.ts +30 -0
  273. package/dist/markdown/highlight.js +127 -0
  274. package/dist/markdown/html.d.ts +23 -0
  275. package/dist/markdown/html.js +289 -0
  276. package/dist/markdown/images.d.ts +15 -0
  277. package/dist/markdown/images.js +45 -0
  278. package/dist/markdown/index.d.ts +12 -0
  279. package/dist/markdown/index.js +5 -0
  280. package/dist/markdown/inlines.d.ts +48 -0
  281. package/dist/markdown/inlines.js +1714 -0
  282. package/dist/markdown/math.d.ts +2 -0
  283. package/dist/markdown/math.js +22 -0
  284. package/dist/markdown/parse.d.ts +81 -0
  285. package/dist/markdown/parse.js +204 -0
  286. package/dist/markdown/plugin.d.ts +178 -0
  287. package/dist/markdown/plugin.js +160 -0
  288. package/dist/markdown/sanitize.d.ts +78 -0
  289. package/dist/markdown/sanitize.js +522 -0
  290. package/dist/markdown/share.d.ts +12 -0
  291. package/dist/markdown/share.js +59 -0
  292. package/dist/markdown/table.d.ts +13 -0
  293. package/dist/markdown/table.js +60 -0
  294. package/dist/media.stylex.d.ts +52 -0
  295. package/dist/media.stylex.js +53 -0
  296. package/dist/preset.d.ts +40 -16
  297. package/dist/preset.js +169 -51
  298. package/dist/tokens.stylex.d.ts +70 -37
  299. package/dist/tokens.stylex.js +127 -62
  300. package/dist/typography.d.ts +48 -0
  301. package/dist/typography.js +55 -0
  302. package/dist/vite.d.ts +9 -1
  303. package/dist/vite.js +37 -4
  304. package/package.json +58 -6
  305. package/skills/building-ui/SKILL.md +242 -0
  306. package/skills/building-ui/references/components.md +207 -0
  307. package/skills/markdown/SKILL.md +452 -0
  308. package/skills/markdown/references/plugins.md +103 -0
  309. package/skills/setup/SKILL.md +191 -0
  310. package/skills/styling/SKILL.md +269 -0
  311. package/skills/theming/SKILL.md +184 -0
  312. package/dist/internal/loop.d.ts +0 -35
  313. package/dist/internal/loop.js +0 -72
@@ -0,0 +1,2 @@
1
+ import { type MarkdownPlugin } from './plugin.js';
2
+ export declare function math(): MarkdownPlugin;
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Math for Narvi's Markdown, typeset with KaTeX.
3
+ *
4
+ * import { math } from '@diegopluna/narvi/markdown/math';
5
+ * <Markdown value={text} plugins={[math()]} />
6
+ *
7
+ * It turns on formula syntax (`$x$`, `$$x$$`, `\(x\)`, `\[x\]`), which changes what a dollar
8
+ * sign means, under rules that leave prices and shell variables as text. KaTeX itself loads
9
+ * the first time a formula is shown.
10
+ */
11
+ import MarkdownMath from '../components/markdown/MarkdownMath.svelte';
12
+ import { withProps } from './plugin.js';
13
+ export function math() {
14
+ const component = withProps(MarkdownMath);
15
+ return {
16
+ name: 'math',
17
+ syntax: { math: true },
18
+ components: { math: component, inlineMath: component },
19
+ // A fenced block in the language `math` is a formula too
20
+ languages: { math: component }
21
+ };
22
+ }
@@ -0,0 +1,81 @@
1
+ /**
2
+ * Parsing a whole document, or one that arrives a piece at a time.
3
+ */
4
+ import type { Document } from './ast.js';
5
+ import { type BlockOptions } from './blocks.js';
6
+ export type ParseOptions = BlockOptions & {
7
+ /**
8
+ * Reads YAML front matter: a file that starts with a line of `---`, then `key: value`
9
+ * lines, then a line of `---` or `...`. It is kept as `frontmatter` on the document and
10
+ * left out of what is drawn. Off by default: a reply that starts with `---` starts with a
11
+ * rule. Turn it on for files.
12
+ */
13
+ frontmatter?: boolean;
14
+ };
15
+ /**
16
+ * A document that grows as text arrives. Each complete line is read once; `snapshot()` shows
17
+ * the document as it stands, with the unfinished last line and every open block previewed on
18
+ * a copy, so nothing already read is read again.
19
+ *
20
+ * Blocks that are finished keep their `id` and their object from one snapshot to the next.
21
+ * So does every part of the unfinished end that says what it said before (`share.ts`): of a
22
+ * paragraph that grew by a word, the text at its end is new, and the emphasis, code and links
23
+ * before it are the objects they were.
24
+ */
25
+ export declare class MarkdownStream {
26
+ private parser;
27
+ private text;
28
+ /** Where the line that is not finished yet starts. */
29
+ private pending;
30
+ private finished;
31
+ /** The last snapshot, which the next one shares what it can with. */
32
+ private last;
33
+ private options;
34
+ /**
35
+ * Front matter in the making. `start` is before the first line; `maybe` after an opening
36
+ * `---`; `open` once the next line reads as YAML; `closed` at its end; `none` when the
37
+ * file has none. Until it closes its lines are held here, in case it never does: then
38
+ * they are a rule and text after all.
39
+ */
40
+ private front;
41
+ private held;
42
+ private frontmatter;
43
+ constructor(options?: ParseOptions);
44
+ /** The text read so far. */
45
+ get value(): string;
46
+ /** Adds text to the end. */
47
+ append(chunk: string): this;
48
+ /**
49
+ * Sets the whole text. When it continues the text so far, only the new part is read;
50
+ * otherwise the document is read again from the start.
51
+ */
52
+ set(text: string): this;
53
+ private readLines;
54
+ /** Takes one whole line: into front matter while one is open, to the parser otherwise. */
55
+ private readLine;
56
+ /** Hands the lines held for front matter to a parser, as the Markdown they turned out to be. */
57
+ private releaseHeld;
58
+ private withFrontmatter;
59
+ /**
60
+ * The document as it stands, with the unfinished end previewed. Changes nothing.
61
+ *
62
+ * With `tidy`, the end reads as it most likely will once written: emphasis that has started
63
+ * is closed, a link waiting for its destination shows as text, and what is still ambiguous
64
+ * is held back, such as a bare marker on the last line or a table's header row before its
65
+ * delimiter row. Only the unfinished end is tidied, never text that is complete.
66
+ *
67
+ * With `final`, the text is read as all there is, as `finish()` reads it, but the stream
68
+ * stays open: nothing is marked as still being written, and a formula that never closed
69
+ * is the text it was written as.
70
+ */
71
+ snapshot({ tidy, final }?: {
72
+ tidy?: boolean;
73
+ final?: boolean;
74
+ }): Document;
75
+ /** A snapshot with all that the one before it said too kept as the same objects. */
76
+ private shared;
77
+ /** Ends the document: every open block closes for good. */
78
+ finish(): Document;
79
+ }
80
+ /** Parses a whole document. */
81
+ export declare function parse(text: string, options?: ParseOptions): Document;
@@ -0,0 +1,204 @@
1
+ import { BlockParser } from './blocks.js';
2
+ import { shareDocument } from './share.js';
3
+ const reFrontOpen = /^---[ \t]*$/;
4
+ const reFrontClose = /^(?:---|\.\.\.)[ \t]*$/;
5
+ // What the line after the opening `---` is when front matter follows: a key, a comment, or
6
+ // the end of an empty one. Text under a rule is none of these.
7
+ const reFrontFirst = /^(?:[A-Za-z_][\w.-]*[ \t]*:|#|(?:---|\.\.\.)[ \t]*$)/;
8
+ const reLineEnding = /\r\n|\n|\r/g;
9
+ // A line that so far holds only what blocks start with: markers and the spaces between
10
+ // them (`- *`, `> -`, `* **`), a list's number with its `.` or `)` (a number alone is a
11
+ // number), and a task's box in the making (`- [x`). Any of them may turn into something else with the next
12
+ // character, so none is drawn yet.
13
+ const reMarkerOnly = /^(?=.*[^ \t])[-*+=#>|_~` \t]*(?:\d{1,9}[.)][-*+=#>|_~` \t]*)?(?:\[[ xX]?\]?[ \t]*)?$/;
14
+ /**
15
+ * A document that grows as text arrives. Each complete line is read once; `snapshot()` shows
16
+ * the document as it stands, with the unfinished last line and every open block previewed on
17
+ * a copy, so nothing already read is read again.
18
+ *
19
+ * Blocks that are finished keep their `id` and their object from one snapshot to the next.
20
+ * So does every part of the unfinished end that says what it said before (`share.ts`): of a
21
+ * paragraph that grew by a word, the text at its end is new, and the emphasis, code and links
22
+ * before it are the objects they were.
23
+ */
24
+ export class MarkdownStream {
25
+ parser;
26
+ text = '';
27
+ /** Where the line that is not finished yet starts. */
28
+ pending = 0;
29
+ finished = null;
30
+ /** The last snapshot, which the next one shares what it can with. */
31
+ last = null;
32
+ options;
33
+ /**
34
+ * Front matter in the making. `start` is before the first line; `maybe` after an opening
35
+ * `---`; `open` once the next line reads as YAML; `closed` at its end; `none` when the
36
+ * file has none. Until it closes its lines are held here, in case it never does: then
37
+ * they are a rule and text after all.
38
+ */
39
+ front = 'none';
40
+ held = [];
41
+ frontmatter = null;
42
+ constructor(options = {}) {
43
+ this.options = options;
44
+ this.parser = new BlockParser(options);
45
+ this.front = options.frontmatter ? 'start' : 'none';
46
+ }
47
+ /** The text read so far. */
48
+ get value() {
49
+ return this.text;
50
+ }
51
+ /** Adds text to the end. */
52
+ append(chunk) {
53
+ if (chunk.length === 0)
54
+ return this;
55
+ if (this.finished)
56
+ throw new Error('This stream is finished');
57
+ this.text += chunk;
58
+ this.readLines();
59
+ return this;
60
+ }
61
+ /**
62
+ * Sets the whole text. When it continues the text so far, only the new part is read;
63
+ * otherwise the document is read again from the start.
64
+ */
65
+ set(text) {
66
+ // Compared as slices, which the engine does a block at a time: `startsWith` reads every
67
+ // character, and this runs on every update of a reply that streams
68
+ if (!this.finished &&
69
+ text.length >= this.text.length &&
70
+ text.slice(0, this.text.length) === this.text)
71
+ return this.append(text.slice(this.text.length));
72
+ this.parser = new BlockParser(this.options);
73
+ this.text = '';
74
+ this.pending = 0;
75
+ this.finished = null;
76
+ // Read from the start again: every block is new, and none is measured against the old
77
+ this.last = null;
78
+ this.front = this.options.frontmatter ? 'start' : 'none';
79
+ this.held = [];
80
+ this.frontmatter = null;
81
+ return this.append(text);
82
+ }
83
+ readLines() {
84
+ const text = this.text;
85
+ reLineEnding.lastIndex = this.pending;
86
+ let m;
87
+ while ((m = reLineEnding.exec(text))) {
88
+ // A `\r` at the very end may be the first half of `\r\n`
89
+ if (m[0] === '\r' && m.index === text.length - 1)
90
+ break;
91
+ this.readLine(text.slice(this.pending, m.index), this.pending);
92
+ this.pending = m.index + m[0].length;
93
+ }
94
+ }
95
+ /** Takes one whole line: into front matter while one is open, to the parser otherwise. */
96
+ readLine(line, start) {
97
+ if (this.front === 'none' || this.front === 'closed') {
98
+ this.parser.incorporateLine(line, start);
99
+ return;
100
+ }
101
+ if (this.front === 'start') {
102
+ if (reFrontOpen.test(line)) {
103
+ this.front = 'maybe';
104
+ this.held.push([line, start]);
105
+ }
106
+ else {
107
+ this.front = 'none';
108
+ this.parser.incorporateLine(line, start);
109
+ }
110
+ return;
111
+ }
112
+ if (this.front === 'maybe') {
113
+ if (!reFrontFirst.test(line)) {
114
+ // A rule with text under it, not front matter
115
+ this.held.push([line, start]);
116
+ this.releaseHeld(this.parser);
117
+ this.front = 'none';
118
+ return;
119
+ }
120
+ this.front = 'open';
121
+ }
122
+ if (reFrontClose.test(line)) {
123
+ this.frontmatter = this.held
124
+ .slice(1)
125
+ .map(([text]) => text)
126
+ .join('\n');
127
+ this.held = [];
128
+ this.front = 'closed';
129
+ }
130
+ else {
131
+ this.held.push([line, start]);
132
+ }
133
+ }
134
+ /** Hands the lines held for front matter to a parser, as the Markdown they turned out to be. */
135
+ releaseHeld(parser) {
136
+ for (const [line, start] of this.held)
137
+ parser.incorporateLine(line, start);
138
+ if (parser === this.parser)
139
+ this.held = [];
140
+ }
141
+ withFrontmatter(document) {
142
+ return this.frontmatter === null ? document : { ...document, frontmatter: this.frontmatter };
143
+ }
144
+ /**
145
+ * The document as it stands, with the unfinished end previewed. Changes nothing.
146
+ *
147
+ * With `tidy`, the end reads as it most likely will once written: emphasis that has started
148
+ * is closed, a link waiting for its destination shows as text, and what is still ambiguous
149
+ * is held back, such as a bare marker on the last line or a table's header row before its
150
+ * delimiter row. Only the unfinished end is tidied, never text that is complete.
151
+ *
152
+ * With `final`, the text is read as all there is, as `finish()` reads it, but the stream
153
+ * stays open: nothing is marked as still being written, and a formula that never closed
154
+ * is the text it was written as.
155
+ */
156
+ snapshot({ tidy = false, final = false } = {}) {
157
+ if (this.finished)
158
+ return this.finished;
159
+ const preview = this.parser.fork();
160
+ // Front matter that has not closed: nothing of it is drawn while more may come. Read
161
+ // as final, it never closed, and is a rule and the text under it.
162
+ const waiting = this.front === 'maybe' || this.front === 'open';
163
+ if (waiting && !final)
164
+ return this.shared(preview.toDocument(this.parser, tidy));
165
+ if (waiting)
166
+ this.releaseHeld(preview);
167
+ let rest = this.text.slice(this.pending).replace(/\r$/, '');
168
+ // A line that so far is only a marker could still become a list, a heading or a rule
169
+ if (tidy && reMarkerOnly.test(rest))
170
+ rest = '';
171
+ if (rest.length > 0)
172
+ preview.incorporateLine(rest, this.pending);
173
+ if (!final)
174
+ preview.markTail();
175
+ preview.closeAll();
176
+ return this.shared(this.withFrontmatter(preview.toDocument(this.parser, tidy)));
177
+ }
178
+ /** A snapshot with all that the one before it said too kept as the same objects. */
179
+ shared(document) {
180
+ return (this.last = shareDocument(this.last, document));
181
+ }
182
+ /** Ends the document: every open block closes for good. */
183
+ finish() {
184
+ if (this.finished)
185
+ return this.finished;
186
+ if (this.front === 'maybe' || this.front === 'open') {
187
+ this.releaseHeld(this.parser);
188
+ this.front = 'none';
189
+ }
190
+ const rest = this.text.slice(this.pending).replace(/\r$/, '');
191
+ if (rest.length > 0 || this.text.endsWith('\r')) {
192
+ if (rest.length > 0)
193
+ this.parser.incorporateLine(rest, this.pending);
194
+ this.pending = this.text.length;
195
+ }
196
+ this.parser.closeAll();
197
+ this.finished = this.withFrontmatter(this.parser.toDocument());
198
+ return this.finished;
199
+ }
200
+ }
201
+ /** Parses a whole document. */
202
+ export function parse(text, options = {}) {
203
+ return new MarkdownStream(options).append(text).finish();
204
+ }
@@ -0,0 +1,178 @@
1
+ /**
2
+ * What a plugin can add to Narvi's Markdown, and the default policy for links and images.
3
+ */
4
+ import type { Component } from 'svelte';
5
+ import type { Block, Document, Image, Inline, Link } from './ast.js';
6
+ /**
7
+ * A piece of highlighted code. `kind` names what it is (`keyword`, `string`, `comment`…) and
8
+ * takes Narvi's colour for that kind; `color` is a CSS colour of the highlighter's own, from
9
+ * an editor theme, and wins over the kind.
10
+ */
11
+ export type HighlightToken = {
12
+ text: string;
13
+ kind?: string;
14
+ color?: string;
15
+ italic?: boolean;
16
+ bold?: boolean;
17
+ };
18
+ /** Turns code into lines of tokens, or returns null to show it plain. */
19
+ export type Highlighter = (code: string, lang: string | null) => HighlightToken[][] | null;
20
+ /**
21
+ * Highlights code that arrives in pieces, keeping its place between them, so a block that
22
+ * streams is never tokenized again from the top.
23
+ */
24
+ export type HighlightStream = {
25
+ /** Takes more code, and returns the lines it completed. */
26
+ push(chunk: string): HighlightToken[][];
27
+ /** Ends the code, and returns the lines that were left. */
28
+ end(): HighlightToken[][];
29
+ /** Frees what the stream holds. */
30
+ dispose(): void;
31
+ };
32
+ /** The props a component that replaces a node type receives. */
33
+ export type NodeProps<N> = {
34
+ node: N;
35
+ /** Renders the node's children as Narvi would. */
36
+ children?: import('svelte').Snippet;
37
+ };
38
+ /** A component for a node, with extra props it is given beside `node` and `children`. */
39
+ export type PluginComponent = {
40
+ component: Component<NodeProps<never>>;
41
+ props: Record<string, unknown>;
42
+ };
43
+ /**
44
+ * A plugin's component, typed for the node it draws, with the extra props it takes:
45
+ * `components: { file: withProps(FileChip, { open }) }`.
46
+ */
47
+ export declare function withProps<Props extends Record<string, unknown>>(component: Component<Props>, props?: Omit<Props, 'node' | 'children'>): PluginComponent;
48
+ export type LinkPolicy = {
49
+ /**
50
+ * The address to link to, or null to show the link's text without a link. The default
51
+ * allows `http:`, `https:`, `mailto:`, `tel:`, and addresses without a scheme. On a page
52
+ * that is not on the web (a desktop app on `file:` or a scheme of its own), an address
53
+ * without a scheme is a file on the machine, and is allowed only when the app gives `open`.
54
+ *
55
+ * `url` is the address as a browser will read it (see `readUrl`). It may still be
56
+ * `//host/…`, which starts with `/` and leaves the site: test `readUrl(url).kind`, not the
57
+ * start of the string. A `javascript:` address is refused whatever this returns.
58
+ */
59
+ href?: (url: string, node: Link) => string | null;
60
+ /**
61
+ * The address to load an image from, or null to show its alt text instead. The default
62
+ * allows `http:`, `https:`, and addresses without a scheme. A remote image is requested as
63
+ * soon as it is shown, so text written by a model can send data out through its address:
64
+ * an app that renders such text should decide here which hosts it loads.
65
+ */
66
+ image?: (url: string, node: Image) => string | null;
67
+ /** Whether a link opens in a new tab. The default: links to another origin do. */
68
+ external?: (href: string) => boolean;
69
+ /**
70
+ * Called when a link is clicked, before the browser follows it. Call
71
+ * `event.preventDefault()` to open it yourself: in a side panel, an in-app browser, or
72
+ * the app's own router. Modifier keys are on the event.
73
+ */
74
+ open?: (href: string, event: MouseEvent) => void;
75
+ };
76
+ /** A policy with every decision filled in; `open` stays optional. */
77
+ export type ResolvedLinkPolicy = Required<Omit<LinkPolicy, 'open'>> & Pick<LinkPolicy, 'open'>;
78
+ export type MarkdownPlugin = {
79
+ name: string;
80
+ /** Syntax to read beyond CommonMark and GFM. */
81
+ syntax?: {
82
+ /** Formulas: `$x$`, `$$x$$`, `\\(x\\)` and `\\[x\\]`, as `inlineMath` and `math` nodes. */
83
+ math?: boolean;
84
+ };
85
+ /**
86
+ * Rewrites a block after it is parsed: for example, turning `@thread:12` in its text into
87
+ * a link. It is called for the blocks that hold text or code (paragraphs, headings, tables,
88
+ * code blocks, wherever they are nested), and never for the lists and quotes around them.
89
+ * A finished block is transformed once; return the same block to change nothing.
90
+ */
91
+ transform?: (block: Block, document: Document) => Block;
92
+ /**
93
+ * Components that replace Narvi's for node types, and that draw the `custom` nodes a
94
+ * transform adds, by their `name`. Use `withProps` to hand one extra props.
95
+ */
96
+ components?: Record<string, Component<NodeProps<never>> | PluginComponent>;
97
+ /**
98
+ * Components that draw a fenced code block in a language, by its name in lower case:
99
+ * `{ mermaid: Diagram }`. One takes over once the fence has closed; while the block is
100
+ * still being written it shows as code.
101
+ */
102
+ languages?: Record<string, Component<NodeProps<never>> | PluginComponent>;
103
+ /** Colours code blocks. */
104
+ highlight?: Highlighter;
105
+ /**
106
+ * Colours a code block while it streams. Returns null for a language it does not know.
107
+ * Without it, a streaming block is highlighted once its fence closes.
108
+ */
109
+ highlightStream?: (lang: string | null) => HighlightStream | null;
110
+ /** Decides links and images. Later plugins override earlier ones, one function at a time. */
111
+ links?: LinkPolicy;
112
+ /**
113
+ * For `static`: how to write the nodes this plugin draws as markup, by the same names as
114
+ * `components`. A finished block that holds one of these nodes can then be written as
115
+ * markup too; without it, the block is built from parts as ever. It is used only while
116
+ * this plugin's own component is the one that draws the node.
117
+ */
118
+ static?: Record<string, StaticPart<never>>;
119
+ };
120
+ /** What a `static` writer is given to work with. */
121
+ export type StaticTools = {
122
+ /** Escapes text for markup, and for an attribute value in double quotes. */
123
+ escape: (text: string) => string;
124
+ /** The node's own children, as markup. Throws nothing: a child that needs a part ends the attempt. */
125
+ children: () => string;
126
+ /** The attribute that marks an element as this part's, so that its clicks reach `click`. */
127
+ part: string;
128
+ };
129
+ /**
130
+ * A node as markup, for `static`. The plugin is trusted code: everything it writes is put
131
+ * on the page as it is, so every piece of text and every attribute value from the node must
132
+ * go through `tools.escape`.
133
+ */
134
+ export type StaticPart<N> = {
135
+ /** The node as markup, or null to have the plugin's component draw it. */
136
+ html: (node: N, tools: StaticTools) => string | null;
137
+ /** Called for a click, or a middle click, on an element that carries `tools.part`. */
138
+ click?: (element: HTMLElement, event: MouseEvent) => void;
139
+ };
140
+ /**
141
+ * An address as a browser will read it. A browser drops tabs and line endings from anywhere
142
+ * in an address, and control characters and spaces from its ends, before it looks for the
143
+ * scheme: `java\tscript:` is `javascript:`. So does this, and every check is made on the
144
+ * result, which is also what gets rendered.
145
+ *
146
+ * `kind` says where the address goes: `relative` stays on the page's own site; `network` is
147
+ * `//host/…`, which borrows the page's scheme; `scheme` has one of its own; and `unsafe` is
148
+ * an address with backslashes at its start, which a browser reads as `//host` and which on
149
+ * a `file:` page is a network share.
150
+ */
151
+ export declare function readUrl(url: string): {
152
+ url: string;
153
+ scheme: string | null;
154
+ kind: 'relative' | 'network' | 'scheme' | 'unsafe';
155
+ };
156
+ /**
157
+ * The default decision for a link. `followRelative` says whether an address on the page's
158
+ * own site may be followed when the page is not on the web. There, in a desktop app on a
159
+ * `file:` page or a scheme of its own, `/Users/me/report.html` is a file on the machine, and
160
+ * following it puts that file in the app's window. So such a link is refused unless the app
161
+ * opens links itself. A link to a place in the same page (`#name`) is always allowed, and a
162
+ * link with no address is never one.
163
+ */
164
+ export declare function defaultHref(url: string, followRelative: boolean): string | null;
165
+ export declare const defaultLinkPolicy: ResolvedLinkPolicy;
166
+ /**
167
+ * Calls `fn` on every inline node of a block, outermost first, and puts back what it returns.
168
+ * Return null to leave a node as it is; its children are then visited too. `inLink` says
169
+ * whether the node sits inside a link, which cannot hold another.
170
+ */
171
+ export declare function mapInlines(block: Block, fn: (node: Inline, context: {
172
+ inLink: boolean;
173
+ }) => Inline[] | null): Block;
174
+ /**
175
+ * Calls `fn` on every text node of a block, and puts back what it returns. Code is not text,
176
+ * and text inside a link is left alone, so what `fn` returns may hold links.
177
+ */
178
+ export declare function mapText(block: Block, fn: (value: string) => Inline[] | null): Block;
@@ -0,0 +1,160 @@
1
+ /**
2
+ * A plugin's component, typed for the node it draws, with the extra props it takes:
3
+ * `components: { file: withProps(FileChip, { open }) }`.
4
+ */
5
+ export function withProps(component, props = {}) {
6
+ return { component: component, props };
7
+ }
8
+ const reScheme = /^([a-z][a-z0-9+.-]*):/i;
9
+ // Tabs and line endings: the characters a browser strips from inside an address
10
+ const reStripped = /[\t\n\r]/g;
11
+ // eslint-disable-next-line no-control-regex
12
+ const reEdges = /^[\x00-\x20]+|[\x00-\x20]+$/g;
13
+ /**
14
+ * An address as a browser will read it. A browser drops tabs and line endings from anywhere
15
+ * in an address, and control characters and spaces from its ends, before it looks for the
16
+ * scheme: `java\tscript:` is `javascript:`. So does this, and every check is made on the
17
+ * result, which is also what gets rendered.
18
+ *
19
+ * `kind` says where the address goes: `relative` stays on the page's own site; `network` is
20
+ * `//host/…`, which borrows the page's scheme; `scheme` has one of its own; and `unsafe` is
21
+ * an address with backslashes at its start, which a browser reads as `//host` and which on
22
+ * a `file:` page is a network share.
23
+ */
24
+ export function readUrl(url) {
25
+ const cleaned = url.replace(reStripped, '').replace(reEdges, '');
26
+ const scheme = cleaned.match(reScheme)?.[1]?.toLowerCase() ?? null;
27
+ if (scheme)
28
+ return { url: cleaned, scheme, kind: 'scheme' };
29
+ if (/^[\\/]{2}/.test(cleaned)) {
30
+ return { url: cleaned, scheme: null, kind: cleaned.startsWith('//') ? 'network' : 'unsafe' };
31
+ }
32
+ return { url: cleaned, scheme: null, kind: 'relative' };
33
+ }
34
+ /** Whether the page itself is on the web, where `//host/…` means another web host. */
35
+ function onTheWeb() {
36
+ return typeof location === 'undefined' || /^https?:$/.test(location.protocol);
37
+ }
38
+ /** An address with one of the schemes given, or none, as it may be rendered; null otherwise. */
39
+ function allow(url, schemes) {
40
+ const read = readUrl(url);
41
+ if (read.kind === 'unsafe')
42
+ return null;
43
+ // On a `file:` or app page, `//host/…` is not the web: it is a share, or the app's own scheme
44
+ if (read.kind === 'network')
45
+ return onTheWeb() ? read.url : null;
46
+ if (read.kind === 'scheme')
47
+ return schemes.includes(read.scheme) ? read.url : null;
48
+ return read.url;
49
+ }
50
+ const LINK_SCHEMES = ['http', 'https', 'mailto', 'tel'];
51
+ /**
52
+ * The default decision for a link. `followRelative` says whether an address on the page's
53
+ * own site may be followed when the page is not on the web. There, in a desktop app on a
54
+ * `file:` page or a scheme of its own, `/Users/me/report.html` is a file on the machine, and
55
+ * following it puts that file in the app's window. So such a link is refused unless the app
56
+ * opens links itself. A link to a place in the same page (`#name`) is always allowed, and a
57
+ * link with no address is never one.
58
+ */
59
+ export function defaultHref(url, followRelative) {
60
+ const href = allow(url, LINK_SCHEMES);
61
+ if (href === null || href === '')
62
+ return null;
63
+ if (followRelative || href.startsWith('#') || onTheWeb())
64
+ return href;
65
+ return readUrl(href).kind === 'relative' ? null : href;
66
+ }
67
+ export const defaultLinkPolicy = {
68
+ href: (url) => defaultHref(url, false),
69
+ image: (url) => allow(url, ['http', 'https']),
70
+ external(href) {
71
+ const read = readUrl(href);
72
+ if (read.kind === 'relative')
73
+ return false;
74
+ if (read.kind === 'scheme' && read.scheme !== 'http' && read.scheme !== 'https')
75
+ return false;
76
+ if (typeof location === 'undefined')
77
+ return true;
78
+ try {
79
+ return new URL(read.url, location.href).origin !== location.origin;
80
+ }
81
+ catch {
82
+ return true;
83
+ }
84
+ }
85
+ };
86
+ /**
87
+ * Calls `fn` on every inline node of a block, outermost first, and puts back what it returns.
88
+ * Return null to leave a node as it is; its children are then visited too. `inLink` says
89
+ * whether the node sits inside a link, which cannot hold another.
90
+ */
91
+ export function mapInlines(block, fn) {
92
+ const mapInlines = (inlines, inLink = false) => {
93
+ let changed = false;
94
+ const out = [];
95
+ for (const node of inlines) {
96
+ const replaced = fn(node, { inLink });
97
+ if (replaced) {
98
+ changed = true;
99
+ out.push(...replaced);
100
+ }
101
+ else if ('children' in node && node.type !== 'image') {
102
+ const children = mapInlines(node.children, inLink || node.type === 'link');
103
+ if (children !== node.children) {
104
+ changed = true;
105
+ out.push({ ...node, children });
106
+ }
107
+ else {
108
+ out.push(node);
109
+ }
110
+ }
111
+ else {
112
+ out.push(node);
113
+ }
114
+ }
115
+ return changed ? out : inlines;
116
+ };
117
+ const mapBlock = (b) => {
118
+ switch (b.type) {
119
+ case 'paragraph':
120
+ case 'heading': {
121
+ const children = mapInlines(b.children);
122
+ return children === b.children ? b : { ...b, children };
123
+ }
124
+ case 'blockquote':
125
+ case 'listItem':
126
+ case 'footnoteDefinition': {
127
+ const children = b.children.map(mapBlock);
128
+ return children.every((c, i) => c === b.children[i]) ? b : { ...b, children };
129
+ }
130
+ case 'list': {
131
+ const children = b.children.map(mapBlock);
132
+ return children.every((c, i) => c === b.children[i]) ? b : { ...b, children };
133
+ }
134
+ case 'table': {
135
+ let changed = false;
136
+ const rows = b.children.map((row) => {
137
+ const cells = row.children.map((cell) => {
138
+ const children = mapInlines(cell.children);
139
+ if (children === cell.children)
140
+ return cell;
141
+ changed = true;
142
+ return { ...cell, children };
143
+ });
144
+ return cells.every((c, i) => c === row.children[i]) ? row : { ...row, children: cells };
145
+ });
146
+ return changed ? { ...b, children: rows } : b;
147
+ }
148
+ default:
149
+ return b;
150
+ }
151
+ };
152
+ return mapBlock(block);
153
+ }
154
+ /**
155
+ * Calls `fn` on every text node of a block, and puts back what it returns. Code is not text,
156
+ * and text inside a link is left alone, so what `fn` returns may hold links.
157
+ */
158
+ export function mapText(block, fn) {
159
+ return mapInlines(block, (node, { inLink }) => node.type === 'text' && !inLink ? fn(node.value) : null);
160
+ }