@skeudo/html 0.1.0 → 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.
- package/README.md +6 -4
- package/dist/code/enhance.d.ts +6 -1
- package/dist/code/enhance.js +117 -2
- package/dist/code/render.js +114 -9
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/skeudo.css +119 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
# @skeudo/html
|
|
2
2
|
|
|
3
|
-
>
|
|
3
|
+
> **Alpha.** The format and this API will change in ways that break existing sketches and code. Until 1.0, breaking changes come in minor versions (0.1 to 0.2), never in patches, and the [release notes](https://github.com/wlezzar/skeudo/releases) list each one.
|
|
4
4
|
|
|
5
|
-
Renders [Skeudo](https://github.com/wlezzar/skeudo) sketches as HTML. A sketch is pseudocode for code review where every outside effect is tagged, or
|
|
5
|
+
Renders [Skeudo](https://github.com/wlezzar/skeudo) sketches as HTML. A sketch is pseudocode for code review where every outside effect is tagged, or a data model drawn as tables and a story of how their rows change, and its first line says which: `view: code` or `view: data`. The format is defined in [spec/spec.md](https://github.com/wlezzar/skeudo/blob/main/spec/spec.md).
|
|
6
6
|
|
|
7
7
|
For Markdown, use [`markdown-it-skeudo`](https://www.npmjs.com/package/markdown-it-skeudo) or [`remark-skeudo`](https://www.npmjs.com/package/remark-skeudo), which call this package. To render whole pages from the command line, use the [`skeudo`](https://www.npmjs.com/package/skeudo) CLI.
|
|
8
8
|
|
|
@@ -15,7 +15,8 @@ npm install @skeudo/html
|
|
|
15
15
|
```js
|
|
16
16
|
import { toHtml } from '@skeudo/html';
|
|
17
17
|
|
|
18
|
-
const html = toHtml(`
|
|
18
|
+
const html = toHtml(`view: code
|
|
19
|
+
get_user(id):
|
|
19
20
|
row = find(id) ::read:postgres.users
|
|
20
21
|
return row
|
|
21
22
|
`);
|
|
@@ -26,7 +27,7 @@ const html = toHtml(`get_user(id):
|
|
|
26
27
|
The page needs two more things:
|
|
27
28
|
|
|
28
29
|
- **The stylesheet**, `@skeudo/html/skeudo.css`. Import it from a bundler (`import '@skeudo/html/skeudo.css'`), or copy `node_modules/@skeudo/html/dist/skeudo.css` next to your page.
|
|
29
|
-
- **`enhance()`**, called once in the browser. It wires the interaction: hovering a name highlights its other uses and clicking pins them, explanations from `::doc` show in a popover, and data sketches step through their story by click or with the arrow keys. It watches the page, so sketches added later, as when a single-page site changes page, work too. Without it, explanations are the browser's tooltips and a data sketch shows its last step.
|
|
30
|
+
- **`enhance()`**, called once in the browser. It wires the interaction: hovering a name highlights its other uses and clicking pins them, explanations from `::doc` show in a popover, blocks fold and unfold by their chevron, with a folded line showing the tags of the lines under it, and data sketches step through their story by click or with the arrow keys. It watches the page, so sketches added later, as when a single-page site changes page, work too, and a sketch rendered again, as a live preview does on each edit, keeps its step and its blocks as the reader left them. Without it, explanations are the browser's tooltips, every block shows open and a data sketch shows its last step.
|
|
30
31
|
|
|
31
32
|
```js
|
|
32
33
|
import { enhance } from '@skeudo/html';
|
|
@@ -53,6 +54,7 @@ The data view takes no options.
|
|
|
53
54
|
|
|
54
55
|
- `tagHtml(tag, options)` and `tagHast(tag, options)` render one tag as a pill, as the Markdown plugins do for inline code such as `` `::read:postgres.users` ``.
|
|
55
56
|
- `repoHref(repo, location, gitHosts)` is the default link for a `::src` location in a sketch with `::repo`.
|
|
57
|
+
- `unfoldTo(row)` opens the folded blocks around a line's row, for a page that scrolls to a line, as the playground's review panel does. It needs `enhance()`.
|
|
56
58
|
- `escapeHtml(text)`.
|
|
57
59
|
|
|
58
60
|
## License
|
package/dist/code/enhance.d.ts
CHANGED
|
@@ -1,2 +1,7 @@
|
|
|
1
|
-
/** Variable highlighting and
|
|
1
|
+
/** Variable highlighting, explanations and folding for the code sketches under `root`. */
|
|
2
2
|
export declare function enhanceCode(root: HTMLElement | Document): () => void;
|
|
3
|
+
/**
|
|
4
|
+
* Unfolds the blocks around a row of a code sketch, so the row shows, as clicks on their chevrons would,
|
|
5
|
+
* so enhance() remembers them open.
|
|
6
|
+
*/
|
|
7
|
+
export declare function unfoldTo(row: Element): void;
|
package/dist/code/enhance.js
CHANGED
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
* Interaction for rendered code sketches, wired by enhance() in index.ts.
|
|
3
3
|
*/
|
|
4
4
|
import { Popover, POPOVER_DELAY } from '../shared/popover.js';
|
|
5
|
-
/** Variable highlighting and
|
|
5
|
+
/** Variable highlighting, explanations and folding for the code sketches under `root`. */
|
|
6
6
|
export function enhanceCode(root) {
|
|
7
|
-
const off = [highlightVariables(root), showDocs(root)];
|
|
7
|
+
const off = [highlightVariables(root), showDocs(root), folding(root)];
|
|
8
8
|
return () => off.forEach((remove) => remove());
|
|
9
9
|
}
|
|
10
10
|
function highlightVariables(root) {
|
|
@@ -40,6 +40,121 @@ function highlightVariables(root) {
|
|
|
40
40
|
}
|
|
41
41
|
/** A sketch row's depth, from the `--d` its style sets. */
|
|
42
42
|
const rowDepth = (row) => Number(row.style.getPropertyValue('--d')) || 0;
|
|
43
|
+
/** Each foldable header of a code sketch, with a key that finds it again when the sketch is rendered again: its code, and which of the headers with that code it is. */
|
|
44
|
+
function foldableRows(sketch) {
|
|
45
|
+
const seen = new Map();
|
|
46
|
+
return [...sketch.querySelectorAll('.sk-foldable')].map((row) => {
|
|
47
|
+
const code = row.querySelector('.sk-code')?.textContent ?? '';
|
|
48
|
+
const n = (seen.get(code) ?? 0) + 1;
|
|
49
|
+
seen.set(code, n);
|
|
50
|
+
return { key: `${code}\u0000${n}`, row };
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Folding. The chevron on a block header folds or unfolds its block, and clicking a folded line's
|
|
55
|
+
* summary unfolds it. The renderer marks which lines start folded and hidden; code.css acts on those
|
|
56
|
+
* marks only under `sk-folding`, which this puts on the root, so a page without enhance() shows every line.
|
|
57
|
+
*/
|
|
58
|
+
function folding(root) {
|
|
59
|
+
const host = root instanceof Document ? root.documentElement : root;
|
|
60
|
+
host.classList.add('sk-folding');
|
|
61
|
+
const win = (root instanceof Document ? root : root.ownerDocument).defaultView;
|
|
62
|
+
const address = () => (win ? win.location.pathname + win.location.search : '');
|
|
63
|
+
let page = address();
|
|
64
|
+
/**
|
|
65
|
+
* The blocks the reader folded or opened, by the sketch's position under root and the header's key,
|
|
66
|
+
* with whether the markup started the block folded. A preview that renders the page again on each edit,
|
|
67
|
+
* as VS Code's does, then keeps each block as the reader left it, unless the edit added or removed its
|
|
68
|
+
* `::fold`. It is forgotten when the page's address changes, as for the steps of data sketches.
|
|
69
|
+
*/
|
|
70
|
+
const remembered = new Map();
|
|
71
|
+
const sketches = () => [...root.querySelectorAll('[data-sketch]')];
|
|
72
|
+
/** Puts back the blocks of each sketch not seen yet as the reader left them, and remembers how its markup starts them. */
|
|
73
|
+
function start() {
|
|
74
|
+
if (address() !== page) {
|
|
75
|
+
remembered.clear();
|
|
76
|
+
page = address();
|
|
77
|
+
}
|
|
78
|
+
sketches().forEach((sketch, i) => {
|
|
79
|
+
if (sketch.hasAttribute('data-sk-live'))
|
|
80
|
+
return;
|
|
81
|
+
sketch.setAttribute('data-sk-live', '');
|
|
82
|
+
const before = remembered.get(i);
|
|
83
|
+
const now = new Map();
|
|
84
|
+
for (const { key, row } of foldableRows(sketch)) {
|
|
85
|
+
const markup = row.classList.contains('sk-folded');
|
|
86
|
+
const left = before?.get(key);
|
|
87
|
+
const folded = left && left.markup === markup ? left.folded : markup;
|
|
88
|
+
if (folded !== markup)
|
|
89
|
+
setFolded(row, folded);
|
|
90
|
+
now.set(key, { folded, markup });
|
|
91
|
+
}
|
|
92
|
+
remembered.set(i, now);
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
const click = (e) => {
|
|
96
|
+
// A variable in a summary, as in `← body`, pins its highlight instead.
|
|
97
|
+
if (e.target?.closest?.('[data-var]'))
|
|
98
|
+
return;
|
|
99
|
+
const el = e.target?.closest?.('.sk-fold, .sk-folded > .sk-tags > .sk-rollup');
|
|
100
|
+
const row = el?.closest('.sk-row');
|
|
101
|
+
const sketch = row?.closest('[data-sketch]');
|
|
102
|
+
if (!row || !sketch)
|
|
103
|
+
return;
|
|
104
|
+
e.preventDefault();
|
|
105
|
+
const folded = !row.classList.contains('sk-folded');
|
|
106
|
+
setFolded(row, folded);
|
|
107
|
+
const entry = remembered.get(sketches().indexOf(sketch))?.get(foldableRows(sketch).find((r) => r.row === row)?.key ?? '');
|
|
108
|
+
if (entry)
|
|
109
|
+
entry.folded = folded;
|
|
110
|
+
};
|
|
111
|
+
start();
|
|
112
|
+
const observer = new MutationObserver(start);
|
|
113
|
+
observer.observe(host, { childList: true, subtree: true });
|
|
114
|
+
root.addEventListener('click', click);
|
|
115
|
+
return () => {
|
|
116
|
+
observer.disconnect();
|
|
117
|
+
host.classList.remove('sk-folding');
|
|
118
|
+
root.removeEventListener('click', click);
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
function setFolded(row, folded) {
|
|
122
|
+
row.classList.toggle('sk-folded', folded);
|
|
123
|
+
const button = row.querySelector('.sk-fold');
|
|
124
|
+
button?.setAttribute('aria-expanded', String(!folded));
|
|
125
|
+
button?.setAttribute('aria-label', folded ? 'Unfold block' : 'Fold block');
|
|
126
|
+
if (row.parentElement)
|
|
127
|
+
hideFolded(row.parentElement);
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Hides the lines inside folded blocks, and shows the others, as the renderer does for the blocks that
|
|
131
|
+
* start folded. A blank line takes the depth of the shallower line around it, so one inside a block is
|
|
132
|
+
* hidden with it.
|
|
133
|
+
*/
|
|
134
|
+
function hideFolded(lines) {
|
|
135
|
+
let foldDepth = -1;
|
|
136
|
+
for (const row of lines.children) {
|
|
137
|
+
const depth = rowDepth(row);
|
|
138
|
+
const hidden = foldDepth >= 0 && depth > foldDepth;
|
|
139
|
+
row.classList.toggle('sk-hide', hidden);
|
|
140
|
+
if (!hidden)
|
|
141
|
+
foldDepth = row.classList.contains('sk-folded') ? depth : -1;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Unfolds the blocks around a row of a code sketch, so the row shows, as clicks on their chevrons would,
|
|
146
|
+
* so enhance() remembers them open.
|
|
147
|
+
*/
|
|
148
|
+
export function unfoldTo(row) {
|
|
149
|
+
let depth = rowDepth(row);
|
|
150
|
+
for (let r = row.previousElementSibling; r && depth > 0; r = r.previousElementSibling) {
|
|
151
|
+
if (r.classList.contains('sk-blank') || rowDepth(r) >= depth)
|
|
152
|
+
continue;
|
|
153
|
+
depth = rowDepth(r);
|
|
154
|
+
if (r.classList.contains('sk-folded'))
|
|
155
|
+
r.querySelector('.sk-fold')?.click();
|
|
156
|
+
}
|
|
157
|
+
}
|
|
43
158
|
/**
|
|
44
159
|
* Shows the explanation of a call or a line in a popover. The popover sits on the page's body with a
|
|
45
160
|
* fixed position, since the sketch's box scrolls and would cut it off. It stands in for the element's
|
package/dist/code/render.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { CHANGE_KINDS, formatLocation, HIDDEN_KINDS, } from '@skeudo/core';
|
|
1
|
+
import { CHANGE_KINDS, formatLocation, HIDDEN_KINDS, rollup, } from '@skeudo/core';
|
|
2
2
|
import { classSafe, h, link, serialize, text } from '../shared/hast.js';
|
|
3
3
|
import { textWidth } from '../shared/width.js';
|
|
4
4
|
import { repoHref } from './links.js';
|
|
5
|
-
const KNOWN_KINDS = new Set(['read', 'write', 'fail', 'src', 'repo', 'doc', 'danger', 'unsure', 'untrusted', 'secret', ...CHANGE_KINDS]);
|
|
6
|
-
/** Tags a renderer hides: `::src` and `::repo` become links,
|
|
5
|
+
const KNOWN_KINDS = new Set(['read', 'write', 'fail', 'src', 'repo', 'doc', 'fold', 'danger', 'unsure', 'untrusted', 'secret', ...CHANGE_KINDS]);
|
|
6
|
+
/** Tags a renderer hides: `::src` and `::repo` become links, `::doc` shows on hover, and `::fold` folds its block. */
|
|
7
7
|
const isHiddenTag = (tag) => HIDDEN_KINDS.has(tag.kind);
|
|
8
8
|
const DEFAULT_LABEL_KINDS = ['untrusted', 'secret'];
|
|
9
9
|
/** Text that is copied with the sketch but not shown, such as the `::` of a tag. */
|
|
@@ -162,8 +162,11 @@ function gutterWidth(lines, gutter) {
|
|
|
162
162
|
const digits = Math.max(1, ...lines.map((l) => (l.src?.start !== undefined && !l.src.inherited ? String(l.src.start).length : 1)));
|
|
163
163
|
return 2 + 0.78 * Math.max(4.5, 1.5 + digits);
|
|
164
164
|
}
|
|
165
|
-
/**
|
|
166
|
-
|
|
165
|
+
/**
|
|
166
|
+
* The `sk-fit-*` class for a line whose trailing tags (given as widths) may not fit in the tag column, if
|
|
167
|
+
* any. `prefix` names the class: `sk-fit-90` and `sk-under`, or for a folded line `sk-ffit-90` and `sk-funder`.
|
|
168
|
+
*/
|
|
169
|
+
function fitClass(widths, before, prefix = '') {
|
|
167
170
|
if (!widths.length)
|
|
168
171
|
return undefined;
|
|
169
172
|
const column = TAG_COLUMN_PADDING + widths.reduce((a, b) => a + b, 0) + TAG_GAP * (widths.length - 1);
|
|
@@ -172,14 +175,88 @@ function fitClass(widths, before) {
|
|
|
172
175
|
const need = Math.ceil((before + column) / FIT_STEP) * FIT_STEP;
|
|
173
176
|
if (need < FIT_MIN)
|
|
174
177
|
return undefined;
|
|
175
|
-
return need > FIT_MAX ?
|
|
178
|
+
return need > FIT_MAX ? `sk-${prefix}under` : `sk-${prefix}fit-${need}`;
|
|
179
|
+
}
|
|
180
|
+
/** Text for the lines a rolled-up tag comes from: `On line 5`, `On lines 5 and 9`, `On lines 5, 7 and 9`. */
|
|
181
|
+
function onLines(lines) {
|
|
182
|
+
const list = [...new Set(lines)];
|
|
183
|
+
if (list.length === 1)
|
|
184
|
+
return `On line ${list[0]}`;
|
|
185
|
+
return `On lines ${list.slice(0, -1).join(', ')} and ${list[list.length - 1]}`;
|
|
186
|
+
}
|
|
187
|
+
/** The tag a rolled-up tag shows as: the first of them, with their danger notes as its note and no other. */
|
|
188
|
+
function rolledTag(rolled) {
|
|
189
|
+
const { tag, dangers } = rolled;
|
|
190
|
+
// A ::danger tag is amber already, so its note isn't marked again.
|
|
191
|
+
const note = dangers.length ? { text: dangers.join('; '), danger: tag.kind !== 'danger' } : undefined;
|
|
192
|
+
return { ...tag, note };
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* A rolled-up tag as a pill, with a count when several lines carry it. The labeled variables that reach it
|
|
196
|
+
* follow, as `← body`, underlined like their other uses, since the line that shows it is folded away.
|
|
197
|
+
*/
|
|
198
|
+
function rolledHast(rolled, sketch, opts) {
|
|
199
|
+
const el = tagHast(rolledTag(rolled), opts);
|
|
200
|
+
el.properties.className.push('sk-rolled');
|
|
201
|
+
if (rolled.removed)
|
|
202
|
+
el.properties.className.push('sk-rolled-removed');
|
|
203
|
+
el.properties.title = `${onLines(rolled.lines)}${rolled.removed ? ', removed by the change' : ''}`;
|
|
204
|
+
if (rolled.lines.length > 1) {
|
|
205
|
+
const pill = el.children[0];
|
|
206
|
+
pill.children.push(h('span', { className: ['sk-count'] }, text(`×${rolled.lines.length}`)));
|
|
207
|
+
}
|
|
208
|
+
if (rolled.flows.length) {
|
|
209
|
+
const names = rolled.flows.flatMap((name, i) => [...(i ? text(', ') : []), varHast(sketch, name, name, false)]);
|
|
210
|
+
const reach = rolled.flows.map((name) => `${name} (${sketch.variables[name]?.labels.join(', ')})`).join(', ');
|
|
211
|
+
el.children.push(h('span', { className: ['sk-flow'], title: `${reach} reaches this effect` }, [h('span', { className: ['sk-arrow'] }, text('← ')), ...names]));
|
|
212
|
+
}
|
|
213
|
+
return el;
|
|
214
|
+
}
|
|
215
|
+
/** Estimated width of a rolled-up tag, with its count and the variables that reach it. */
|
|
216
|
+
function rolledWidth(rolled) {
|
|
217
|
+
const count = rolled.lines.length > 1 ? (0.35 * EM + textWidth(`×${rolled.lines.length}`)) * TAG_SCALE : 0;
|
|
218
|
+
const flows = rolled.flows.length ? (0.4 * EM + textWidth(`← ${rolled.flows.join(', ')}`)) * TAG_SCALE : 0;
|
|
219
|
+
return tagWidth(rolledTag(rolled)) + count + flows;
|
|
220
|
+
}
|
|
221
|
+
/** The ⋯ that starts a folded line's summary, and the padding around it, in `ch`. */
|
|
222
|
+
const ROLLUP_MARK_WIDTH = 2 + 0.9 * EM * TAG_SCALE;
|
|
223
|
+
/**
|
|
224
|
+
* The summary a folded line shows in its tag column, after its own tags: a ⋯, then the tags of the lines
|
|
225
|
+
* under it. Clicking it unfolds the block.
|
|
226
|
+
*/
|
|
227
|
+
function rollupHast(r, sketch, opts) {
|
|
228
|
+
const folded = `${r.lines} line${r.lines === 1 ? '' : 's'} folded`;
|
|
229
|
+
const warnings = r.diagnostics.length ? `, ${r.diagnostics.length} with a warning` : '';
|
|
230
|
+
return h('span', { className: ['sk-rollup'], title: `${folded}${warnings}. Click to unfold.` }, [
|
|
231
|
+
h('span', { className: ['sk-more'], ariaHidden: 'true' }, text('⋯')),
|
|
232
|
+
...r.tags.map((t) => rolledHast(t, sketch, opts)),
|
|
233
|
+
]);
|
|
234
|
+
}
|
|
235
|
+
/** A line can fold when it is a block header other than the signature. */
|
|
236
|
+
const canFold = (line) => line.header && !line.signature;
|
|
237
|
+
/**
|
|
238
|
+
* The lines hidden at first: the lines of every block tagged `::fold`. A blank line takes the depth of
|
|
239
|
+
* the shallower line around it, so a blank line inside a block is hidden with it. enhance() does the same
|
|
240
|
+
* each time a block is folded or unfolded.
|
|
241
|
+
*/
|
|
242
|
+
function hiddenAtFirst(lines) {
|
|
243
|
+
const hidden = new Set();
|
|
244
|
+
let foldDepth = -1;
|
|
245
|
+
for (const line of lines) {
|
|
246
|
+
if (foldDepth >= 0 && line.depth > foldDepth) {
|
|
247
|
+
hidden.add(line.number);
|
|
248
|
+
continue;
|
|
249
|
+
}
|
|
250
|
+
foldDepth = line.fold && canFold(line) ? line.depth : -1;
|
|
251
|
+
}
|
|
252
|
+
return hidden;
|
|
176
253
|
}
|
|
177
254
|
/** A file path that can break after each slash. */
|
|
178
255
|
function breakablePath(path) {
|
|
179
256
|
const parts = path.split('/');
|
|
180
257
|
return parts.flatMap((part, i) => (i < parts.length - 1 ? [...text(`${part}/`), h('wbr', {})] : text(part)));
|
|
181
258
|
}
|
|
182
|
-
function lineHast(line, sketch, opts, gutter, before) {
|
|
259
|
+
function lineHast(line, sketch, opts, gutter, before, fold) {
|
|
183
260
|
const indent = ' '.repeat(line.depth * (sketch.indentUnit ?? 4));
|
|
184
261
|
const cls = ['sk-row'];
|
|
185
262
|
if (line.blank)
|
|
@@ -193,6 +270,18 @@ function lineHast(line, sketch, opts, gutter, before) {
|
|
|
193
270
|
const diags = sketch.diagnostics.filter((d) => d.line === line.number);
|
|
194
271
|
if (diags.length)
|
|
195
272
|
cls.push('sk-has-diag');
|
|
273
|
+
const { rollup: summary } = fold;
|
|
274
|
+
if (summary) {
|
|
275
|
+
cls.push('sk-foldable');
|
|
276
|
+
if (line.fold)
|
|
277
|
+
cls.push('sk-folded');
|
|
278
|
+
if (summary.changes.length)
|
|
279
|
+
cls.push('sk-ch-in', `sk-ch-in-${summary.changes[0]}`);
|
|
280
|
+
if (summary.diagnostics.length)
|
|
281
|
+
cls.push('sk-diag-in');
|
|
282
|
+
}
|
|
283
|
+
if (fold.hidden)
|
|
284
|
+
cls.push('sk-hide');
|
|
196
285
|
let code = line.segments.flatMap((s) => segmentHast(s, sketch, opts));
|
|
197
286
|
// Wrapped so the strike-through of a removed line skips its indentation.
|
|
198
287
|
if (code.length && line.change?.kind === 'removed')
|
|
@@ -208,15 +297,26 @@ function lineHast(line, sketch, opts, gutter, before) {
|
|
|
208
297
|
const fit = fitClass(widths, before);
|
|
209
298
|
if (fit)
|
|
210
299
|
cls.push(fit);
|
|
300
|
+
if (summary) {
|
|
301
|
+
tags.push(rollupHast(summary, sketch, opts));
|
|
302
|
+
const ffit = fitClass([...widths, ROLLUP_MARK_WIDTH + summary.tags.reduce((w, t) => w + rolledWidth(t) + TAG_GAP, 0)], before, 'f');
|
|
303
|
+
if (ffit)
|
|
304
|
+
cls.push(ffit);
|
|
305
|
+
}
|
|
211
306
|
const title = diags.length ? { title: diags.map((d) => d.message).join('\n') } : {};
|
|
212
307
|
// A line with an explanation gets a marker after its code. Its circle is drawn by code.css, so it isn't copied.
|
|
213
308
|
const mark = line.doc ? [h('span', { className: ['sk-doc-mark'], ariaHidden: 'true' })] : [];
|
|
214
309
|
// The indentation is an element with a width, not bare spaces, since some hosts drop whitespace-only
|
|
215
310
|
// text: VitePress compiles Markdown pages as Vue templates, and Vue does.
|
|
216
311
|
const ind = indent ? [h('span', { className: ['sk-ind'] }, text(indent))] : [];
|
|
312
|
+
// The chevron that folds the block. code.css draws it, so copying the code leaves it out, and shows it
|
|
313
|
+
// only once enhance() runs, since nothing could fold the block without it.
|
|
314
|
+
const chevron = summary
|
|
315
|
+
? [h('button', { type: 'button', className: ['sk-fold'], ariaExpanded: line.fold ? 'false' : 'true', ariaLabel: line.fold ? 'Unfold block' : 'Fold block' })]
|
|
316
|
+
: [];
|
|
217
317
|
return h('div', { className: cls, style: `--d:${line.depth}`, dataLine: line.number, ...title }, [
|
|
218
318
|
h('span', { className: ['sk-gut'] }, gutter ? gutterHast(line.src, sketch, opts) : []),
|
|
219
|
-
withDoc(h('span', { className: ['sk-code'] }, [...ind, ...(code.length ? code : text('')), ...mark]), line.doc),
|
|
319
|
+
withDoc(h('span', { className: ['sk-code'] }, [...ind, ...(code.length ? code : text('')), ...mark, ...chevron]), line.doc),
|
|
220
320
|
h('span', { className: ['sk-tags'] }, tags),
|
|
221
321
|
]);
|
|
222
322
|
}
|
|
@@ -228,7 +328,12 @@ export function codeToHast(sketch, opts = {}) {
|
|
|
228
328
|
// Everything left of the tag column: the gutter and the widest line of code.
|
|
229
329
|
const unit = sketch.indentUnit ?? 4;
|
|
230
330
|
const before = gutterWidth(lines, gutter) + Math.max(0, ...lines.map((l) => codeWidth(l, unit)));
|
|
231
|
-
const
|
|
331
|
+
const hidden = hiddenAtFirst(lines);
|
|
332
|
+
const rows = lines.map((l) => {
|
|
333
|
+
// A block of lines the renderer drops, such as ::src tags, has nothing to fold.
|
|
334
|
+
const summary = canFold(l) ? rollup(sketch, l) : undefined;
|
|
335
|
+
return lineHast(l, sketch, opts, gutter, before, { rollup: summary?.lines ? summary : undefined, hidden: hidden.has(l.number) });
|
|
336
|
+
});
|
|
232
337
|
return h('div', { className: gutter ? ['sketch', 'sk-gutter'] : ['sketch'], style: `--sk-indent:${unit}ch`, dataSketch: true }, [
|
|
233
338
|
h('div', { className: ['sk-lines'] }, rows),
|
|
234
339
|
]);
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { type Sketch } from '@skeudo/core';
|
|
2
2
|
import type { Element as HastElement } from 'hast';
|
|
3
|
+
export { unfoldTo } from './code/enhance.ts';
|
|
3
4
|
import { type CodeHtmlOptions } from './code/render.ts';
|
|
4
5
|
export { escapeHtml } from './shared/hast.ts';
|
|
5
6
|
export { tagHast, tagHtml } from './code/render.ts';
|
|
@@ -22,6 +23,8 @@ export declare function toHtml(input: string | Sketch, opts?: HtmlOptions): stri
|
|
|
22
23
|
* clicking pins the highlight.
|
|
23
24
|
* - Hovering or focusing a call or a line with a `::doc` shows its explanation in a popover. On a
|
|
24
25
|
* block header, the block is highlighted with it.
|
|
26
|
+
* - The chevron on a block header folds or unfolds the block. A folded header shows the tags of the
|
|
27
|
+
* lines under it, and clicking them unfolds it. Blocks tagged `::fold` start folded.
|
|
25
28
|
*
|
|
26
29
|
* In data sketches:
|
|
27
30
|
* - The story starts at its first step. Clicking a step shows its rows, and the arrow keys step through
|
package/dist/index.js
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
*/
|
|
6
6
|
import { parse } from '@skeudo/core';
|
|
7
7
|
import { enhanceCode } from './code/enhance.js';
|
|
8
|
+
export { unfoldTo } from './code/enhance.js';
|
|
8
9
|
import { codeToHast } from './code/render.js';
|
|
9
10
|
import { enhanceData } from './data/enhance.js';
|
|
10
11
|
import { dataToHast } from './data/render.js';
|
|
@@ -37,6 +38,8 @@ export function toHtml(input, opts = {}) {
|
|
|
37
38
|
* clicking pins the highlight.
|
|
38
39
|
* - Hovering or focusing a call or a line with a `::doc` shows its explanation in a popover. On a
|
|
39
40
|
* block header, the block is highlighted with it.
|
|
41
|
+
* - The chevron on a block header folds or unfolds the block. A folded header shows the tags of the
|
|
42
|
+
* lines under it, and clicking them unfolds it. Blocks tagged `::fold` start folded.
|
|
40
43
|
*
|
|
41
44
|
* In data sketches:
|
|
42
45
|
* - The story starts at its first step. Clicking a step shows its rows, and the arrow keys step through
|
package/dist/skeudo.css
CHANGED
|
@@ -229,6 +229,30 @@ a.sk-src:hover { color: var(--sk-blue); text-decoration: underline; }
|
|
|
229
229
|
@container sketch (max-width: 240ch) { .sk-fit-240 > .sk-tags { grid-area: 2 / 2 / auto / -1; padding-left: var(--sk-under-indent); } }
|
|
230
230
|
@container sketch (max-width: 250ch) { .sk-fit-250 > .sk-tags { grid-area: 2 / 2 / auto / -1; padding-left: var(--sk-under-indent); } }
|
|
231
231
|
|
|
232
|
+
/* A folded line's tags include the summary of its block, so it gets a second mark for when it is
|
|
233
|
+
folded: `sk-ffit-90`, or `sk-funder`. */
|
|
234
|
+
.sk-folding .sk-folded.sk-funder > .sk-tags { grid-area: 2 / 2 / auto / -1; padding-left: var(--sk-under-indent); }
|
|
235
|
+
@container sketch (max-width: 60ch) { .sk-folding .sk-folded.sk-ffit-60 > .sk-tags { grid-area: 2 / 2 / auto / -1; padding-left: var(--sk-under-indent); } }
|
|
236
|
+
@container sketch (max-width: 70ch) { .sk-folding .sk-folded.sk-ffit-70 > .sk-tags { grid-area: 2 / 2 / auto / -1; padding-left: var(--sk-under-indent); } }
|
|
237
|
+
@container sketch (max-width: 80ch) { .sk-folding .sk-folded.sk-ffit-80 > .sk-tags { grid-area: 2 / 2 / auto / -1; padding-left: var(--sk-under-indent); } }
|
|
238
|
+
@container sketch (max-width: 90ch) { .sk-folding .sk-folded.sk-ffit-90 > .sk-tags { grid-area: 2 / 2 / auto / -1; padding-left: var(--sk-under-indent); } }
|
|
239
|
+
@container sketch (max-width: 100ch) { .sk-folding .sk-folded.sk-ffit-100 > .sk-tags { grid-area: 2 / 2 / auto / -1; padding-left: var(--sk-under-indent); } }
|
|
240
|
+
@container sketch (max-width: 110ch) { .sk-folding .sk-folded.sk-ffit-110 > .sk-tags { grid-area: 2 / 2 / auto / -1; padding-left: var(--sk-under-indent); } }
|
|
241
|
+
@container sketch (max-width: 120ch) { .sk-folding .sk-folded.sk-ffit-120 > .sk-tags { grid-area: 2 / 2 / auto / -1; padding-left: var(--sk-under-indent); } }
|
|
242
|
+
@container sketch (max-width: 130ch) { .sk-folding .sk-folded.sk-ffit-130 > .sk-tags { grid-area: 2 / 2 / auto / -1; padding-left: var(--sk-under-indent); } }
|
|
243
|
+
@container sketch (max-width: 140ch) { .sk-folding .sk-folded.sk-ffit-140 > .sk-tags { grid-area: 2 / 2 / auto / -1; padding-left: var(--sk-under-indent); } }
|
|
244
|
+
@container sketch (max-width: 150ch) { .sk-folding .sk-folded.sk-ffit-150 > .sk-tags { grid-area: 2 / 2 / auto / -1; padding-left: var(--sk-under-indent); } }
|
|
245
|
+
@container sketch (max-width: 160ch) { .sk-folding .sk-folded.sk-ffit-160 > .sk-tags { grid-area: 2 / 2 / auto / -1; padding-left: var(--sk-under-indent); } }
|
|
246
|
+
@container sketch (max-width: 170ch) { .sk-folding .sk-folded.sk-ffit-170 > .sk-tags { grid-area: 2 / 2 / auto / -1; padding-left: var(--sk-under-indent); } }
|
|
247
|
+
@container sketch (max-width: 180ch) { .sk-folding .sk-folded.sk-ffit-180 > .sk-tags { grid-area: 2 / 2 / auto / -1; padding-left: var(--sk-under-indent); } }
|
|
248
|
+
@container sketch (max-width: 190ch) { .sk-folding .sk-folded.sk-ffit-190 > .sk-tags { grid-area: 2 / 2 / auto / -1; padding-left: var(--sk-under-indent); } }
|
|
249
|
+
@container sketch (max-width: 200ch) { .sk-folding .sk-folded.sk-ffit-200 > .sk-tags { grid-area: 2 / 2 / auto / -1; padding-left: var(--sk-under-indent); } }
|
|
250
|
+
@container sketch (max-width: 210ch) { .sk-folding .sk-folded.sk-ffit-210 > .sk-tags { grid-area: 2 / 2 / auto / -1; padding-left: var(--sk-under-indent); } }
|
|
251
|
+
@container sketch (max-width: 220ch) { .sk-folding .sk-folded.sk-ffit-220 > .sk-tags { grid-area: 2 / 2 / auto / -1; padding-left: var(--sk-under-indent); } }
|
|
252
|
+
@container sketch (max-width: 230ch) { .sk-folding .sk-folded.sk-ffit-230 > .sk-tags { grid-area: 2 / 2 / auto / -1; padding-left: var(--sk-under-indent); } }
|
|
253
|
+
@container sketch (max-width: 240ch) { .sk-folding .sk-folded.sk-ffit-240 > .sk-tags { grid-area: 2 / 2 / auto / -1; padding-left: var(--sk-under-indent); } }
|
|
254
|
+
@container sketch (max-width: 250ch) { .sk-folding .sk-folded.sk-ffit-250 > .sk-tags { grid-area: 2 / 2 / auto / -1; padding-left: var(--sk-under-indent); } }
|
|
255
|
+
|
|
232
256
|
/* Narrow boxes: code wraps (continuation lines hang 2ch past the indentation) and all trailing tags
|
|
233
257
|
move under the code. Nothing scrolls sideways. */
|
|
234
258
|
@container sketch (max-width: 36rem) {
|
|
@@ -346,6 +370,101 @@ a.sk-src:hover { color: var(--sk-blue); text-decoration: underline; }
|
|
|
346
370
|
}
|
|
347
371
|
.sk-ch-removed > .sk-tags .sk-tagw:not(:has(.sk-k-removed)) { opacity: 0.5; }
|
|
348
372
|
|
|
373
|
+
/* ---------- Folds ---------- */
|
|
374
|
+
|
|
375
|
+
/* Folding needs enhance(), which puts sk-folding on its root. Without it, every block stays open and
|
|
376
|
+
no chevron or summary shows. */
|
|
377
|
+
.sk-fold,
|
|
378
|
+
.sk-rollup { display: none; }
|
|
379
|
+
|
|
380
|
+
/* The chevron hangs left of the header's text, in the indentation or the gutter's padding. It shows on
|
|
381
|
+
a folded line, and on an open one while the row is hovered or the chevron has focus. */
|
|
382
|
+
.sk-folding .sk-fold {
|
|
383
|
+
display: block;
|
|
384
|
+
position: absolute;
|
|
385
|
+
top: 0;
|
|
386
|
+
left: calc(var(--d, 0) * var(--sk-indent) - 2.2ch);
|
|
387
|
+
width: 2ch;
|
|
388
|
+
height: calc(var(--sk-lh) * 1em);
|
|
389
|
+
margin: 0;
|
|
390
|
+
padding: 0;
|
|
391
|
+
border: 0;
|
|
392
|
+
border-radius: 3px;
|
|
393
|
+
background: none;
|
|
394
|
+
color: var(--sk-muted);
|
|
395
|
+
cursor: pointer;
|
|
396
|
+
opacity: 0;
|
|
397
|
+
}
|
|
398
|
+
.sk-folding .sk-fold::before {
|
|
399
|
+
content: '';
|
|
400
|
+
position: absolute;
|
|
401
|
+
left: 50%;
|
|
402
|
+
top: 50%;
|
|
403
|
+
width: 0.42em;
|
|
404
|
+
height: 0.42em;
|
|
405
|
+
border-right: 1.5px solid currentColor;
|
|
406
|
+
border-bottom: 1.5px solid currentColor;
|
|
407
|
+
transform: translate(-50%, -70%) rotate(45deg);
|
|
408
|
+
}
|
|
409
|
+
.sk-folding .sk-folded .sk-fold::before { transform: translate(-70%, -50%) rotate(-45deg); }
|
|
410
|
+
.sk-folding .sk-row:hover .sk-fold,
|
|
411
|
+
.sk-folding .sk-fold:focus-visible,
|
|
412
|
+
.sk-folding .sk-folded .sk-fold { opacity: 1; }
|
|
413
|
+
.sk-folding .sk-fold:hover { color: var(--sk-fg); }
|
|
414
|
+
.sk-folding .sk-fold:focus { outline: none; }
|
|
415
|
+
.sk-folding .sk-fold:focus-visible { outline: 1.5px solid color-mix(in srgb, var(--sk-blue) 60%, transparent); }
|
|
416
|
+
|
|
417
|
+
/* Lines inside a folded block keep their width, so folding never moves the tag column. */
|
|
418
|
+
.sk-folding .sk-hide {
|
|
419
|
+
height: 0;
|
|
420
|
+
overflow: hidden;
|
|
421
|
+
visibility: hidden;
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
/* A folded line's summary: a ⋯, then the tags of the lines under it, on a faint band that sets them
|
|
425
|
+
apart from the line's own tags. Clicking it unfolds the block. */
|
|
426
|
+
.sk-folding .sk-folded > .sk-tags > .sk-rollup {
|
|
427
|
+
display: inline-flex;
|
|
428
|
+
flex-wrap: wrap;
|
|
429
|
+
align-items: baseline;
|
|
430
|
+
gap: 0.15rem 0.45rem;
|
|
431
|
+
min-width: 0;
|
|
432
|
+
padding: 0 0.5em 0 0.4em;
|
|
433
|
+
border-radius: 0.75em;
|
|
434
|
+
background: color-mix(in srgb, var(--sk-muted) 11%, transparent);
|
|
435
|
+
cursor: pointer;
|
|
436
|
+
}
|
|
437
|
+
.sk-folding .sk-folded > .sk-tags > .sk-rollup:hover { background: color-mix(in srgb, var(--sk-muted) 19%, transparent); }
|
|
438
|
+
.sk-more {
|
|
439
|
+
color: var(--sk-muted);
|
|
440
|
+
font-weight: 700;
|
|
441
|
+
font-size: 0.9em;
|
|
442
|
+
user-select: none;
|
|
443
|
+
}
|
|
444
|
+
.sk-count {
|
|
445
|
+
margin-left: 0.35em;
|
|
446
|
+
font-weight: 700;
|
|
447
|
+
}
|
|
448
|
+
/* The labeled variables that reach a rolled-up effect: `← body`, set like a note. */
|
|
449
|
+
.sk-flow {
|
|
450
|
+
margin-left: 0.4em;
|
|
451
|
+
font-size: 0.8em;
|
|
452
|
+
color: var(--sk-muted);
|
|
453
|
+
}
|
|
454
|
+
.sk-arrow { user-select: none; }
|
|
455
|
+
.sk-rolled-removed { opacity: 0.5; }
|
|
456
|
+
.sk-rolled-removed .sk-tag { text-decoration: line-through; }
|
|
457
|
+
|
|
458
|
+
/* A folded line marks what its block holds: a dotted bar in the gutter for a change inside, and the
|
|
459
|
+
warning bar for a warning inside. */
|
|
460
|
+
.sk-folding .sk-folded.sk-ch-in:not(.sk-ch) > .sk-gut {
|
|
461
|
+
background: repeating-linear-gradient(var(--sk-ch-in) 0 3px, transparent 3px 6px) no-repeat right 0.35rem top 0 / 3px 100%;
|
|
462
|
+
}
|
|
463
|
+
.sk-ch-in-new { --sk-ch-in: var(--sk-green); }
|
|
464
|
+
.sk-ch-in-changed { --sk-ch-in: var(--sk-blue); }
|
|
465
|
+
.sk-ch-in-removed { --sk-ch-in: var(--sk-red); }
|
|
466
|
+
.sk-folding .sk-folded.sk-diag-in { box-shadow: inset 3px 0 var(--sk-amber); }
|
|
467
|
+
|
|
349
468
|
/* ---------- Code ---------- */
|
|
350
469
|
|
|
351
470
|
.sk-word { color: var(--sk-violet); }
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@skeudo/html",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Renders Skeudo sketches as HTML, with the stylesheet, and a browser script for hovering names and stepping through data stories.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"skeudo",
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
43
|
"@types/hast": "^3.0.5",
|
|
44
|
-
"@skeudo/core": "0.
|
|
44
|
+
"@skeudo/core": "0.2.0"
|
|
45
45
|
},
|
|
46
46
|
"scripts": {
|
|
47
47
|
"build": "node ../../scripts/build-library.mts"
|