@diegopluna/narvi 0.5.8 → 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.
- package/dist/components/dialog/ConfirmDialog.svelte +1 -2
- package/dist/components/dialog/DialogDescription.svelte +2 -2
- package/dist/components/dialog/DialogTitle.svelte +2 -2
- package/dist/components/input/Input.svelte +156 -15
- package/dist/components/input/Input.svelte.d.ts +17 -3
- package/dist/components/markdown/Markdown.svelte +68 -7
- package/dist/components/markdown/Markdown.svelte.d.ts +9 -0
- package/dist/components/markdown/MarkdownBlocks.svelte +4 -3
- package/dist/components/markdown/MarkdownCode.svelte +108 -9
- package/dist/components/markdown/MarkdownCode.svelte.d.ts +5 -1
- package/dist/components/markdown/MarkdownHtmlPreview.svelte +186 -0
- package/dist/components/markdown/MarkdownHtmlPreview.svelte.d.ts +11 -0
- package/dist/components/markdown/MarkdownPreview.svelte +17 -0
- package/dist/components/markdown/MarkdownPreview.svelte.d.ts +6 -0
- package/dist/components/markdown/MarkdownTable.svelte +61 -4
- package/dist/components/markdown/context.d.ts +21 -0
- package/dist/components/markdown/labels.d.ts +12 -0
- package/dist/components/markdown/labels.js +5 -0
- package/dist/components/markdown/preview.d.ts +9 -0
- package/dist/components/markdown/preview.js +14 -0
- package/dist/components/markdown/static.d.ts +2 -0
- package/dist/components/markdown/static.js +5 -1
- package/dist/components/markdown/table-image.d.ts +15 -0
- package/dist/components/markdown/table-image.js +342 -0
- package/dist/components/message/MessageAction.svelte +17 -2
- package/dist/components/message/MessageAction.svelte.d.ts +7 -0
- package/dist/internal/Glyph.svelte +11 -0
- package/dist/internal/Glyph.svelte.d.ts +2 -0
- package/dist/internal/closing.svelte.d.ts +0 -6
- package/dist/internal/closing.svelte.js +0 -33
- package/dist/internal/markers.stylex.d.ts +4 -0
- package/dist/internal/markers.stylex.js +2 -0
- package/dist/markdown/highlight.d.ts +2 -2
- package/dist/markdown/highlight.js +2 -2
- package/dist/markdown/html.d.ts +3 -1
- package/dist/markdown/html.js +6 -0
- package/dist/markdown/index.d.ts +2 -2
- package/dist/markdown/index.js +2 -2
- package/dist/markdown/table.d.ts +20 -1
- package/dist/markdown/table.js +111 -0
- package/package.json +19 -19
- package/skills/building-ui/SKILL.md +5 -5
- package/skills/building-ui/references/components.md +26 -26
- package/skills/markdown/SKILL.md +26 -7
- package/skills/setup/SKILL.md +1 -1
- package/skills/styling/SKILL.md +1 -1
- package/skills/theming/SKILL.md +1 -1
|
@@ -8,6 +8,10 @@
|
|
|
8
8
|
would cost more than the rest of the reply together. The markup is Narvi's own: a
|
|
9
9
|
`<span>` with one of its own classes, around text that has been escaped (`tokensHtml`).
|
|
10
10
|
Nothing of the code's text is ever read as markup.
|
|
11
|
+
|
|
12
|
+
With `previews`, a finished Markdown or HTML block can be shown rendered: Markdown by Narvi's
|
|
13
|
+
own Markdown, HTML in a sandboxed frame that runs no script. What draws either is loaded the
|
|
14
|
+
first time a block is shown so.
|
|
11
15
|
-->
|
|
12
16
|
<script lang="ts" module>
|
|
13
17
|
import * as stylex from '@stylexjs/stylex';
|
|
@@ -19,7 +23,9 @@
|
|
|
19
23
|
import { attrs, classes } from '../../internal/sx.js';
|
|
20
24
|
import { escapeHtml } from '../../markdown/html.js';
|
|
21
25
|
import { colors, font, radius } from '../../tokens.stylex.js';
|
|
22
|
-
import { actionHtml } from '../message/MessageAction.svelte';
|
|
26
|
+
import { actionClass, actionHtml } from '../message/MessageAction.svelte';
|
|
27
|
+
import type { Component } from 'svelte';
|
|
28
|
+
import type { PreviewKind } from './preview.js';
|
|
23
29
|
import { copyHtml } from '../message/MessageCopy.svelte';
|
|
24
30
|
import { getMarkdownContext } from './context.js';
|
|
25
31
|
import { showHidden } from './hidden.js';
|
|
@@ -128,9 +134,41 @@
|
|
|
128
134
|
},
|
|
129
135
|
barePre: { paddingBlockStart: '0.75rem', paddingInlineEnd: '2.5rem' },
|
|
130
136
|
actions: { display: 'inline-flex', flexShrink: 0, gap: '0.125rem' },
|
|
131
|
-
copy: { flexShrink: 0 }
|
|
137
|
+
copy: { flexShrink: 0 },
|
|
138
|
+
// Shown rendered, the block is a page set into the reply: an edge and no fill, so the
|
|
139
|
+
// code blocks in it keep their own well
|
|
140
|
+
rendered: {
|
|
141
|
+
backgroundColor: 'transparent',
|
|
142
|
+
boxShadow: `inset 0 0 0 1px ${colors.border}`
|
|
143
|
+
},
|
|
144
|
+
preview: { minWidth: 0, paddingInline: '0.75rem', paddingBlockEnd: '0.75rem' }
|
|
132
145
|
});
|
|
133
146
|
|
|
147
|
+
/** What draws each kind of block rendered, once it has loaded: one for the whole page. */
|
|
148
|
+
const previewParts: Partial<Record<PreviewKind, Component<{ source: string }>>> = {};
|
|
149
|
+
const loadingParts: Partial<Record<PreviewKind, Promise<Component<{ source: string }>>>> = {};
|
|
150
|
+
|
|
151
|
+
function loadPreview(kind: PreviewKind): Promise<Component<{ source: string }>> {
|
|
152
|
+
loadingParts[kind] ??= (
|
|
153
|
+
kind === 'markdown'
|
|
154
|
+
? import('./MarkdownPreview.svelte')
|
|
155
|
+
: import('./MarkdownHtmlPreview.svelte')
|
|
156
|
+
).then(
|
|
157
|
+
(module) => (previewParts[kind] = module.default),
|
|
158
|
+
(cause: unknown) => {
|
|
159
|
+
// Asked again the next time, after it failed to load
|
|
160
|
+
delete loadingParts[kind];
|
|
161
|
+
throw cause;
|
|
162
|
+
}
|
|
163
|
+
);
|
|
164
|
+
return loadingParts[kind];
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** The name of a block's preview toggle: the same whether it is pressed or not. */
|
|
168
|
+
function previewLabel(title: string | null, labels: { showRendered: string }): string {
|
|
169
|
+
return title ? `${labels.showRendered}: ${title}` : labels.showRendered;
|
|
170
|
+
}
|
|
171
|
+
|
|
134
172
|
/** Token kinds a highlighter names, and the colours Narvi gives them. */
|
|
135
173
|
const kinds = stylex.create({
|
|
136
174
|
keyword: { color: colors.codeLiteral },
|
|
@@ -321,11 +359,22 @@
|
|
|
321
359
|
*/
|
|
322
360
|
function actionsHtml(
|
|
323
361
|
title: string | null,
|
|
324
|
-
labels: { copyCode: string; downloadCode: string },
|
|
325
|
-
download: boolean
|
|
362
|
+
labels: { copyCode: string; downloadCode: string; showRendered: string },
|
|
363
|
+
download: boolean,
|
|
364
|
+
preview = false
|
|
326
365
|
): string {
|
|
327
366
|
const label = (words: string) => escapeHtml(title ? `${words}: ${title}` : words);
|
|
328
367
|
return (
|
|
368
|
+
// A block that can be shown rendered starts as its source. Pressed, the document draws
|
|
369
|
+
// it by its component, which holds which of the two it shows
|
|
370
|
+
(preview
|
|
371
|
+
? actionHtml({
|
|
372
|
+
label: escapeHtml(previewLabel(title, labels)),
|
|
373
|
+
attributes: ' aria-pressed="false" data-markdown-action="preview"',
|
|
374
|
+
bare: true,
|
|
375
|
+
inner: glyphHtml('eye')
|
|
376
|
+
})
|
|
377
|
+
: '') +
|
|
329
378
|
(download
|
|
330
379
|
? actionHtml({
|
|
331
380
|
label: label(labels.downloadCode),
|
|
@@ -350,10 +399,12 @@
|
|
|
350
399
|
export function codeHtml(
|
|
351
400
|
node: CodeBlock,
|
|
352
401
|
options: {
|
|
353
|
-
labels: { copyCode: string; downloadCode: string };
|
|
402
|
+
labels: { copyCode: string; downloadCode: string; showRendered: string };
|
|
354
403
|
highlight: Highlighter | null;
|
|
355
404
|
lineNumbers: boolean;
|
|
356
405
|
download: boolean;
|
|
406
|
+
/** Whether the block has a toggle that shows it rendered. */
|
|
407
|
+
preview?: boolean | undefined;
|
|
357
408
|
}
|
|
358
409
|
): string {
|
|
359
410
|
const lang = node.lang;
|
|
@@ -393,7 +444,7 @@
|
|
|
393
444
|
(named || escapeHtml(lang ?? '')) +
|
|
394
445
|
'</span>' +
|
|
395
446
|
`<span class="${classes(styles.actions)}">` +
|
|
396
|
-
actionsHtml(title, options.labels, options.download) +
|
|
447
|
+
actionsHtml(title, options.labels, options.download, options.preview) +
|
|
397
448
|
'</span></div>' +
|
|
398
449
|
`<pre dir="ltr" data-markdown-scroll class="${classes(styles.pre, lang === null && styles.laidOut, lang === null && styles.wrap, bare && styles.barePre, numbered && styles.numbered)}"${lang === null ? '' : ` style="--narvi-code-lines: ${countLines(code)};"`}>` +
|
|
399
450
|
gutter +
|
|
@@ -418,6 +469,31 @@
|
|
|
418
469
|
const title = $derived(named ? codeTitle(node) : null);
|
|
419
470
|
const bare = $derived(title === null && lang === null);
|
|
420
471
|
|
|
472
|
+
// A Markdown or HTML block can be shown rendered once its fence has closed. It starts as its
|
|
473
|
+
// source; which it shows is kept by the document, so the static path's button can say it
|
|
474
|
+
const kind = $derived(live ? null : md.preview(node.lang));
|
|
475
|
+
const rendered = $derived(kind !== null && md.previewed(node.id));
|
|
476
|
+
let Preview = $state<Component<{ source: string }> | null>(null);
|
|
477
|
+
$effect(() => {
|
|
478
|
+
if (!rendered || !kind) return;
|
|
479
|
+
const part = previewParts[kind];
|
|
480
|
+
if (part) {
|
|
481
|
+
Preview = part;
|
|
482
|
+
return;
|
|
483
|
+
}
|
|
484
|
+
let gone = false;
|
|
485
|
+
loadPreview(kind).then(
|
|
486
|
+
(loaded) => {
|
|
487
|
+
if (!gone) Preview = loaded;
|
|
488
|
+
},
|
|
489
|
+
// What could not load leaves the source showing
|
|
490
|
+
() => {}
|
|
491
|
+
);
|
|
492
|
+
return () => {
|
|
493
|
+
gone = true;
|
|
494
|
+
};
|
|
495
|
+
});
|
|
496
|
+
|
|
421
497
|
// A block that streams is highlighted as it arrives: the stream keeps its place, so only
|
|
422
498
|
// new lines are tokenized, and the lines already drawn are never drawn again
|
|
423
499
|
let session: {
|
|
@@ -557,7 +633,13 @@
|
|
|
557
633
|
data-markdown-code
|
|
558
634
|
data-markdown-block={node.id}
|
|
559
635
|
data-open={live || undefined}
|
|
560
|
-
{...attrs([
|
|
636
|
+
{...attrs([
|
|
637
|
+
prose.block,
|
|
638
|
+
styles.frame,
|
|
639
|
+
bare && styles.bare,
|
|
640
|
+
rendered && Preview && styles.rendered,
|
|
641
|
+
enter && prose.enter
|
|
642
|
+
])}
|
|
561
643
|
>
|
|
562
644
|
<!-- Always there, so the code does not move down when the fence closes and the button arrives -->
|
|
563
645
|
<div data-markdown-chrome {...attrs([styles.header, bare && styles.bareHeader])}>
|
|
@@ -570,13 +652,30 @@
|
|
|
570
652
|
<!-- Markup, pressed through the document's root: see `actionsHtml` -->
|
|
571
653
|
<!-- eslint-disable svelte/no-at-html-tags -->
|
|
572
654
|
<span {...attrs([styles.actions])}
|
|
573
|
-
>{
|
|
655
|
+
>{#if kind}<!-- An element of its own, which stays as it is pressed, so focus stays on it --><button
|
|
656
|
+
type="button"
|
|
657
|
+
aria-label={previewLabel(title, md.labels)}
|
|
658
|
+
aria-pressed={rendered}
|
|
659
|
+
data-slot="message-action"
|
|
660
|
+
data-markdown-action="preview"
|
|
661
|
+
class={actionClass({ pressed: rendered })}>{@html glyphHtml('eye')}</button
|
|
662
|
+
>{/if}{@html actionsHtml(title, md.labels, md.code().download)}</span
|
|
574
663
|
>
|
|
575
664
|
<!-- eslint-enable svelte/no-at-html-tags -->
|
|
576
665
|
{/if}
|
|
577
666
|
</div>
|
|
667
|
+
{#if rendered && Preview}
|
|
668
|
+
<div data-markdown-preview={kind} {...attrs([styles.preview])}>
|
|
669
|
+
<Preview source={code} />
|
|
670
|
+
</div>
|
|
671
|
+
{:else}
|
|
672
|
+
{@render source()}
|
|
673
|
+
{/if}
|
|
674
|
+
</div>
|
|
675
|
+
|
|
676
|
+
{#snippet source()}
|
|
578
677
|
<!-- Narvi's own markup around escaped text: see `tokensHtml` -->
|
|
579
678
|
<!-- eslint-disable svelte/no-at-html-tags -->
|
|
580
679
|
<!-- prettier-ignore -->
|
|
581
680
|
<pre dir="ltr" use:scrollable style:--narvi-code-lines={lineCount} {...attrs([styles.pre, (live || node.lang === null) && styles.laidOut, node.lang === null && styles.wrap, bare && styles.barePre, numbers !== null && styles.numbered])}>{#if numbers}<span aria-hidden="true" data-markdown-chrome {...attrs([styles.numbers])}>{#each numbers as group, g (g)}<span {...attrs([styles.lines])}>{group}</span>{/each}</span>{/if}<code {...attrs([styles.code, numbers !== null && styles.numberedCode])}>{#if view.kind === 'text'}{view.text}{:else if view.kind === 'whole'}{@html view.html}{:else}{#each view.groups as group, g (g)}<span data-markdown-lines {...attrs([styles.lines])}>{#each group.lines as line, i (i)}{@html i > 0 ? NEWLINE + line : line}{/each}{#if g === view.groups.length - 1}{@html view.tail}{:else}{NEWLINE}{/if}</span>{/each}{/if}</code></pre>
|
|
582
|
-
|
|
681
|
+
{/snippet}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { CodeBlock } from '../../markdown/ast.js';
|
|
2
2
|
import type { Highlighter } from '../../markdown/plugin.js';
|
|
3
|
+
import type { Component } from 'svelte';
|
|
3
4
|
export type MarkdownCodeProps = {
|
|
4
5
|
node: CodeBlock;
|
|
5
6
|
enter?: boolean | undefined;
|
|
@@ -13,11 +14,14 @@ export declare function codeHtml(node: CodeBlock, options: {
|
|
|
13
14
|
labels: {
|
|
14
15
|
copyCode: string;
|
|
15
16
|
downloadCode: string;
|
|
17
|
+
showRendered: string;
|
|
16
18
|
};
|
|
17
19
|
highlight: Highlighter | null;
|
|
18
20
|
lineNumbers: boolean;
|
|
19
21
|
download: boolean;
|
|
22
|
+
/** Whether the block has a toggle that shows it rendered. */
|
|
23
|
+
preview?: boolean | undefined;
|
|
20
24
|
}): string;
|
|
21
|
-
declare const MarkdownCode:
|
|
25
|
+
declare const MarkdownCode: Component<MarkdownCodeProps, {}, "">;
|
|
22
26
|
type MarkdownCode = ReturnType<typeof MarkdownCode>;
|
|
23
27
|
export default MarkdownCode;
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
An HTML code block, shown rendered, in a frame of its own. Loaded the first time a block is
|
|
3
|
+
shown rendered.
|
|
4
|
+
|
|
5
|
+
The frame is sandboxed with `allow-same-origin` and nothing else: no script in it runs, it
|
|
6
|
+
cannot submit a form, open a window or navigate the page, and this page can still reach into
|
|
7
|
+
it to measure it and to take its link clicks. What goes into it is read first by the
|
|
8
|
+
browser's inert parser, which runs nothing, and loses what could navigate it or reach the
|
|
9
|
+
network on its own: a `<meta http-equiv>` (a refresh) and a `<base>`. A content security
|
|
10
|
+
policy then lets it load nothing from the network, so a preview cannot tell a server it was
|
|
11
|
+
seen: pictures and fonts written into the HTML itself (`data:`) still show.
|
|
12
|
+
|
|
13
|
+
Every link in it is made to open a new window, which the sandbox refuses, so the browser
|
|
14
|
+
itself follows none of them, in the frame or out of it. Where this page's listeners run in
|
|
15
|
+
the frame (Chromium and Firefox), a link the document's link policy allows is offered to the
|
|
16
|
+
app's `open`, and otherwise opens in a new tab, as a link in the reply would. WebKit runs no
|
|
17
|
+
listener in a frame that runs no script, so in Safari a link in a preview does nothing.
|
|
18
|
+
|
|
19
|
+
The frame is as tall as what is in it, up to a limit, past which it scrolls. It takes the
|
|
20
|
+
reply's colour scheme, text colour and face, which the HTML's own styles override.
|
|
21
|
+
-->
|
|
22
|
+
<script lang="ts" module>
|
|
23
|
+
import * as stylex from '@stylexjs/stylex';
|
|
24
|
+
import { attrs } from '../../internal/sx.js';
|
|
25
|
+
import type { Link } from '../../markdown/ast.js';
|
|
26
|
+
import { colors } from '../../tokens.stylex.js';
|
|
27
|
+
import { getMarkdownContext } from './context.js';
|
|
28
|
+
|
|
29
|
+
/** What the frame may load: nothing from the network, nothing that runs. */
|
|
30
|
+
const POLICY =
|
|
31
|
+
"default-src 'none'; style-src 'unsafe-inline'; img-src data: blob:; font-src data:; " +
|
|
32
|
+
"media-src data: blob:; form-action 'none'; base-uri 'none'";
|
|
33
|
+
|
|
34
|
+
const BASE_ID = 'narvi-preview-base';
|
|
35
|
+
|
|
36
|
+
const styles = stylex.create({
|
|
37
|
+
frame: {
|
|
38
|
+
display: 'block',
|
|
39
|
+
width: '100%',
|
|
40
|
+
// Until it has been measured, about the height of a few lines
|
|
41
|
+
height: '6rem',
|
|
42
|
+
maxHeight: 'min(32rem, 70vh)',
|
|
43
|
+
borderWidth: 0,
|
|
44
|
+
borderRadius: 0,
|
|
45
|
+
backgroundColor: 'transparent',
|
|
46
|
+
// The theme's text colour, read into the frame. A change of theme changes it, and the
|
|
47
|
+
// change is heard as the end of this transition, which is not seen: it moves nothing
|
|
48
|
+
color: colors.text,
|
|
49
|
+
transitionProperty: 'color',
|
|
50
|
+
transitionDuration: '1ms'
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
/** The look the frame starts from: the reply's, under anything the HTML says. */
|
|
55
|
+
function baseCss(frame: HTMLIFrameElement): string {
|
|
56
|
+
const style = getComputedStyle(frame);
|
|
57
|
+
const scheme = /dark/.test(style.colorScheme)
|
|
58
|
+
? /light/.test(style.colorScheme)
|
|
59
|
+
? // Both: the one the reply is drawn in now, which its text colour gives away
|
|
60
|
+
brightness(style.color) > 0.5
|
|
61
|
+
? 'dark'
|
|
62
|
+
: 'light'
|
|
63
|
+
: 'dark'
|
|
64
|
+
: 'light';
|
|
65
|
+
const text = style.color.replace(/[<>;{}]/g, '');
|
|
66
|
+
const face = style.fontFamily.replace(/[<>;{}]/g, '');
|
|
67
|
+
return (
|
|
68
|
+
`:root{color-scheme:${scheme};color:${text};font-family:${face};line-height:1.5}` +
|
|
69
|
+
':where(body){margin:0;overflow-wrap:break-word}' +
|
|
70
|
+
':where(img,video,svg){max-width:100%;height:auto}'
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** How light a computed colour is, 0 to 1, by its sRGB channels. */
|
|
75
|
+
function brightness(color: string): number {
|
|
76
|
+
const channels = /rgba?\(\s*([\d.]+)[\s,]+([\d.]+)[\s,]+([\d.]+)/.exec(color);
|
|
77
|
+
if (!channels) return 0;
|
|
78
|
+
const [r, g, b] = channels.slice(1).map(Number) as [number, number, number];
|
|
79
|
+
return (0.2126 * r + 0.7152 * g + 0.0722 * b) / 255;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* The HTML as the frame's document: read by the inert parser, without what could navigate
|
|
84
|
+
* the frame or point its addresses elsewhere, with the policy first in its head.
|
|
85
|
+
*/
|
|
86
|
+
export function previewDocument(source: string, css: string): string {
|
|
87
|
+
const doc = new DOMParser().parseFromString(source, 'text/html');
|
|
88
|
+
for (const element of doc.querySelectorAll('meta[http-equiv], base, script')) element.remove();
|
|
89
|
+
// A new window, which the sandbox refuses: a link that is followed goes nowhere
|
|
90
|
+
for (const link of doc.querySelectorAll('a, area')) link.setAttribute('target', '_blank');
|
|
91
|
+
const policy = doc.createElement('meta');
|
|
92
|
+
policy.httpEquiv = 'Content-Security-Policy';
|
|
93
|
+
policy.content = POLICY;
|
|
94
|
+
const charset = doc.createElement('meta');
|
|
95
|
+
charset.setAttribute('charset', 'utf-8');
|
|
96
|
+
const base = doc.createElement('style');
|
|
97
|
+
base.id = BASE_ID;
|
|
98
|
+
base.textContent = css;
|
|
99
|
+
doc.head.prepend(charset, policy, base);
|
|
100
|
+
return `<!doctype html>${doc.documentElement.outerHTML}`;
|
|
101
|
+
}
|
|
102
|
+
</script>
|
|
103
|
+
|
|
104
|
+
<script lang="ts">
|
|
105
|
+
let { source }: { source: string } = $props();
|
|
106
|
+
|
|
107
|
+
const md = getMarkdownContext();
|
|
108
|
+
let frame = $state<HTMLIFrameElement | null>(null);
|
|
109
|
+
let srcdoc = $state<string | undefined>(undefined);
|
|
110
|
+
let height = $state<number | undefined>(undefined);
|
|
111
|
+
|
|
112
|
+
// Written once the frame is on the page, whose look it takes
|
|
113
|
+
$effect(() => {
|
|
114
|
+
if (frame) srcdoc = previewDocument(source, baseCss(frame));
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
/** A link in the frame, pressed: never followed there, offered to the app or a new tab. */
|
|
118
|
+
function follow(event: MouseEvent) {
|
|
119
|
+
const target = event.target as Element | null;
|
|
120
|
+
const link = target?.closest?.('a[href], area[href]');
|
|
121
|
+
if (!link) return;
|
|
122
|
+
const raw = link.getAttribute('href') ?? '';
|
|
123
|
+
// A place in the frame's own page is the frame's own business
|
|
124
|
+
if (raw.startsWith('#')) return;
|
|
125
|
+
event.preventDefault();
|
|
126
|
+
const node: Link = { type: 'link', url: raw, title: null, kind: 'inline', children: [] };
|
|
127
|
+
const href = md.links.href(raw, node);
|
|
128
|
+
if (href === null || href.startsWith('#')) return;
|
|
129
|
+
md.links.open?.(href, event);
|
|
130
|
+
if (event.defaultPrevented && md.links.open) return;
|
|
131
|
+
if (event.type === 'click' && md.links.external(href))
|
|
132
|
+
window.open(href, '_blank', 'noopener,noreferrer');
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function measure(doc: Document) {
|
|
136
|
+
// The document's own height, not the frame's: the root is as tall as what is in it
|
|
137
|
+
const content = Math.ceil(doc.documentElement.getBoundingClientRect().height);
|
|
138
|
+
if (content > 0) height = content;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
let watching: (() => void) | null = null;
|
|
142
|
+
|
|
143
|
+
function loaded() {
|
|
144
|
+
watching?.();
|
|
145
|
+
watching = null;
|
|
146
|
+
const doc = frame?.contentDocument;
|
|
147
|
+
if (!doc?.documentElement) return;
|
|
148
|
+
// Listened to from here: these run although nothing in the frame may
|
|
149
|
+
const capture = { capture: true };
|
|
150
|
+
const submit = (event: Event) => event.preventDefault();
|
|
151
|
+
doc.addEventListener('click', follow, capture);
|
|
152
|
+
doc.addEventListener('auxclick', follow, capture);
|
|
153
|
+
doc.addEventListener('submit', submit, capture);
|
|
154
|
+
const observer = new ResizeObserver(() => measure(doc));
|
|
155
|
+
observer.observe(doc.documentElement);
|
|
156
|
+
if (doc.body) observer.observe(doc.body);
|
|
157
|
+
measure(doc);
|
|
158
|
+
watching = () => {
|
|
159
|
+
observer.disconnect();
|
|
160
|
+
doc.removeEventListener('click', follow, capture);
|
|
161
|
+
doc.removeEventListener('auxclick', follow, capture);
|
|
162
|
+
doc.removeEventListener('submit', submit, capture);
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** The theme changed: the frame's base look follows, in place, without loading it again. */
|
|
167
|
+
function restyle() {
|
|
168
|
+
const base = frame?.contentDocument?.getElementById(BASE_ID);
|
|
169
|
+
if (frame && base) base.textContent = baseCss(frame);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
$effect(() => () => watching?.());
|
|
173
|
+
</script>
|
|
174
|
+
|
|
175
|
+
<iframe
|
|
176
|
+
bind:this={frame}
|
|
177
|
+
title={md.labels.htmlPreview}
|
|
178
|
+
sandbox="allow-same-origin"
|
|
179
|
+
referrerpolicy="no-referrer"
|
|
180
|
+
{srcdoc}
|
|
181
|
+
onload={loaded}
|
|
182
|
+
ontransitionend={restyle}
|
|
183
|
+
data-markdown-html-preview
|
|
184
|
+
style:height={height === undefined ? undefined : `${height}px`}
|
|
185
|
+
{...attrs([styles.frame])}
|
|
186
|
+
></iframe>
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The HTML as the frame's document: read by the inert parser, without what could navigate
|
|
3
|
+
* the frame or point its addresses elsewhere, with the policy first in its head.
|
|
4
|
+
*/
|
|
5
|
+
export declare function previewDocument(source: string, css: string): string;
|
|
6
|
+
type $$ComponentProps = {
|
|
7
|
+
source: string;
|
|
8
|
+
};
|
|
9
|
+
declare const MarkdownHtmlPreview: import("svelte").Component<$$ComponentProps, {}, "">;
|
|
10
|
+
type MarkdownHtmlPreview = ReturnType<typeof MarkdownHtmlPreview>;
|
|
11
|
+
export default MarkdownHtmlPreview;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
A Markdown code block, shown rendered: by Narvi's own Markdown, with the options and plugins
|
|
3
|
+
of the document it is in. Nothing in it streams, its tasks cannot be ticked (they are not
|
|
4
|
+
the document's text), and a Markdown block inside it shows as its source, so a block can't
|
|
5
|
+
be shown rendered inside itself. Loaded the first time a block is shown rendered.
|
|
6
|
+
-->
|
|
7
|
+
<script lang="ts">
|
|
8
|
+
import { getMarkdownContext } from './context.js';
|
|
9
|
+
import Markdown from './Markdown.svelte';
|
|
10
|
+
|
|
11
|
+
let { source }: { source: string } = $props();
|
|
12
|
+
|
|
13
|
+
const md = getMarkdownContext();
|
|
14
|
+
const options = $derived(md.nested());
|
|
15
|
+
</script>
|
|
16
|
+
|
|
17
|
+
<Markdown value={source} {...options} previews={false} />
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
<!--
|
|
2
2
|
A table, in a frame that scrolls sideways when it is wide. A button at its corner, shown
|
|
3
|
-
on hover or focus, copies it as Markdown or
|
|
3
|
+
on hover or focus, copies it as Markdown, aligned text, HTML, CSV or a PNG, or saves it as
|
|
4
|
+
a CSV file.
|
|
4
5
|
-->
|
|
5
6
|
<script lang="ts" module>
|
|
6
7
|
import * as stylex from '@stylexjs/stylex';
|
|
@@ -11,7 +12,7 @@
|
|
|
11
12
|
import { escapeHtml } from '../../markdown/html.js';
|
|
12
13
|
import type { Table } from '../../markdown/ast.js';
|
|
13
14
|
import { groupInlineHtml } from '../../markdown/sanitize.js';
|
|
14
|
-
import { tableToCsv, tableToMarkdown } from '../../markdown/table.js';
|
|
15
|
+
import { tableToCsv, tableToHtml, tableToMarkdown, tableToText } from '../../markdown/table.js';
|
|
15
16
|
import { colors, motion, radius } from '../../tokens.stylex.js';
|
|
16
17
|
import { media } from '../../media.stylex.js';
|
|
17
18
|
import Button, { buttonClass, buttonHtml } from '../button/Button.svelte';
|
|
@@ -64,6 +65,21 @@
|
|
|
64
65
|
}
|
|
65
66
|
});
|
|
66
67
|
|
|
68
|
+
/**
|
|
69
|
+
* Whether this browser can put a picture on the clipboard: a `ClipboardItem` that takes a
|
|
70
|
+
* PNG. Asked when the menu is made, which is only ever in a browser.
|
|
71
|
+
*/
|
|
72
|
+
function canCopyImage(): boolean {
|
|
73
|
+
if (typeof ClipboardItem === 'undefined' || typeof navigator.clipboard?.write !== 'function')
|
|
74
|
+
return false;
|
|
75
|
+
const supports = (ClipboardItem as { supports?: (type: string) => boolean }).supports;
|
|
76
|
+
try {
|
|
77
|
+
return typeof supports === 'function' ? supports('image/png') : true;
|
|
78
|
+
} catch {
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
67
83
|
/** The button of a table whose menu has not been asked for: worked out once. */
|
|
68
84
|
const idleButtonClass = buttonClass({
|
|
69
85
|
variant: 'ghost',
|
|
@@ -158,9 +174,13 @@
|
|
|
158
174
|
let copied = $state(false);
|
|
159
175
|
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
160
176
|
|
|
161
|
-
|
|
177
|
+
let root = $state<HTMLDivElement | null>(null);
|
|
178
|
+
const imageCopies = $derived(armed && canCopyImage());
|
|
179
|
+
|
|
180
|
+
/** Puts something on the clipboard, then says so, or says it could not. */
|
|
181
|
+
async function put(write: () => Promise<void>) {
|
|
162
182
|
try {
|
|
163
|
-
await
|
|
183
|
+
await write();
|
|
164
184
|
} catch {
|
|
165
185
|
md.announce(md.labels.copyFailed);
|
|
166
186
|
return;
|
|
@@ -171,6 +191,35 @@
|
|
|
171
191
|
timer = setTimeout(() => (copied = false), 2000);
|
|
172
192
|
}
|
|
173
193
|
|
|
194
|
+
const copy = (text: string) => put(() => navigator.clipboard.writeText(text));
|
|
195
|
+
|
|
196
|
+
/** As HTML for a rich-text editor, and the same markup as text for one that takes no formatting. */
|
|
197
|
+
function copyHtml() {
|
|
198
|
+
const html = tableToHtml(node);
|
|
199
|
+
if (typeof ClipboardItem === 'undefined' || typeof navigator.clipboard.write !== 'function')
|
|
200
|
+
return copy(html);
|
|
201
|
+
return put(() =>
|
|
202
|
+
navigator.clipboard.write([
|
|
203
|
+
new ClipboardItem({
|
|
204
|
+
'text/html': new Blob([html], { type: 'text/html' }),
|
|
205
|
+
'text/plain': new Blob([html], { type: 'text/plain' })
|
|
206
|
+
})
|
|
207
|
+
])
|
|
208
|
+
);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* As a picture. The clipboard is written to now, inside the press, with the picture still
|
|
213
|
+
* to come: Safari takes a write only while the press is being handled, and drawing it, and
|
|
214
|
+
* loading what draws it, come after.
|
|
215
|
+
*/
|
|
216
|
+
function copyImage() {
|
|
217
|
+
const table = root?.querySelector('table');
|
|
218
|
+
if (!table) return;
|
|
219
|
+
const png = import('./table-image.js').then(({ tableImage }) => tableImage(table));
|
|
220
|
+
return put(() => navigator.clipboard.write([new ClipboardItem({ 'image/png': png })]));
|
|
221
|
+
}
|
|
222
|
+
|
|
174
223
|
function download() {
|
|
175
224
|
// The byte order mark tells a spreadsheet the file is UTF-8
|
|
176
225
|
saveFile('table.csv', '\uFEFF' + tableToCsv(node), 'text/csv');
|
|
@@ -180,6 +229,7 @@
|
|
|
180
229
|
</script>
|
|
181
230
|
|
|
182
231
|
<div
|
|
232
|
+
bind:this={root}
|
|
183
233
|
data-markdown-table
|
|
184
234
|
{...attrs([prose.block, styles.wrap, markdownTableMarker, enter && prose.enter])}
|
|
185
235
|
>
|
|
@@ -269,7 +319,14 @@
|
|
|
269
319
|
<Menu.Item onSelect={() => copy(tableToMarkdown(node))}
|
|
270
320
|
>{md.labels.copyAsMarkdown}</Menu.Item
|
|
271
321
|
>
|
|
322
|
+
<Menu.Item onSelect={() => copy(tableToText(node))}>{md.labels.copyAsText}</Menu.Item>
|
|
323
|
+
<Menu.Item onSelect={copyHtml}>{md.labels.copyAsHtml}</Menu.Item>
|
|
272
324
|
<Menu.Item onSelect={() => copy(tableToCsv(node))}>{md.labels.copyAsCsv}</Menu.Item>
|
|
325
|
+
<!-- Left out where the clipboard takes no pictures -->
|
|
326
|
+
{#if imageCopies}
|
|
327
|
+
<Menu.Item onSelect={copyImage}>{md.labels.copyAsPng}</Menu.Item>
|
|
328
|
+
{/if}
|
|
329
|
+
<Menu.Separator />
|
|
273
330
|
<Menu.Item onSelect={download}>{md.labels.saveAsCsv}</Menu.Item>
|
|
274
331
|
</Menu.Content>
|
|
275
332
|
</Menu.Root>
|
|
@@ -3,6 +3,7 @@ import type { Block, Document, FootnoteReference, Heading, ListItem } from '../.
|
|
|
3
3
|
import type { MarkdownLabels } from './labels.js';
|
|
4
4
|
import { type Highlighter, type ResolvedLinkPolicy, type MarkdownPlugin, type PluginComponent, type StaticPart } from '../../markdown/plugin.js';
|
|
5
5
|
import type { UrlPolicy } from '../../markdown/sanitize.js';
|
|
6
|
+
import type { PreviewKind } from './preview.js';
|
|
6
7
|
export type MarkdownContext = {
|
|
7
8
|
/** Components that replace Narvi's, by node type. */
|
|
8
9
|
component: (type: string) => PluginComponent | undefined;
|
|
@@ -65,6 +66,15 @@ export type MarkdownContext = {
|
|
|
65
66
|
lineNumbers: boolean;
|
|
66
67
|
download: boolean;
|
|
67
68
|
};
|
|
69
|
+
/** What a finished code block in a language can be shown rendered as, or null. */
|
|
70
|
+
preview: (lang: string | null) => PreviewKind | null;
|
|
71
|
+
/** Whether a code block is shown rendered now. */
|
|
72
|
+
previewed: (id: number) => boolean;
|
|
73
|
+
/**
|
|
74
|
+
* The document's options, for a Markdown block shown rendered: the same plugins and the
|
|
75
|
+
* same way of reading, with nothing that streams or that would show a block rendered again.
|
|
76
|
+
*/
|
|
77
|
+
nested: () => NestedOptions;
|
|
68
78
|
/**
|
|
69
79
|
* How much formula markup the document holds, which its formulas share a limit on, and
|
|
70
80
|
* what a formula in a line of text calls once it is on the page.
|
|
@@ -84,6 +94,17 @@ export type MarkdownContext = {
|
|
|
84
94
|
/** Scrolls to a heading by its anchor; false when the document has none by that name. */
|
|
85
95
|
goto: (name: string) => boolean;
|
|
86
96
|
};
|
|
97
|
+
export type NestedOptions = {
|
|
98
|
+
gfm: boolean;
|
|
99
|
+
breaks: boolean;
|
|
100
|
+
singleTilde: boolean;
|
|
101
|
+
frontmatter: boolean;
|
|
102
|
+
plugins: ReadonlyArray<MarkdownPlugin>;
|
|
103
|
+
lineNumbers: boolean;
|
|
104
|
+
download: boolean;
|
|
105
|
+
labels: Partial<MarkdownLabels> | undefined;
|
|
106
|
+
headingOffset: number;
|
|
107
|
+
};
|
|
87
108
|
export declare function setMarkdownContext(context: MarkdownContext): void;
|
|
88
109
|
export declare function getMarkdownContext(): MarkdownContext;
|
|
89
110
|
/** Merges plugins: later ones win, one component or policy function at a time. */
|
|
@@ -11,8 +11,20 @@ export type MarkdownLabels = {
|
|
|
11
11
|
/** Names the table's menu button. */
|
|
12
12
|
tableActions: string;
|
|
13
13
|
copyAsMarkdown: string;
|
|
14
|
+
/** Copies the table as text whose columns line up in a monospace font. */
|
|
15
|
+
copyAsText: string;
|
|
16
|
+
copyAsHtml: string;
|
|
14
17
|
copyAsCsv: string;
|
|
18
|
+
/** Copies the table as a picture, as it is drawn. */
|
|
19
|
+
copyAsPng: string;
|
|
15
20
|
saveAsCsv: string;
|
|
21
|
+
/**
|
|
22
|
+
* Names the toggle that shows a Markdown or HTML code block rendered. It keeps this name
|
|
23
|
+
* either way; whether it is pressed says which is shown.
|
|
24
|
+
*/
|
|
25
|
+
showRendered: string;
|
|
26
|
+
/** Names a rendered HTML code block's frame. */
|
|
27
|
+
htmlPreview: string;
|
|
16
28
|
diagram: string;
|
|
17
29
|
showCode: string;
|
|
18
30
|
showDiagram: string;
|
|
@@ -5,8 +5,13 @@ export const defaultLabels = {
|
|
|
5
5
|
copyFailed: "Couldn't copy",
|
|
6
6
|
tableActions: 'Copy or save this table',
|
|
7
7
|
copyAsMarkdown: 'Copy as Markdown',
|
|
8
|
+
copyAsText: 'Copy as text',
|
|
9
|
+
copyAsHtml: 'Copy as HTML',
|
|
8
10
|
copyAsCsv: 'Copy as CSV',
|
|
11
|
+
copyAsPng: 'Copy as PNG',
|
|
9
12
|
saveAsCsv: 'Save as CSV',
|
|
13
|
+
showRendered: 'Show rendered',
|
|
14
|
+
htmlPreview: 'Rendered HTML',
|
|
10
15
|
diagram: 'Diagram',
|
|
11
16
|
showCode: 'Show code',
|
|
12
17
|
showDiagram: 'Show diagram',
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which code blocks can be shown rendered: Markdown and HTML, when the document's `previews`
|
|
3
|
+
* option asks for them.
|
|
4
|
+
*/
|
|
5
|
+
export type PreviewKind = 'markdown' | 'html';
|
|
6
|
+
/** What `<Markdown previews>` takes: every kind, or the ones listed. */
|
|
7
|
+
export type PreviewsOption = boolean | ReadonlyArray<PreviewKind> | undefined;
|
|
8
|
+
/** What a fence's language renders as, when the option allows it; null for any other block. */
|
|
9
|
+
export declare function previewKind(lang: string | null, previews: PreviewsOption): PreviewKind | null;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which code blocks can be shown rendered: Markdown and HTML, when the document's `previews`
|
|
3
|
+
* option asks for them.
|
|
4
|
+
*/
|
|
5
|
+
const KINDS = { markdown: 'markdown', md: 'markdown', html: 'html' };
|
|
6
|
+
/** What a fence's language renders as, when the option allows it; null for any other block. */
|
|
7
|
+
export function previewKind(lang, previews) {
|
|
8
|
+
if (!previews || lang === null)
|
|
9
|
+
return null;
|
|
10
|
+
const kind = KINDS[lang.toLowerCase()];
|
|
11
|
+
if (kind === undefined)
|
|
12
|
+
return null;
|
|
13
|
+
return previews === true || previews.includes(kind) ? kind : null;
|
|
14
|
+
}
|
|
@@ -23,6 +23,8 @@ export type StaticContext = {
|
|
|
23
23
|
lineNumbers: boolean;
|
|
24
24
|
download: boolean;
|
|
25
25
|
};
|
|
26
|
+
/** Whether a code block in a language can be shown rendered. */
|
|
27
|
+
preview: (lang: string | null) => boolean;
|
|
26
28
|
/** Whether task items can be ticked. */
|
|
27
29
|
tickable: boolean;
|
|
28
30
|
/** Whether a block has been asked for as parts. */
|