@skeudo/html 0.0.0-stage → 0.1.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.
- package/LICENSE +21 -0
- package/README.md +59 -2
- package/dist/code/enhance.d.ts +2 -0
- package/dist/code/enhance.js +175 -0
- package/dist/code/links.d.ts +20 -0
- package/dist/code/links.js +45 -0
- package/dist/code/render.d.ts +21 -0
- package/dist/code/render.js +235 -0
- package/dist/data/enhance.d.ts +1 -0
- package/dist/data/enhance.js +355 -0
- package/dist/data/render.d.ts +4 -0
- package/dist/data/render.js +274 -0
- package/dist/index.d.ts +35 -0
- package/dist/index.js +53 -0
- package/dist/shared/hast.d.ts +10 -0
- package/dist/shared/hast.js +48 -0
- package/dist/shared/popover.d.ts +23 -0
- package/dist/shared/popover.js +76 -0
- package/dist/shared/width.d.ts +4 -0
- package/dist/shared/width.js +37 -0
- package/dist/skeudo.css +904 -0
- package/package.json +47 -3
package/dist/index.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Renders sketches of every view as HTML. Each view has its own folder, with its renderer (a hast
|
|
3
|
+
* tree), its interaction (enhance) and its stylesheet. This file picks the view of each sketch, so
|
|
4
|
+
* callers such as the Markdown plugins never name a view. skeudo.css holds every view's styles.
|
|
5
|
+
*/
|
|
6
|
+
import { parse } from '@skeudo/core';
|
|
7
|
+
import { enhanceCode } from './code/enhance.js';
|
|
8
|
+
import { codeToHast } from './code/render.js';
|
|
9
|
+
import { enhanceData } from './data/enhance.js';
|
|
10
|
+
import { dataToHast } from './data/render.js';
|
|
11
|
+
import { serialize } from './shared/hast.js';
|
|
12
|
+
export { escapeHtml } from './shared/hast.js';
|
|
13
|
+
export { tagHast, tagHtml } from './code/render.js';
|
|
14
|
+
export { repoHref, srcBaseHref } from './code/links.js';
|
|
15
|
+
/**
|
|
16
|
+
* Builds a sketch (source text or an already parsed sketch) as a hast tree: one element, the same
|
|
17
|
+
* markup toHtml returns.
|
|
18
|
+
*/
|
|
19
|
+
export function toHast(input, opts = {}) {
|
|
20
|
+
const sketch = typeof input === 'string' ? parse(input, opts) : input;
|
|
21
|
+
switch (sketch.view) {
|
|
22
|
+
case 'code':
|
|
23
|
+
return codeToHast(sketch, opts);
|
|
24
|
+
case 'data':
|
|
25
|
+
return dataToHast(sketch);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
/** Renders a sketch (source text or an already parsed sketch) to an HTML string. */
|
|
29
|
+
export function toHtml(input, opts = {}) {
|
|
30
|
+
return serialize(toHast(input, opts));
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Makes rendered sketches interactive.
|
|
34
|
+
*
|
|
35
|
+
* In code sketches:
|
|
36
|
+
* - Hovering a binding, parameter or labeled name highlights all its uses within the same sketch;
|
|
37
|
+
* clicking pins the highlight.
|
|
38
|
+
* - Hovering or focusing a call or a line with a `::doc` shows its explanation in a popover. On a
|
|
39
|
+
* block header, the block is highlighted with it.
|
|
40
|
+
*
|
|
41
|
+
* In data sketches:
|
|
42
|
+
* - The story starts at its first step. Clicking a step shows its rows, and the arrow keys step through
|
|
43
|
+
* the story while the sketch has focus.
|
|
44
|
+
* - Hovering a reference highlights the row it points at, and hovering a row the references to it.
|
|
45
|
+
* - Resting on a step or a table's name shows its explanation; clicking its mark keeps it open.
|
|
46
|
+
*
|
|
47
|
+
* Listeners sit on `root`, so sketches rendered under it later work too. Returns a function that
|
|
48
|
+
* removes them.
|
|
49
|
+
*/
|
|
50
|
+
export function enhance(root = document) {
|
|
51
|
+
const off = [enhanceCode(root), enhanceData(root)];
|
|
52
|
+
return () => off.forEach((remove) => remove());
|
|
53
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Element as HastElement, ElementContent, Properties, Text } from 'hast';
|
|
2
|
+
export declare const escapeHtml: (s: string) => string;
|
|
3
|
+
export declare function h(tagName: string, properties: Properties, children?: ElementContent[]): HastElement;
|
|
4
|
+
/** A text node, or none for an empty string. */
|
|
5
|
+
export declare const text: (value: string) => Text[];
|
|
6
|
+
/** Serializes the elements and text the renderer builds. Invisible spaces are written as entities. */
|
|
7
|
+
export declare function serialize(node: ElementContent): string;
|
|
8
|
+
export declare const classSafe: (s: string) => string;
|
|
9
|
+
/** A link that opens in a new tab, or a plain span without an href. */
|
|
10
|
+
export declare function link(cls: string[], href: string | undefined, title: string, children: ElementContent[]): HastElement;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
const ESC = { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' };
|
|
2
|
+
export const escapeHtml = (s) => s.replace(/[&<>"']/g, (c) => ESC[c]);
|
|
3
|
+
/*
|
|
4
|
+
* The renderer builds a hast tree (https://github.com/syntax-tree/hast), the HTML syntax tree that
|
|
5
|
+
* remark and rehype plugins pass around, so a plugin can hand it on without parsing HTML. toHtml
|
|
6
|
+
* serializes the same tree. Properties follow hast's naming: `className` for `class`, and camel case
|
|
7
|
+
* for data attributes (`dataLine` for `data-line`).
|
|
8
|
+
*/
|
|
9
|
+
export function h(tagName, properties, children = []) {
|
|
10
|
+
return { type: 'element', tagName, properties, children };
|
|
11
|
+
}
|
|
12
|
+
/** A text node, or none for an empty string. */
|
|
13
|
+
export const text = (value) => (value ? [{ type: 'text', value }] : []);
|
|
14
|
+
const VOID_ELEMENTS = new Set(['wbr', 'col']);
|
|
15
|
+
function attributeName(property) {
|
|
16
|
+
if (property === 'className')
|
|
17
|
+
return 'class';
|
|
18
|
+
if (property === 'tabIndex')
|
|
19
|
+
return 'tabindex';
|
|
20
|
+
const prefix = /^(data|aria)[A-Z]/.exec(property)?.[1];
|
|
21
|
+
if (prefix)
|
|
22
|
+
return `${prefix}${property.slice(prefix.length).replace(/[A-Z]/g, (c) => `-${c.toLowerCase()}`)}`;
|
|
23
|
+
return property;
|
|
24
|
+
}
|
|
25
|
+
/** Serializes the elements and text the renderer builds. Invisible spaces are written as entities. */
|
|
26
|
+
export function serialize(node) {
|
|
27
|
+
if (node.type === 'text')
|
|
28
|
+
return escapeHtml(node.value).replaceAll('', '​');
|
|
29
|
+
if (node.type !== 'element')
|
|
30
|
+
return '';
|
|
31
|
+
let attributes = '';
|
|
32
|
+
for (const [property, value] of Object.entries(node.properties)) {
|
|
33
|
+
if (value === undefined || value === null || value === false)
|
|
34
|
+
continue;
|
|
35
|
+
const name = attributeName(property);
|
|
36
|
+
attributes += value === true ? ` ${name}` : ` ${name}="${escapeHtml(Array.isArray(value) ? value.join(' ') : String(value))}"`;
|
|
37
|
+
}
|
|
38
|
+
const open = `<${node.tagName}${attributes}>`;
|
|
39
|
+
return VOID_ELEMENTS.has(node.tagName) ? open : `${open}${node.children.map(serialize).join('')}</${node.tagName}>`;
|
|
40
|
+
}
|
|
41
|
+
export const classSafe = (s) => s.replace(/[^\p{L}\p{N}_-]/gu, '_');
|
|
42
|
+
/** A link that opens in a new tab, or a plain span without an href. */
|
|
43
|
+
export function link(cls, href, title, children) {
|
|
44
|
+
const t = title ? { title } : {};
|
|
45
|
+
return href
|
|
46
|
+
? h('a', { className: cls, href, target: '_blank', rel: ['noopener'], ...t }, children)
|
|
47
|
+
: h('span', { className: cls, ...t }, children);
|
|
48
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/** How long the pointer rests on something before its explanation shows, in milliseconds. */
|
|
2
|
+
export declare const POPOVER_DELAY = 200;
|
|
3
|
+
/** One popover, created on the page's body the first time it shows. */
|
|
4
|
+
export declare class Popover {
|
|
5
|
+
private readonly doc;
|
|
6
|
+
/** For `aria-describedby` on what the popover explains. */
|
|
7
|
+
readonly id: string;
|
|
8
|
+
private el;
|
|
9
|
+
constructor(doc: Document);
|
|
10
|
+
/** Fills the popover with `content` and shows it. Place it next, with below() or beside(). */
|
|
11
|
+
show(content: Node[]): void;
|
|
12
|
+
/** Places the popover under `box`, or above it when there's no room below, starting at `left`. */
|
|
13
|
+
below(box: DOMRect, left: number): void;
|
|
14
|
+
/** Places the popover right of `box`, level with its top, or under it when there's no room on the right. */
|
|
15
|
+
beside(box: DOMRect): void;
|
|
16
|
+
/** Whether `node` is inside the popover. */
|
|
17
|
+
contains(node: Node | null): boolean;
|
|
18
|
+
hide(): void;
|
|
19
|
+
remove(): void;
|
|
20
|
+
private measure;
|
|
21
|
+
private viewport;
|
|
22
|
+
private moveTo;
|
|
23
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The popover that explanations show in, for every view. It sits on the page's body with a fixed
|
|
3
|
+
* position, since a sketch's box scrolls and would cut it off. It doesn't follow the page, so a view
|
|
4
|
+
* hides it when anything scrolls. Its styles are in popover.css.
|
|
5
|
+
*/
|
|
6
|
+
/** How long the pointer rests on something before its explanation shows, in milliseconds. */
|
|
7
|
+
export const POPOVER_DELAY = 200;
|
|
8
|
+
/** Space between the popover and what it explains, and between the popover and the window's edge, in pixels. */
|
|
9
|
+
const GAP = 4;
|
|
10
|
+
const MARGIN = 8;
|
|
11
|
+
let popovers = 0;
|
|
12
|
+
/** One popover, created on the page's body the first time it shows. */
|
|
13
|
+
export class Popover {
|
|
14
|
+
doc;
|
|
15
|
+
/** For `aria-describedby` on what the popover explains. */
|
|
16
|
+
id = `sk-doc-pop-${++popovers}`;
|
|
17
|
+
el;
|
|
18
|
+
constructor(doc) {
|
|
19
|
+
this.doc = doc;
|
|
20
|
+
}
|
|
21
|
+
/** Fills the popover with `content` and shows it. Place it next, with below() or beside(). */
|
|
22
|
+
show(content) {
|
|
23
|
+
if (!this.el?.isConnected) {
|
|
24
|
+
this.el = this.doc.createElement('div');
|
|
25
|
+
this.el.className = 'sk-doc-pop';
|
|
26
|
+
this.el.id = this.id;
|
|
27
|
+
this.el.setAttribute('role', 'tooltip');
|
|
28
|
+
this.doc.body.append(this.el);
|
|
29
|
+
}
|
|
30
|
+
this.el.replaceChildren(...content);
|
|
31
|
+
this.el.hidden = false;
|
|
32
|
+
}
|
|
33
|
+
/** Places the popover under `box`, or above it when there's no room below, starting at `left`. */
|
|
34
|
+
below(box, left) {
|
|
35
|
+
const { width, height } = this.measure();
|
|
36
|
+
const { vw, vh } = this.viewport();
|
|
37
|
+
const under = box.bottom + GAP;
|
|
38
|
+
const over = box.top - GAP - height;
|
|
39
|
+
this.moveTo(left, under + height <= vh - MARGIN || over < MARGIN ? under : over, width, height, vw, vh);
|
|
40
|
+
}
|
|
41
|
+
/** Places the popover right of `box`, level with its top, or under it when there's no room on the right. */
|
|
42
|
+
beside(box) {
|
|
43
|
+
const { width, height } = this.measure();
|
|
44
|
+
const { vw, vh } = this.viewport();
|
|
45
|
+
const left = box.right + 2 * GAP;
|
|
46
|
+
if (left + width > vw - MARGIN)
|
|
47
|
+
this.below(box, box.left);
|
|
48
|
+
else
|
|
49
|
+
this.moveTo(left, box.top, width, height, vw, vh);
|
|
50
|
+
}
|
|
51
|
+
/** Whether `node` is inside the popover. */
|
|
52
|
+
contains(node) {
|
|
53
|
+
return !!node && !!this.el?.contains(node);
|
|
54
|
+
}
|
|
55
|
+
hide() {
|
|
56
|
+
if (this.el)
|
|
57
|
+
this.el.hidden = true;
|
|
58
|
+
}
|
|
59
|
+
remove() {
|
|
60
|
+
this.el?.remove();
|
|
61
|
+
}
|
|
62
|
+
measure() {
|
|
63
|
+
const el = this.el;
|
|
64
|
+
el.style.left = '0px';
|
|
65
|
+
el.style.top = '0px';
|
|
66
|
+
return el.getBoundingClientRect();
|
|
67
|
+
}
|
|
68
|
+
viewport() {
|
|
69
|
+
return { vw: this.doc.documentElement.clientWidth, vh: this.doc.documentElement.clientHeight };
|
|
70
|
+
}
|
|
71
|
+
moveTo(left, top, width, height, vw, vh) {
|
|
72
|
+
const el = this.el;
|
|
73
|
+
el.style.left = `${Math.max(MARGIN, Math.min(left, vw - width - MARGIN))}px`;
|
|
74
|
+
el.style.top = `${Math.max(MARGIN, Math.min(top, vh - height - MARGIN))}px`;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
/** Estimated width of a text, in `ch` of a monospace font. */
|
|
2
|
+
export declare function textWidth(s: string): number;
|
|
3
|
+
/** Estimated width of a text in a sans-serif font, in `ch` of that font, with a margin for wider fonts. */
|
|
4
|
+
export declare function proportionalWidth(s: string): number;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Width estimates. A renderer can't measure the page, but sketches are set in a monospace font, so
|
|
3
|
+
* widths can be estimated in `ch`: one per character, two for a wide one.
|
|
4
|
+
*/
|
|
5
|
+
const WIDE_RE = /[ᄀ-ᅟ⺀-가-힣豈-︰-﹏-⦆¢-₩\u{1f300}-\u{1faff}\u{20000}-\u{3fffd}]/u;
|
|
6
|
+
/** Estimated width of a text, in `ch` of a monospace font. */
|
|
7
|
+
export function textWidth(s) {
|
|
8
|
+
let w = 0;
|
|
9
|
+
for (const c of s)
|
|
10
|
+
w += WIDE_RE.test(c) ? 2 : 1;
|
|
11
|
+
return w;
|
|
12
|
+
}
|
|
13
|
+
/*
|
|
14
|
+
* Widths of the printable ASCII characters in a common sans-serif font (Helvetica's, in thousandths of
|
|
15
|
+
* an em), from space to `~`. Other fonts differ a little, so proportionalWidth() adds a margin.
|
|
16
|
+
*/
|
|
17
|
+
// prettier-ignore
|
|
18
|
+
const SANS = [
|
|
19
|
+
278, 278, 355, 556, 556, 889, 667, 191, 333, 333, 389, 584, 278, 333, 278, 278, // space to /
|
|
20
|
+
556, 556, 556, 556, 556, 556, 556, 556, 556, 556, 278, 278, 584, 584, 584, 556, // 0 to ?
|
|
21
|
+
1015, 667, 667, 722, 722, 667, 611, 778, 722, 278, 500, 667, 556, 833, 722, 778, // @ to O
|
|
22
|
+
667, 778, 722, 667, 611, 722, 667, 944, 667, 667, 611, 278, 278, 278, 469, 556, // P to _
|
|
23
|
+
333, 556, 556, 500, 556, 556, 278, 556, 556, 222, 222, 500, 222, 833, 556, 556, // ` to o
|
|
24
|
+
556, 556, 333, 500, 278, 556, 500, 722, 500, 500, 500, 334, 260, 334, 584, // p to ~
|
|
25
|
+
];
|
|
26
|
+
/** The width of `0` in the same units: what `ch` measures. */
|
|
27
|
+
const ZERO = 556;
|
|
28
|
+
const MARGIN = 1.12;
|
|
29
|
+
/** Estimated width of a text in a sans-serif font, in `ch` of that font, with a margin for wider fonts. */
|
|
30
|
+
export function proportionalWidth(s) {
|
|
31
|
+
let w = 0;
|
|
32
|
+
for (const c of s) {
|
|
33
|
+
const code = c.codePointAt(0);
|
|
34
|
+
w += code >= 32 && code < 127 ? SANS[code - 32] / ZERO : WIDE_RE.test(c) ? 2 : 1;
|
|
35
|
+
}
|
|
36
|
+
return w * MARGIN;
|
|
37
|
+
}
|