@diegopluna/narvi 0.5.9 → 0.5.10

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 (33) hide show
  1. package/dist/components/markdown/Markdown.svelte +68 -7
  2. package/dist/components/markdown/Markdown.svelte.d.ts +9 -0
  3. package/dist/components/markdown/MarkdownBlocks.svelte +4 -3
  4. package/dist/components/markdown/MarkdownCode.svelte +108 -9
  5. package/dist/components/markdown/MarkdownCode.svelte.d.ts +5 -1
  6. package/dist/components/markdown/MarkdownHtmlPreview.svelte +186 -0
  7. package/dist/components/markdown/MarkdownHtmlPreview.svelte.d.ts +11 -0
  8. package/dist/components/markdown/MarkdownPreview.svelte +17 -0
  9. package/dist/components/markdown/MarkdownPreview.svelte.d.ts +6 -0
  10. package/dist/components/markdown/MarkdownTable.svelte +61 -4
  11. package/dist/components/markdown/context.d.ts +21 -0
  12. package/dist/components/markdown/labels.d.ts +12 -0
  13. package/dist/components/markdown/labels.js +5 -0
  14. package/dist/components/markdown/preview.d.ts +9 -0
  15. package/dist/components/markdown/preview.js +14 -0
  16. package/dist/components/markdown/static.d.ts +2 -0
  17. package/dist/components/markdown/static.js +5 -1
  18. package/dist/components/markdown/table-image.d.ts +15 -0
  19. package/dist/components/markdown/table-image.js +342 -0
  20. package/dist/components/message/MessageAction.svelte +17 -2
  21. package/dist/components/message/MessageAction.svelte.d.ts +7 -0
  22. package/dist/markdown/html.d.ts +3 -1
  23. package/dist/markdown/html.js +6 -0
  24. package/dist/markdown/index.d.ts +2 -2
  25. package/dist/markdown/index.js +2 -2
  26. package/dist/markdown/table.d.ts +20 -1
  27. package/dist/markdown/table.js +111 -0
  28. package/package.json +1 -1
  29. package/skills/building-ui/SKILL.md +1 -1
  30. package/skills/markdown/SKILL.md +24 -5
  31. package/skills/setup/SKILL.md +1 -1
  32. package/skills/styling/SKILL.md +1 -1
  33. package/skills/theming/SKILL.md +1 -1
@@ -1,4 +1,4 @@
1
- import type { Document } from './ast.js';
1
+ import type { Document, Inline } from './ast.js';
2
2
  export type HtmlOptions = {
3
3
  /** Writes raw HTML as text instead of passing it through. Default `true` (pass through). */
4
4
  rawHtml?: boolean | undefined;
@@ -11,4 +11,6 @@ export type HtmlOptions = {
11
11
  safeLinks?: boolean | undefined;
12
12
  };
13
13
  export declare function escapeHtml(s: string): string;
14
+ /** Inline nodes as HTML, as `toHtml` writes them inside a block. */
15
+ export declare function inlinesToHtml(nodes: Inline[], options?: HtmlOptions): string;
14
16
  export declare function toHtml(doc: Document, options?: HtmlOptions): string;
@@ -279,6 +279,12 @@ class Writer {
279
279
  this.write('</ol>\n</section>\n');
280
280
  }
281
281
  }
282
+ /** Inline nodes as HTML, as `toHtml` writes them inside a block. */
283
+ export function inlinesToHtml(nodes, options = {}) {
284
+ const writer = new Writer({ rawHtml: true, tagFilter: true, safeLinks: false, ...options });
285
+ writer.inlines(nodes);
286
+ return writer.out;
287
+ }
282
288
  export function toHtml(doc, options = {}) {
283
289
  const writer = new Writer({ rawHtml: true, tagFilter: true, safeLinks: false, ...options });
284
290
  for (const f of doc.footnotes)
@@ -6,7 +6,7 @@
6
6
  */
7
7
  export type * from './ast.js';
8
8
  export { MarkdownStream, parse, type ParseOptions } from './parse.js';
9
- export { escapeHtml, toHtml, type HtmlOptions } from './html.js';
10
- export { inlineText, tableToCsv, tableToMarkdown } from './table.js';
9
+ export { escapeHtml, inlinesToHtml, toHtml, type HtmlOptions } from './html.js';
10
+ export { displayWidth, inlineText, tableToCsv, tableToHtml, tableToMarkdown, tableToText } from './table.js';
11
11
  export { defaultLinkPolicy, mapInlines, mapText, readUrl, withProps, type HighlightStream, type HighlightToken, type Highlighter, type LinkPolicy, type MarkdownPlugin, type NodeProps, type PluginComponent, type StaticPart, type StaticTools, type ResolvedLinkPolicy } from './plugin.js';
12
12
  export { groupBlockHtml, groupInlineHtml, sanitizeHtml, type HtmlElement, type HtmlGroup, type HtmlInlineElement, type HtmlMarkdown, type HtmlNode, type HtmlText, type RenderBlock, type RenderInline, type UrlPolicy } from './sanitize.js';
@@ -1,5 +1,5 @@
1
1
  export { MarkdownStream, parse } from './parse.js';
2
- export { escapeHtml, toHtml } from './html.js';
3
- export { inlineText, tableToCsv, tableToMarkdown } from './table.js';
2
+ export { escapeHtml, inlinesToHtml, toHtml } from './html.js';
3
+ export { displayWidth, inlineText, tableToCsv, tableToHtml, tableToMarkdown, tableToText } from './table.js';
4
4
  export { defaultLinkPolicy, mapInlines, mapText, readUrl, withProps } from './plugin.js';
5
5
  export { groupBlockHtml, groupInlineHtml, sanitizeHtml } from './sanitize.js';
@@ -1,5 +1,6 @@
1
1
  /**
2
- * A table, written out again: as Markdown, or as CSV for a spreadsheet.
2
+ * A table, written out again: as Markdown, as CSV for a spreadsheet, as HTML for a rich-text
3
+ * editor, or as text whose columns line up.
3
4
  */
4
5
  import type { Inline, Table } from './ast.js';
5
6
  /** The text of inline nodes, without their formatting. A formula is its TeX. */
@@ -11,3 +12,21 @@ export declare function tableToMarkdown(table: Table): string;
11
12
  * separator or a line ending, and kept from being read as formulas. `separator` is a comma by default; a tab makes TSV.
12
13
  */
13
14
  export declare function tableToCsv(table: Table, separator?: string): string;
15
+ /**
16
+ * The table as HTML, for a rich-text editor: a `<table>` with a head and a body, each cell's
17
+ * alignment as `align` and as `text-align`, and its emphasis, code and links kept. Raw HTML in
18
+ * a cell is written as text, and a link or image the default policy refuses is its words.
19
+ */
20
+ export declare function tableToHtml(table: Table): string;
21
+ /**
22
+ * How many columns text takes in a monospace font, as a terminal draws it: two for a CJK
23
+ * character or an emoji, none for a combining mark or a zero-width space, one for the rest.
24
+ */
25
+ export declare function displayWidth(text: string): number;
26
+ /**
27
+ * The table as text whose columns line up in a monospace font: for a terminal, a commit
28
+ * message or a chat that takes no formatting. Columns are two spaces apart, the head is
29
+ * underlined with dashes, and each column is aligned as the table says, counting wide
30
+ * characters as two columns. Lines carry no spaces at their end.
31
+ */
32
+ export declare function tableToText(table: Table): string;
@@ -1,3 +1,4 @@
1
+ import { inlinesToHtml } from './html.js';
1
2
  /** The text of inline nodes, without their formatting. A formula is its TeX. */
2
3
  export function inlineText(nodes) {
3
4
  let out = '';
@@ -58,3 +59,113 @@ export function tableToCsv(table, separator = ',') {
58
59
  .map((r) => r.children.map((c) => cell(inlineText(c.children))).join(separator))
59
60
  .join('\r\n');
60
61
  }
62
+ /**
63
+ * The table as HTML, for a rich-text editor: a `<table>` with a head and a body, each cell's
64
+ * alignment as `align` and as `text-align`, and its emphasis, code and links kept. Raw HTML in
65
+ * a cell is written as text, and a link or image the default policy refuses is its words.
66
+ */
67
+ export function tableToHtml(table) {
68
+ const [head, ...body] = table.children;
69
+ if (!head)
70
+ return '';
71
+ const row = (cells, tag) => '<tr>' +
72
+ cells.children
73
+ .map((cell, i) => {
74
+ const align = table.align[i];
75
+ const aligned = align ? ` align="${align}" style="text-align: ${align}"` : '';
76
+ const html = inlinesToHtml(cell.children, { rawHtml: false, safeLinks: true });
77
+ return `<${tag}${aligned}>${html}</${tag}>`;
78
+ })
79
+ .join('') +
80
+ '</tr>';
81
+ return (`<table><thead>${row(head, 'th')}</thead>` +
82
+ (body.length > 0 ? `<tbody>${body.map((r) => row(r, 'td')).join('')}</tbody>` : '') +
83
+ '</table>');
84
+ }
85
+ // What takes no column in a terminal: marks that join the letter before them, formatting
86
+ // characters (zero-width spaces and joiners) and control characters
87
+ const reZeroWidth = /^[\p{Mn}\p{Me}\p{Cf}\p{Cc}]+$/u;
88
+ // What takes two: a picture shown as an emoji, by default or because it asks to be (U+FE0F),
89
+ // and a flag, which is two regional letters
90
+ const reEmoji = /\p{Emoji_Presentation}|\p{Extended_Pictographic}️|\p{Regional_Indicator}/u;
91
+ /** East Asian wide and full-width characters: CJK, kana, Hangul and their punctuation. */
92
+ function wide(code) {
93
+ return ((code >= 0x1100 && code <= 0x115f) ||
94
+ (code >= 0x2e80 && code <= 0x303e) ||
95
+ (code >= 0x3041 && code <= 0x33ff) ||
96
+ (code >= 0x3400 && code <= 0x4dbf) ||
97
+ (code >= 0x4e00 && code <= 0x9fff) ||
98
+ (code >= 0xa000 && code <= 0xa4cf) ||
99
+ (code >= 0xa960 && code <= 0xa97f) ||
100
+ (code >= 0xac00 && code <= 0xd7a3) ||
101
+ (code >= 0xf900 && code <= 0xfaff) ||
102
+ (code >= 0xfe10 && code <= 0xfe19) ||
103
+ (code >= 0xfe30 && code <= 0xfe6f) ||
104
+ (code >= 0xff00 && code <= 0xff60) ||
105
+ (code >= 0xffe0 && code <= 0xffe6) ||
106
+ (code >= 0x16fe0 && code <= 0x16fe4) ||
107
+ (code >= 0x17000 && code <= 0x18cff) ||
108
+ (code >= 0x1b000 && code <= 0x1b2ff) ||
109
+ (code >= 0x20000 && code <= 0x3fffd));
110
+ }
111
+ let segmenter;
112
+ /** The characters as a reader sees them: an emoji made of five code points is one. */
113
+ function graphemes(text) {
114
+ if (segmenter === undefined)
115
+ segmenter =
116
+ typeof Intl !== 'undefined' && 'Segmenter' in Intl
117
+ ? new Intl.Segmenter(undefined, { granularity: 'grapheme' })
118
+ : null;
119
+ if (!segmenter)
120
+ return Array.from(text);
121
+ return Array.from(segmenter.segment(text), (part) => part.segment);
122
+ }
123
+ /**
124
+ * How many columns text takes in a monospace font, as a terminal draws it: two for a CJK
125
+ * character or an emoji, none for a combining mark or a zero-width space, one for the rest.
126
+ */
127
+ export function displayWidth(text) {
128
+ // Most cells are plain ASCII: counted without looking at each character
129
+ if (/^[\x20-\x7e]*$/.test(text))
130
+ return text.length;
131
+ let width = 0;
132
+ for (const character of graphemes(text)) {
133
+ if (reZeroWidth.test(character))
134
+ continue;
135
+ width += reEmoji.test(character) || wide(character.codePointAt(0)) ? 2 : 1;
136
+ }
137
+ return width;
138
+ }
139
+ /**
140
+ * The table as text whose columns line up in a monospace font: for a terminal, a commit
141
+ * message or a chat that takes no formatting. Columns are two spaces apart, the head is
142
+ * underlined with dashes, and each column is aligned as the table says, counting wide
143
+ * characters as two columns. Lines carry no spaces at their end.
144
+ */
145
+ export function tableToText(table) {
146
+ const rows = table.children.map((r) => r.children.map((c) => {
147
+ const text = inlineText(c.children).replace(/\s+/g, ' ').trim();
148
+ return { text, width: displayWidth(text) };
149
+ }));
150
+ const count = Math.max(table.align.length, ...rows.map((r) => r.length));
151
+ const widths = Array.from({ length: count }, (_, i) => Math.max(1, ...rows.map((r) => r[i]?.width ?? 0)));
152
+ const line = (cells) => widths
153
+ .map((width, i) => {
154
+ const cell = cells[i] ?? { text: '', width: 0 };
155
+ const room = width - cell.width;
156
+ const align = table.align[i];
157
+ if (align === 'right')
158
+ return ' '.repeat(room) + cell.text;
159
+ if (align === 'center') {
160
+ const before = Math.floor(room / 2);
161
+ return ' '.repeat(before) + cell.text + ' '.repeat(room - before);
162
+ }
163
+ return cell.text + ' '.repeat(room);
164
+ })
165
+ .join(' ')
166
+ .trimEnd();
167
+ const [head, ...body] = rows;
168
+ if (!head)
169
+ return '';
170
+ return [line(head), widths.map((w) => '-'.repeat(w)).join(' '), ...body.map(line)].join('\n');
171
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@diegopluna/narvi",
3
- "version": "0.5.9",
3
+ "version": "0.5.10",
4
4
  "packageManager": "pnpm@12.18.0",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -12,7 +12,7 @@ metadata:
12
12
  Grid, and Narvi's conventions for colour, labels and copy.
13
13
  type: core
14
14
  library: '@diegopluna/narvi'
15
- library_version: '0.5.9'
15
+ library_version: '0.5.10'
16
16
  requires:
17
17
  - setup
18
18
  sources:
@@ -12,7 +12,7 @@ metadata:
12
12
  patching the DOM, and decide which links and images text from a model may load.
13
13
  type: core
14
14
  library: '@diegopluna/narvi'
15
- library_version: '0.5.9'
15
+ library_version: '0.5.10'
16
16
  requires:
17
17
  - setup
18
18
  sources:
@@ -74,6 +74,18 @@ código' }}`. The type is `MarkdownLabels`, exported from `@diegopluna/narvi`.
74
74
  fades in whole. Both are off by default and do nothing once `streaming` is false.
75
75
  - `lineNumbers` numbers the lines of code blocks that have a language, and `download` gives
76
76
  code blocks a button that saves the code, named by the fence's file name or its language.
77
+ - `previews` gives a finished code block tagged `markdown`, `md` or `html` an eye toggle
78
+ (named "Show rendered", with `aria-pressed`) that shows it rendered: `previews` for both, or
79
+ `previews={['html']}` for one kind. It is off by default, the toggle appears only once the
80
+ fence has closed, blocks start as source, and Copy always copies the source. Markdown is
81
+ drawn by `Markdown` itself with the same options and plugins, minus streaming, tickable tasks
82
+ and nested previews. HTML goes in an `<iframe srcdoc>` sandboxed with only
83
+ `allow-same-origin`: no script runs, nothing loads from the network (a CSP allows only
84
+ `data:` and `blob:` pictures and fonts), and no link in it navigates the frame or the page.
85
+ In Chromium and Firefox a link goes through the link policy (`links.open`, otherwise a new
86
+ tab); Safari runs no listener in a frame that runs no script, so there a link does nothing. The frame fits
87
+ its content up to 32rem, then scrolls. Turn it on for replies where seeing a README or a
88
+ snippet of markup rendered helps; leave it off where every block should read as code.
77
89
  - `static` says whether finished blocks are written as markup, not built from parts: about
78
90
  twice as fast to show, in a fifth of the memory. Leave it out. Narvi then writes markup for
79
91
  text that was whole when the component was made and has not changed since (a reply loaded
@@ -248,10 +260,17 @@ export const plugins = [
248
260
 
249
261
  `url.startsWith('/attachments/')` alone would let `/attachments/../api/logout` through.
250
262
 
251
- Tables need no plugin: each has a button, shown on hover or focus, that copies it as Markdown
252
- or CSV or saves a CSV file. In the CSV, a cell a spreadsheet would run as a formula (one that
253
- starts with `=`, `+`, `-` or `@`) is written as text. `tableToCsv` and `tableToMarkdown` from
254
- `@diegopluna/narvi/markdown` do the same for a `table` node.
263
+ Tables need no plugin: each has a button, shown on hover or focus, whose menu copies it as
264
+ Markdown, as text, as HTML, as CSV or as a PNG, or saves a CSV file. "Copy as text" pads the
265
+ columns to line up in a monospace font (two spaces apart, the head underlined with dashes,
266
+ each column aligned as written, CJK and emoji counted as two columns) for terminals and commit
267
+ messages. "Copy as HTML" writes a plain `<table>` as both `text/html` and `text/plain`. "Copy
268
+ as PNG" draws the table as it is on screen, in the current theme, with columns scrolled out of
269
+ view, at the screen's density up to 3x; it is left out where the clipboard can't take images.
270
+ In the CSV, a cell a spreadsheet would run as a formula (one that starts with `=`, `+`, `-` or
271
+ `@`) is written as text. `tableToCsv`, `tableToMarkdown`, `tableToText` and `tableToHtml` from
272
+ `@diegopluna/narvi/markdown` do the same for a `table` node, and `displayWidth(text)` gives the
273
+ monospace columns a string takes.
255
274
 
256
275
  When a plugin needs the component's state, or should load late, build the array in the
257
276
  component's script. A new array of the same plugin objects changes nothing, but a new plugin
@@ -11,7 +11,7 @@ metadata:
11
11
  4 when the app uses it.
12
12
  type: lifecycle
13
13
  library: '@diegopluna/narvi'
14
- library_version: '0.5.9'
14
+ library_version: '0.5.10'
15
15
  sources:
16
16
  - src/lib/vite.ts
17
17
  - src/lib/base.css
@@ -12,7 +12,7 @@ metadata:
12
12
  hover, motion and forced-colour preferences.
13
13
  type: core
14
14
  library: '@diegopluna/narvi'
15
- library_version: '0.5.9'
15
+ library_version: '0.5.10'
16
16
  requires:
17
17
  - setup
18
18
  sources:
@@ -12,7 +12,7 @@ metadata:
12
12
  menus and dialogs, and resolve token colours for drawing code that cannot read CSS variables.
13
13
  type: core
14
14
  library: '@diegopluna/narvi'
15
- library_version: '0.5.9'
15
+ library_version: '0.5.10'
16
16
  requires:
17
17
  - setup
18
18
  sources: