@serene-pub/docs 0.0.0-stage → 0.6.0-pr-2
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 +202 -0
- package/NOTICE +17 -0
- package/README.md +286 -2
- package/dist/compile.d.ts +3 -0
- package/dist/compile.js +329 -0
- package/dist/dialect.d.ts +139 -0
- package/dist/dialect.js +465 -0
- package/dist/escape.d.ts +14 -0
- package/dist/escape.js +21 -0
- package/dist/graph.d.ts +31 -0
- package/dist/graph.js +132 -0
- package/dist/highlight.d.ts +7 -0
- package/dist/highlight.js +40 -0
- package/dist/images.d.ts +22 -0
- package/dist/images.js +137 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.js +23 -0
- package/dist/manifest.d.ts +44 -0
- package/dist/manifest.js +72 -0
- package/dist/sources.d.ts +15 -0
- package/dist/sources.js +52 -0
- package/dist/specGraph.d.ts +34 -0
- package/dist/specGraph.js +0 -0
- package/dist/types.d.ts +208 -0
- package/dist/types.js +19 -0
- package/package.json +67 -5
package/dist/dialect.js
ADDED
|
@@ -0,0 +1,465 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The dialect: everything a Serene Pub docs page may say beyond CommonMark,
|
|
3
|
+
* plus the pure helpers the app and the compiler have to agree on.
|
|
4
|
+
*
|
|
5
|
+
* `slugifyHeading` and the one-argument `rewriteDocHref` are shared with the
|
|
6
|
+
* app, which computes the same anchors for the same pages. This file is the
|
|
7
|
+
* one that decides what they are, and it is not the place to have a better
|
|
8
|
+
* idea about slugs: readers have bookmarked them.
|
|
9
|
+
*
|
|
10
|
+
* Rendering is deliberately a lex → resolve → parse sandwich rather than
|
|
11
|
+
* marked's `async: true` walk: one token tree means the previews, the heading
|
|
12
|
+
* ids and the emitted HTML are all read off the same lex, and an image's
|
|
13
|
+
* converted asset can simply be hung on its token.
|
|
14
|
+
*/
|
|
15
|
+
import { posix } from 'node:path';
|
|
16
|
+
import GithubSlugger from 'github-slugger';
|
|
17
|
+
import { Marked } from 'marked';
|
|
18
|
+
import { escapeHtml } from './escape.js';
|
|
19
|
+
/** Re-exported: it was this module's export first, and every caller names it here. */
|
|
20
|
+
export { escapeHtml };
|
|
21
|
+
const ADMONITION_KINDS = ['note', 'tip', 'warning'];
|
|
22
|
+
/** Anything with a scheme, a root, a protocol-relative host, or a bare anchor. */
|
|
23
|
+
const PASSTHROUGH_HREF = /^(?:[a-z][a-z0-9+.-]*:|\/\/|\/|#)/i;
|
|
24
|
+
const MD_HREF = /^([^#?]*\.md)(#.*)?$/i;
|
|
25
|
+
/**
|
|
26
|
+
* An absolute `http(s)` URL: a link off the docs. Every link between pages is
|
|
27
|
+
* rewritten to a root-relative `linkBase` path, so whatever is still absolute
|
|
28
|
+
* here names another site, and it opens in a new window so the reader keeps
|
|
29
|
+
* their place in the docs (owner, 2026-10-01).
|
|
30
|
+
*/
|
|
31
|
+
const OFFSITE_HREF = /^https?:\/\//i;
|
|
32
|
+
const OFFSITE_LINK_ATTRS = ' target="_blank" rel="noopener noreferrer"';
|
|
33
|
+
/** A bare `#anchor`: a link to a heading on the page that wrote it. */
|
|
34
|
+
const SELF_ANCHOR_HREF = /^#(.+)$/;
|
|
35
|
+
/** The slug the page being rendered compiles to — `collectPages`' rule. */
|
|
36
|
+
function pageSlug(ctx) {
|
|
37
|
+
const bare = (ctx.path || '').replace(/\.md$/i, '');
|
|
38
|
+
return ctx.prefix ? `${ctx.prefix}/${bare}` : bare;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* GitHub's heading-slug rule, via `github-slugger`.
|
|
42
|
+
*
|
|
43
|
+
* Not a choice about slugs so much as an admission of where the corpus comes
|
|
44
|
+
* from: the guides lived on GitHub for a year and link each other with the
|
|
45
|
+
* anchors GitHub gave them (`#portable--self-contained-setup`,
|
|
46
|
+
* `#serene_pub_data_dir-is-the-one-exception`), and TypeDoc's markdown emits
|
|
47
|
+
* the same shape (`#slot_value`, `#band_order`). Any other rule makes those
|
|
48
|
+
* dead on arrival.
|
|
49
|
+
*
|
|
50
|
+
* It lowercases, drops the punctuation GitHub drops — `_` and `-` are not
|
|
51
|
+
* punctuation to it, which is the whole of the `SERENE_PUB_DATA_DIR` case —
|
|
52
|
+
* and turns EACH space into a `-`, so an `&` removed from between two spaces
|
|
53
|
+
* leaves two: `Prompt Configs & Summarization Config` becomes
|
|
54
|
+
* `prompt-configs--summarization-config`.
|
|
55
|
+
*
|
|
56
|
+
* This is the stateless single-heading form — a fresh slugger per call, so
|
|
57
|
+
* nothing de-duplicates across calls. A page renders through ONE slugger (see
|
|
58
|
+
* `renderMarkdown`), which is where the `-1`, `-2` suffixes come from.
|
|
59
|
+
*
|
|
60
|
+
* Nothing is trimmed or otherwise tidied on the way in. Heading text reaches
|
|
61
|
+
* this already trimmed by the lexer, and normalisation GitHub does not do is a
|
|
62
|
+
* slug GitHub's readers do not have.
|
|
63
|
+
*/
|
|
64
|
+
export function slugifyHeading(text) {
|
|
65
|
+
return new GithubSlugger().slug(text);
|
|
66
|
+
}
|
|
67
|
+
/** The app's, character for character. Used for descriptions and previews. */
|
|
68
|
+
export function stripInlineMarkdown(text) {
|
|
69
|
+
return text
|
|
70
|
+
.replace(/`([^`]*)`/g, '$1')
|
|
71
|
+
.replace(/[*_~]/g, '')
|
|
72
|
+
.replace(/\[([^\]]*)\]\([^)]*\)/g, '$1')
|
|
73
|
+
.replace(/#/g, '')
|
|
74
|
+
.replace(/\s+/g, ' ')
|
|
75
|
+
.trim();
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Cuts already-stripped text to at most `limit` chars, ellipsis included, at
|
|
79
|
+
* the last word boundary at or before the limit — never mid-word. Falls back
|
|
80
|
+
* to a hard cut when the text has no whitespace to break on. Trailing
|
|
81
|
+
* punctuation left dangling by the cut is trimmed before the ellipsis lands.
|
|
82
|
+
*/
|
|
83
|
+
function truncateAtWord(text, limit) {
|
|
84
|
+
if (text.length <= limit)
|
|
85
|
+
return text;
|
|
86
|
+
const budget = limit - 1;
|
|
87
|
+
let cut = text.slice(0, budget);
|
|
88
|
+
const lastSpace = cut.lastIndexOf(' ');
|
|
89
|
+
if (lastSpace > 0)
|
|
90
|
+
cut = cut.slice(0, lastSpace);
|
|
91
|
+
cut = cut.replace(/[\s,;:—-]+$/, '');
|
|
92
|
+
return `${cut}…`;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Where a relative `.md` link lands, given the page that wrote it. Returns
|
|
96
|
+
* null for anchor-only, offsite and non-markdown hrefs — those pass through
|
|
97
|
+
* untouched, and are never validated.
|
|
98
|
+
*/
|
|
99
|
+
export function resolveDocLink(href, ctx) {
|
|
100
|
+
if (!href || PASSTHROUGH_HREF.test(href))
|
|
101
|
+
return null;
|
|
102
|
+
const parts = MD_HREF.exec(href);
|
|
103
|
+
if (!parts)
|
|
104
|
+
return null;
|
|
105
|
+
const dir = posix.dirname(ctx.path || '.');
|
|
106
|
+
const rel = parts[1];
|
|
107
|
+
const target = posix.normalize(dir === '.' || dir === '' ? rel : `${dir}/${rel}`);
|
|
108
|
+
const bare = target.replace(/\.md$/i, '');
|
|
109
|
+
const slug = ctx.prefix ? `${ctx.prefix}/${bare}` : bare;
|
|
110
|
+
const anchor = (parts[2] ?? '').replace(/^#/, '');
|
|
111
|
+
const linkBase = (ctx.linkBase ?? '/docs').replace(/\/+$/, '');
|
|
112
|
+
return { slug, anchor, href: `${linkBase}/${slug}${anchor ? `#${anchor}` : ''}` };
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Rewrites a relative markdown link into the route it maps to.
|
|
116
|
+
*
|
|
117
|
+
* With no context this is the app's original one-argument function, kept
|
|
118
|
+
* because the app still calls it that way: flat slugs only, `/docs` hardcoded.
|
|
119
|
+
* With a context it understands nested paths and a source prefix, which is
|
|
120
|
+
* what a catalog page linking `pipelines/core_spec_respond.md` needs.
|
|
121
|
+
*/
|
|
122
|
+
export function rewriteDocHref(href, ctx) {
|
|
123
|
+
if (!ctx) {
|
|
124
|
+
const match = href.match(/^\.?\/?([a-z0-9-]+)\.md(#.*)?$/i);
|
|
125
|
+
if (match)
|
|
126
|
+
return `/docs/${match[1]}${match[2] ?? ''}`;
|
|
127
|
+
return href;
|
|
128
|
+
}
|
|
129
|
+
return resolveDocLink(href, ctx)?.href ?? href;
|
|
130
|
+
}
|
|
131
|
+
const IMAGE_BODY = /!\[([^\]]*)\]\(([^)\s]+)(?:\s+"[^"]*")?\)(?:\{w=(\d+)\})?/;
|
|
132
|
+
const BLOCK_IMAGE = new RegExp(`^${IMAGE_BODY.source}[ \\t]*(?:\\n{2,}|\\n?$)`);
|
|
133
|
+
const INLINE_IMAGE = new RegExp(`^${IMAGE_BODY.source}`);
|
|
134
|
+
const ADMONITION = new RegExp(`^:::(${ADMONITION_KINDS.join('|')})[ \\t]*([^\\n]*)\\n([\\s\\S]*?)\\n?:::[ \\t]*(?:\\n+|$)`);
|
|
135
|
+
function imageToken(match, type) {
|
|
136
|
+
return {
|
|
137
|
+
type,
|
|
138
|
+
raw: match[0],
|
|
139
|
+
caption: match[1] ?? '',
|
|
140
|
+
href: match[2] ?? '',
|
|
141
|
+
maxWidth: match[3] ? Number(match[3]) : undefined,
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
function imgTag(token, assetsBase) {
|
|
145
|
+
const alt = escapeHtml(token.caption);
|
|
146
|
+
const src = token.asset ? `${assetsBase.replace(/\/+$/, '')}/${token.asset.file}` : token.href;
|
|
147
|
+
const dims = token.asset && token.asset.width && token.asset.height
|
|
148
|
+
? ` width="${token.asset.width}" height="${token.asset.height}"`
|
|
149
|
+
: '';
|
|
150
|
+
return `<img src="${escapeHtml(src)}" alt="${alt}"${dims} loading="lazy" decoding="async">`;
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Markdown → the article body, with everything the compile needs to check
|
|
154
|
+
* itself read off the same token tree.
|
|
155
|
+
*/
|
|
156
|
+
export async function renderMarkdown(markdown, opts) {
|
|
157
|
+
const headings = [];
|
|
158
|
+
const links = [];
|
|
159
|
+
const images = [];
|
|
160
|
+
const pipelines = [];
|
|
161
|
+
const assetsBase = opts.assetsBase ?? '';
|
|
162
|
+
const slugger = new GithubSlugger();
|
|
163
|
+
const marked = new Marked({
|
|
164
|
+
gfm: true,
|
|
165
|
+
extensions: [
|
|
166
|
+
{
|
|
167
|
+
name: 'docAdmonition',
|
|
168
|
+
level: 'block',
|
|
169
|
+
start(src) {
|
|
170
|
+
return src.match(/^:::/m)?.index;
|
|
171
|
+
},
|
|
172
|
+
tokenizer(src) {
|
|
173
|
+
const match = ADMONITION.exec(src);
|
|
174
|
+
if (!match)
|
|
175
|
+
return undefined;
|
|
176
|
+
return {
|
|
177
|
+
type: 'docAdmonition',
|
|
178
|
+
raw: match[0],
|
|
179
|
+
kind: match[1],
|
|
180
|
+
title: (match[2] ?? '').trim(),
|
|
181
|
+
tokens: this.lexer.blockTokens(match[3] ?? ''),
|
|
182
|
+
};
|
|
183
|
+
},
|
|
184
|
+
renderer(token) {
|
|
185
|
+
const kind = token.kind;
|
|
186
|
+
const title = token.title || kind.charAt(0).toUpperCase() + kind.slice(1);
|
|
187
|
+
const body = this.parser.parse(token.tokens);
|
|
188
|
+
return (`<aside class="doc-admonition doc-admonition-${kind}" role="note">` +
|
|
189
|
+
`<p class="doc-admonition-title">${escapeHtml(title)}</p>${body}</aside>\n`);
|
|
190
|
+
},
|
|
191
|
+
},
|
|
192
|
+
{
|
|
193
|
+
name: 'docFigure',
|
|
194
|
+
level: 'block',
|
|
195
|
+
start(src) {
|
|
196
|
+
return src.match(/^!\[/m)?.index;
|
|
197
|
+
},
|
|
198
|
+
tokenizer(_src) {
|
|
199
|
+
const match = BLOCK_IMAGE.exec(_src);
|
|
200
|
+
return match ? imageToken(match, 'docFigure') : undefined;
|
|
201
|
+
},
|
|
202
|
+
renderer(token) {
|
|
203
|
+
const image = token;
|
|
204
|
+
const style = image.maxWidth ? ` style="max-width:${image.maxWidth}px"` : '';
|
|
205
|
+
const caption = image.caption
|
|
206
|
+
? `<figcaption>${escapeHtml(image.caption)}</figcaption>`
|
|
207
|
+
: '';
|
|
208
|
+
return (`<figure class="doc-figure"${style}>` +
|
|
209
|
+
`${imgTag(image, assetsBase)}${caption}</figure>\n`);
|
|
210
|
+
},
|
|
211
|
+
},
|
|
212
|
+
{
|
|
213
|
+
name: 'docImage',
|
|
214
|
+
level: 'inline',
|
|
215
|
+
start(src) {
|
|
216
|
+
const at = src.indexOf('![');
|
|
217
|
+
return at < 0 ? undefined : at;
|
|
218
|
+
},
|
|
219
|
+
tokenizer(src) {
|
|
220
|
+
const match = INLINE_IMAGE.exec(src);
|
|
221
|
+
return match ? imageToken(match, 'docImage') : undefined;
|
|
222
|
+
},
|
|
223
|
+
renderer(token) {
|
|
224
|
+
return imgTag(token, assetsBase);
|
|
225
|
+
},
|
|
226
|
+
},
|
|
227
|
+
],
|
|
228
|
+
renderer: {
|
|
229
|
+
heading({ tokens, depth, text }) {
|
|
230
|
+
// The id is GitHub's slug of the heading text, and the page's own
|
|
231
|
+
// slugger is what makes a repeated heading `-1`, `-2` — exactly as
|
|
232
|
+
// GitHub numbers them. The text shown in "On this page" and in
|
|
233
|
+
// search results is a different audience and gets the inline
|
|
234
|
+
// markdown stripped instead.
|
|
235
|
+
const id = slugger.slug(text);
|
|
236
|
+
headings.push({ id, text: stripInlineMarkdown(text), depth });
|
|
237
|
+
const inner = this.parser.parseInline(tokens);
|
|
238
|
+
return `<h${depth} id="${id}">${inner}</h${depth}>\n`;
|
|
239
|
+
},
|
|
240
|
+
link({ href, title, tokens }) {
|
|
241
|
+
const text = this.parser.parseInline(tokens);
|
|
242
|
+
const resolved = resolveDocLink(href, opts);
|
|
243
|
+
if (resolved) {
|
|
244
|
+
links.push({ href, slug: resolved.slug, anchor: resolved.anchor });
|
|
245
|
+
}
|
|
246
|
+
else {
|
|
247
|
+
// A bare `#anchor` is not rewritten — it is already the href a
|
|
248
|
+
// reader needs — but it still names a heading id, and a heading
|
|
249
|
+
// that got retitled is the same dead link as any other. Its
|
|
250
|
+
// target page is the one that wrote it.
|
|
251
|
+
const anchor = SELF_ANCHOR_HREF.exec(href)?.[1];
|
|
252
|
+
if (anchor)
|
|
253
|
+
links.push({ href, slug: pageSlug(opts), anchor });
|
|
254
|
+
}
|
|
255
|
+
const titleAttr = title ? ` title="${escapeHtml(title)}"` : '';
|
|
256
|
+
const away = !resolved && OFFSITE_HREF.test(href) ? OFFSITE_LINK_ATTRS : '';
|
|
257
|
+
return `<a href="${escapeHtml(resolved?.href ?? href)}"${titleAttr}${away}>${text}</a>`;
|
|
258
|
+
},
|
|
259
|
+
code(token) {
|
|
260
|
+
const { text, lang } = token;
|
|
261
|
+
const info = infoWords(lang);
|
|
262
|
+
// The graph was laid out before the parse; all that is left
|
|
263
|
+
// here is to drop it in where its fence stood.
|
|
264
|
+
if (info[0] === 'pipeline') {
|
|
265
|
+
const figure = token.docGraph;
|
|
266
|
+
return figure ? figure + '\n' : `<pre><code>${escapeHtml(text)}</code></pre>\n`;
|
|
267
|
+
}
|
|
268
|
+
const playground = info[0] === 'playground';
|
|
269
|
+
const language = (playground ? info[1] : info[0]) ?? (playground ? 'ts' : '');
|
|
270
|
+
const highlighted = language ? (opts.highlight?.(text, language) ?? null) : null;
|
|
271
|
+
const block = highlighted ?? `<pre><code>${escapeHtml(text)}</code></pre>`;
|
|
272
|
+
if (!playground)
|
|
273
|
+
return block + '\n';
|
|
274
|
+
// The fence's own text, carried beside the highlighted markup
|
|
275
|
+
// for the host to read back out of `textContent`. Shiki's
|
|
276
|
+
// output is spans and entities — recoverable in principle,
|
|
277
|
+
// wrong in practice the first time a fence contains something
|
|
278
|
+
// the highlighter styled — so the source travels as source.
|
|
279
|
+
//
|
|
280
|
+
// `type="text/plain"` is inert: nothing executes, and nothing
|
|
281
|
+
// in it is a tag. It is HTML-escaped, so the reader on the
|
|
282
|
+
// other side unescapes; the second replace is the guard that
|
|
283
|
+
// matters if `escapeHtml`'s policy ever narrows, because a
|
|
284
|
+
// literal `</script` in a fence body would otherwise close
|
|
285
|
+
// this element and spill the rest of the example into the page.
|
|
286
|
+
const source = escapeHtml(text).replace(/<\/script/gi, '<\\/script');
|
|
287
|
+
return (`<div class="doc-playground" data-playground data-lang="${escapeHtml(language)}">` +
|
|
288
|
+
`<script type="text/plain" class="doc-playground-source">${source}</script>` +
|
|
289
|
+
`${block}</div>\n`);
|
|
290
|
+
},
|
|
291
|
+
// A GFM table can run wider than a narrow screen; the wrapper is
|
|
292
|
+
// what the consumer hangs `overflow-x: auto` on. The table markup
|
|
293
|
+
// itself is exactly what marked's own default renderer produces.
|
|
294
|
+
table(token) {
|
|
295
|
+
let header = '';
|
|
296
|
+
let cell = '';
|
|
297
|
+
for (let j = 0; j < token.header.length; j++)
|
|
298
|
+
cell += this.tablecell(token.header[j]);
|
|
299
|
+
header += this.tablerow({ text: cell });
|
|
300
|
+
let body = '';
|
|
301
|
+
for (let j = 0; j < token.rows.length; j++) {
|
|
302
|
+
const row = token.rows[j];
|
|
303
|
+
cell = '';
|
|
304
|
+
for (let k = 0; k < row.length; k++)
|
|
305
|
+
cell += this.tablecell(row[k]);
|
|
306
|
+
body += this.tablerow({ text: cell });
|
|
307
|
+
}
|
|
308
|
+
if (body)
|
|
309
|
+
body = `<tbody>${body}</tbody>`;
|
|
310
|
+
return (`<div class="doc-table">\n<table>\n<thead>\n${header}</thead>\n${body}</table>\n` +
|
|
311
|
+
`</div>\n`);
|
|
312
|
+
},
|
|
313
|
+
},
|
|
314
|
+
});
|
|
315
|
+
const tokens = marked.lexer(markdown);
|
|
316
|
+
const imageTokens = [];
|
|
317
|
+
collectImages(tokens, imageTokens);
|
|
318
|
+
for (const token of imageTokens)
|
|
319
|
+
images.push(token.href);
|
|
320
|
+
if (opts.resolveImage) {
|
|
321
|
+
await Promise.all(imageTokens.map(async (token) => {
|
|
322
|
+
token.asset = await opts.resolveImage(token.href);
|
|
323
|
+
}));
|
|
324
|
+
}
|
|
325
|
+
const fenceTokens = [];
|
|
326
|
+
collectPipelineFences(tokens, fenceTokens);
|
|
327
|
+
for (const token of fenceTokens)
|
|
328
|
+
pipelines.push(pipelineIdOf(token.lang));
|
|
329
|
+
if (opts.resolvePipeline) {
|
|
330
|
+
await Promise.all(fenceTokens.map(async (token) => {
|
|
331
|
+
token.docGraph = await opts.resolvePipeline(pipelineIdOf(token.lang));
|
|
332
|
+
}));
|
|
333
|
+
}
|
|
334
|
+
const { previews, sectionTexts } = extractPreviews(tokens);
|
|
335
|
+
const body = marked.parser(tokens);
|
|
336
|
+
const banner = opts.banner
|
|
337
|
+
? `<aside class="doc-banner" role="note">${escapeHtml(opts.banner)}</aside>\n`
|
|
338
|
+
: '';
|
|
339
|
+
return {
|
|
340
|
+
html: banner + body,
|
|
341
|
+
headings,
|
|
342
|
+
previews,
|
|
343
|
+
sectionTexts,
|
|
344
|
+
links,
|
|
345
|
+
images,
|
|
346
|
+
pipelines,
|
|
347
|
+
title: headings.find((h) => h.depth === 1)?.text,
|
|
348
|
+
description: firstParagraph(markdown),
|
|
349
|
+
};
|
|
350
|
+
}
|
|
351
|
+
/** Containers a heading can legitimately hide inside. */
|
|
352
|
+
const DESCENDS_INTO = new Set(['blockquote', 'docAdmonition']);
|
|
353
|
+
/** Tokens that contribute nothing a reader would want to see in a search hit. */
|
|
354
|
+
const PREVIEW_SKIPS = new Set(['space', 'hr', 'code', 'docFigure']);
|
|
355
|
+
/** A fence's info string, as words: ` ```pipeline core:spec/respond ` → two. */
|
|
356
|
+
function infoWords(lang) {
|
|
357
|
+
return String(lang ?? '')
|
|
358
|
+
.trim()
|
|
359
|
+
.split(/\s+/)
|
|
360
|
+
.filter(Boolean);
|
|
361
|
+
}
|
|
362
|
+
/** The id a `pipeline` fence names — '' when it names none. */
|
|
363
|
+
function pipelineIdOf(lang) {
|
|
364
|
+
return infoWords(lang)[1] ?? '';
|
|
365
|
+
}
|
|
366
|
+
/**
|
|
367
|
+
* Every `pipeline` fence, in document order. A fence can sit inside a list
|
|
368
|
+
* item or a blockquote, so this walks the tree rather than the top level —
|
|
369
|
+
* the same reach `collectImages` has, for the same reason.
|
|
370
|
+
*/
|
|
371
|
+
function collectPipelineFences(tokens, out) {
|
|
372
|
+
for (const token of tokens) {
|
|
373
|
+
if (!token)
|
|
374
|
+
continue;
|
|
375
|
+
if (token.type === 'code' && infoWords(token.lang)[0] === 'pipeline')
|
|
376
|
+
out.push(token);
|
|
377
|
+
if (Array.isArray(token.tokens))
|
|
378
|
+
collectPipelineFences(token.tokens, out);
|
|
379
|
+
if (Array.isArray(token.items))
|
|
380
|
+
collectPipelineFences(token.items, out);
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
function collectImages(tokens, out) {
|
|
384
|
+
for (const token of tokens) {
|
|
385
|
+
if (!token)
|
|
386
|
+
continue;
|
|
387
|
+
if (token.type === 'docFigure' || token.type === 'docImage')
|
|
388
|
+
out.push(token);
|
|
389
|
+
if (Array.isArray(token.tokens))
|
|
390
|
+
collectImages(token.tokens, out);
|
|
391
|
+
if (Array.isArray(token.items))
|
|
392
|
+
collectImages(token.items, out);
|
|
393
|
+
if (Array.isArray(token.header))
|
|
394
|
+
collectImages(token.header, out);
|
|
395
|
+
if (Array.isArray(token.rows))
|
|
396
|
+
for (const row of token.rows)
|
|
397
|
+
collectImages(row, out);
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
/**
|
|
401
|
+
* The body under each heading, ≤180 chars, keyed by the id that heading will
|
|
402
|
+
* get. Read off the same token tree the HTML is rendered from, so a `#` inside
|
|
403
|
+
* a fenced block cannot invent a section the page does not have — which is the
|
|
404
|
+
* failure the app's line-regex version has.
|
|
405
|
+
*/
|
|
406
|
+
/** How much of a section's body the search index carries. */
|
|
407
|
+
const SECTION_TEXT_CHARS = 1200;
|
|
408
|
+
function extractPreviews(tokens) {
|
|
409
|
+
const flat = [];
|
|
410
|
+
const walk = (list) => {
|
|
411
|
+
for (const token of list) {
|
|
412
|
+
if (!token)
|
|
413
|
+
continue;
|
|
414
|
+
if (token.type === 'heading') {
|
|
415
|
+
flat.push({ heading: true, text: token.text });
|
|
416
|
+
}
|
|
417
|
+
else if (DESCENDS_INTO.has(token.type) && Array.isArray(token.tokens)) {
|
|
418
|
+
walk(token.tokens);
|
|
419
|
+
}
|
|
420
|
+
else if (PREVIEW_SKIPS.has(token.type)) {
|
|
421
|
+
// A fence, a rule and a figure all read as noise in a search
|
|
422
|
+
// result — and a figure's raw is the image markup itself.
|
|
423
|
+
continue;
|
|
424
|
+
}
|
|
425
|
+
else {
|
|
426
|
+
flat.push({ heading: false, raw: token.raw ?? '' });
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
};
|
|
430
|
+
walk(tokens);
|
|
431
|
+
const previews = {};
|
|
432
|
+
const sectionTexts = {};
|
|
433
|
+
// Its own slugger, walking the same headings in the same order as the
|
|
434
|
+
// renderer's — so a repeat is numbered identically and the previews stay
|
|
435
|
+
// keyed by the ids the page actually emits.
|
|
436
|
+
const slugger = new GithubSlugger();
|
|
437
|
+
for (let i = 0; i < flat.length; i++) {
|
|
438
|
+
const entry = flat[i];
|
|
439
|
+
if (!entry.heading)
|
|
440
|
+
continue;
|
|
441
|
+
const id = slugger.slug(entry.text);
|
|
442
|
+
const body = [];
|
|
443
|
+
for (let j = i + 1; j < flat.length && !flat[j].heading; j++) {
|
|
444
|
+
body.push(flat[j].raw);
|
|
445
|
+
if (stripInlineMarkdown(body.join('\n')).length >= SECTION_TEXT_CHARS)
|
|
446
|
+
break;
|
|
447
|
+
}
|
|
448
|
+
const text = stripInlineMarkdown(body.join('\n'));
|
|
449
|
+
previews[id] = truncateAtWord(text, 180);
|
|
450
|
+
sectionTexts[id] = truncateAtWord(text, SECTION_TEXT_CHARS);
|
|
451
|
+
}
|
|
452
|
+
return { previews, sectionTexts };
|
|
453
|
+
}
|
|
454
|
+
/** The app's description rule: first paragraph after the H1, ≤200 chars. */
|
|
455
|
+
function firstParagraph(markdown) {
|
|
456
|
+
const h1 = markdown.match(/^#\s+(.+)$/m);
|
|
457
|
+
const rest = h1 ? markdown.slice(markdown.indexOf(h1[0]) + h1[0].length) : markdown;
|
|
458
|
+
const para = rest.match(/^\s*\n+([^\n#][^\n]*(?:\n[^\n#][^\n]*)*)/m);
|
|
459
|
+
return truncateAtWord(stripInlineMarkdown(para?.[1] ?? ''), 200);
|
|
460
|
+
}
|
|
461
|
+
/** The title rule: first H1 in the source, else the slug. */
|
|
462
|
+
export function docTitle(markdown, slug) {
|
|
463
|
+
return markdown.match(/^#\s+(.+)$/m)?.[1]?.trim() ?? slug;
|
|
464
|
+
}
|
|
465
|
+
//# sourceMappingURL=dialect.js.map
|
package/dist/escape.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One function, and its own module for one reason: `graph.ts` needs it and
|
|
3
|
+
* nothing else from the dialect.
|
|
4
|
+
*
|
|
5
|
+
* `renderDocsGraph` runs in a browser — the docs playground draws the document
|
|
6
|
+
* it just ran with it — and `dialect.ts` imports `node:path` and `marked`, so a
|
|
7
|
+
* bundler following one import to reach `escapeHtml` pulls the whole markdown
|
|
8
|
+
* compiler in behind it and then fails on `node:path`. Splitting the leaf out
|
|
9
|
+
* is what keeps the drawing importable anywhere the drawing makes sense.
|
|
10
|
+
*
|
|
11
|
+
* `dialect.ts` re-exports it, so nothing about the package's surface moves.
|
|
12
|
+
*/
|
|
13
|
+
export declare function escapeHtml(value: string): string;
|
|
14
|
+
//# sourceMappingURL=escape.d.ts.map
|
package/dist/escape.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One function, and its own module for one reason: `graph.ts` needs it and
|
|
3
|
+
* nothing else from the dialect.
|
|
4
|
+
*
|
|
5
|
+
* `renderDocsGraph` runs in a browser — the docs playground draws the document
|
|
6
|
+
* it just ran with it — and `dialect.ts` imports `node:path` and `marked`, so a
|
|
7
|
+
* bundler following one import to reach `escapeHtml` pulls the whole markdown
|
|
8
|
+
* compiler in behind it and then fails on `node:path`. Splitting the leaf out
|
|
9
|
+
* is what keeps the drawing importable anywhere the drawing makes sense.
|
|
10
|
+
*
|
|
11
|
+
* `dialect.ts` re-exports it, so nothing about the package's surface moves.
|
|
12
|
+
*/
|
|
13
|
+
export function escapeHtml(value) {
|
|
14
|
+
return value
|
|
15
|
+
.replace(/&/g, '&')
|
|
16
|
+
.replace(/</g, '<')
|
|
17
|
+
.replace(/>/g, '>')
|
|
18
|
+
.replace(/"/g, '"')
|
|
19
|
+
.replace(/'/g, ''');
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=escape.js.map
|
package/dist/graph.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A graph, drawn: ELK's layered layout emitted as inline SVG.
|
|
3
|
+
*
|
|
4
|
+
* The geometry is deliberately the app's. The same documents are laid out in
|
|
5
|
+
* the pipeline workspace's map (`flow/layout.ts`) with layered ELK, 220×58
|
|
6
|
+
* cards and the same three spacings, so a reader who has seen the map reads
|
|
7
|
+
* the same shape here rather than a second, differently-proportioned picture
|
|
8
|
+
* of the same pipeline. What this does NOT copy is the app's compound blocks:
|
|
9
|
+
* a `DocsGraph` is flat by construction, because a printed page has no
|
|
10
|
+
* collapsing and a nested frame that cannot be opened is just a smaller box.
|
|
11
|
+
*
|
|
12
|
+
* Nothing here emits a colour. Every stroke and fill is `currentColor` or
|
|
13
|
+
* `none`, and the classes — `doc-graph-node-{kind}` above all — are the whole
|
|
14
|
+
* of the styling surface, because the app and the standalone site theme these
|
|
15
|
+
* pages differently and an SVG with `#4459c9` baked into it is a drawing that
|
|
16
|
+
* is wrong in one of them.
|
|
17
|
+
*
|
|
18
|
+
* elkjs is dynamic-imported: it is ~1.4MB of transpiled Java, and a compile
|
|
19
|
+
* with no `pipeline` fence in it should never pay for that.
|
|
20
|
+
*/
|
|
21
|
+
import type { DocsGraph } from './types.js';
|
|
22
|
+
/**
|
|
23
|
+
* One `DocsGraph` → one `<figure class="doc-graph">` holding an inline SVG.
|
|
24
|
+
*
|
|
25
|
+
* Every id the SVG defines is prefixed with a slug of the graph's id, so two
|
|
26
|
+
* graphs on one page do not share a marker or an `aria-labelledby` target. A
|
|
27
|
+
* page embedding the SAME graph twice would, which is why the fence's id is
|
|
28
|
+
* the thing that has to be unique on a page rather than merely present.
|
|
29
|
+
*/
|
|
30
|
+
export declare function renderDocsGraph(graph: DocsGraph): Promise<string>;
|
|
31
|
+
//# sourceMappingURL=graph.d.ts.map
|
package/dist/graph.js
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { escapeHtml } from './escape.js';
|
|
2
|
+
/** The app's step card, to the pixel (`STEP_W` / `STEP_H`). */
|
|
3
|
+
const NODE_W = 220;
|
|
4
|
+
const NODE_H = 58;
|
|
5
|
+
/** The app's `scopeOptions`, minus the direction it fills in per call. */
|
|
6
|
+
const SPACING = {
|
|
7
|
+
'elk.spacing.nodeNode': '26',
|
|
8
|
+
'elk.layered.spacing.nodeNodeBetweenLayers': '36',
|
|
9
|
+
'elk.spacing.edgeNode': '16',
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Characters a label keeps. At the emitted 13px this is about 196px of text —
|
|
13
|
+
* the card's 220 less its 12px gutters — so a label that fits is never cut and
|
|
14
|
+
* one that is cut never overruns its box.
|
|
15
|
+
*/
|
|
16
|
+
const LABEL_MAX = 28;
|
|
17
|
+
let elkModule = null;
|
|
18
|
+
async function loadElk() {
|
|
19
|
+
elkModule ??= import('elkjs/lib/elk.bundled.js').then((mod) => mod.default);
|
|
20
|
+
return elkModule;
|
|
21
|
+
}
|
|
22
|
+
/** Ids inside an SVG are document-wide, so every one of ours carries the graph's. */
|
|
23
|
+
function slugifyId(id) {
|
|
24
|
+
return (id
|
|
25
|
+
.toLowerCase()
|
|
26
|
+
.replace(/[^a-z0-9]+/g, '-')
|
|
27
|
+
.replace(/^-+|-+$/g, '') || 'graph');
|
|
28
|
+
}
|
|
29
|
+
const round = (value) => Math.round((value ?? 0) * 10) / 10;
|
|
30
|
+
function truncate(text) {
|
|
31
|
+
return text.length > LABEL_MAX ? `${text.slice(0, LABEL_MAX - 1).trimEnd()}…` : text;
|
|
32
|
+
}
|
|
33
|
+
/** `M x,y L x,y …` over one ELK edge section. */
|
|
34
|
+
function sectionPath(section) {
|
|
35
|
+
const points = [section.startPoint, ...(section.bendPoints ?? []), section.endPoint];
|
|
36
|
+
return points
|
|
37
|
+
.map((point, index) => `${index === 0 ? 'M' : 'L'}${round(point?.x)},${round(point?.y)}`)
|
|
38
|
+
.join(' ');
|
|
39
|
+
}
|
|
40
|
+
function nodeSvg(node, x, y) {
|
|
41
|
+
const label = truncate(node.label);
|
|
42
|
+
const sublabel = node.sublabel ? truncate(node.sublabel) : '';
|
|
43
|
+
// The tooltip is the fallback for what the cut took, so it is only worth a
|
|
44
|
+
// node when something was actually cut.
|
|
45
|
+
const cut = label !== node.label || (!!node.sublabel && sublabel !== node.sublabel);
|
|
46
|
+
const full = node.sublabel ? `${node.label} — ${node.sublabel}` : node.label;
|
|
47
|
+
const kind = slugifyId(node.kind);
|
|
48
|
+
return (`<g class="doc-graph-node doc-graph-node-${escapeHtml(kind)}" transform="translate(${x},${y})">` +
|
|
49
|
+
(cut ? `<title>${escapeHtml(full)}</title>` : '') +
|
|
50
|
+
`<rect width="${NODE_W}" height="${NODE_H}" rx="8" fill="none" stroke="currentColor"/>` +
|
|
51
|
+
`<text x="12" y="${sublabel ? 25 : 34}" font-size="13" fill="currentColor">` +
|
|
52
|
+
`${escapeHtml(label)}</text>` +
|
|
53
|
+
(sublabel
|
|
54
|
+
? `<text class="doc-graph-sublabel" x="12" y="42" font-size="11" ` +
|
|
55
|
+
`fill="currentColor">${escapeHtml(sublabel)}</text>`
|
|
56
|
+
: '') +
|
|
57
|
+
`</g>`);
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* One `DocsGraph` → one `<figure class="doc-graph">` holding an inline SVG.
|
|
61
|
+
*
|
|
62
|
+
* Every id the SVG defines is prefixed with a slug of the graph's id, so two
|
|
63
|
+
* graphs on one page do not share a marker or an `aria-labelledby` target. A
|
|
64
|
+
* page embedding the SAME graph twice would, which is why the fence's id is
|
|
65
|
+
* the thing that has to be unique on a page rather than merely present.
|
|
66
|
+
*/
|
|
67
|
+
export async function renderDocsGraph(graph) {
|
|
68
|
+
const uid = slugifyId(graph.id);
|
|
69
|
+
const title = graph.title ?? graph.id;
|
|
70
|
+
const byKey = new Map(graph.nodes.map((node) => [node.key, node]));
|
|
71
|
+
// An edge to a key no node has would make ELK throw about an id it cannot
|
|
72
|
+
// resolve, from inside a layout the reader never asked for. The resolver
|
|
73
|
+
// owns what a graph means; the drawing just refuses to draw a half-edge.
|
|
74
|
+
const edges = graph.edges.filter((edge) => byKey.has(edge.from) && byKey.has(edge.to));
|
|
75
|
+
const Elk = await loadElk();
|
|
76
|
+
const laidOut = await new Elk().layout({
|
|
77
|
+
id: `${uid}-root`,
|
|
78
|
+
layoutOptions: {
|
|
79
|
+
'elk.algorithm': 'layered',
|
|
80
|
+
'elk.direction': graph.direction ?? 'DOWN',
|
|
81
|
+
...SPACING,
|
|
82
|
+
'elk.padding': '[top=8,left=8,bottom=8,right=8]',
|
|
83
|
+
},
|
|
84
|
+
children: graph.nodes.map((node) => ({
|
|
85
|
+
id: node.key,
|
|
86
|
+
width: NODE_W,
|
|
87
|
+
height: NODE_H,
|
|
88
|
+
})),
|
|
89
|
+
edges: edges.map((edge, index) => ({
|
|
90
|
+
id: `${uid}-e${index}`,
|
|
91
|
+
sources: [edge.from],
|
|
92
|
+
targets: [edge.to],
|
|
93
|
+
})),
|
|
94
|
+
});
|
|
95
|
+
const width = round(laidOut.width);
|
|
96
|
+
const height = round(laidOut.height);
|
|
97
|
+
const labelByEdgeId = new Map(edges.map((edge, index) => [`${uid}-e${index}`, edge.label]));
|
|
98
|
+
const parts = [];
|
|
99
|
+
parts.push(`<figure class="doc-graph"><svg role="img" aria-labelledby="${uid}-title" ` +
|
|
100
|
+
`viewBox="0 0 ${width} ${height}" width="${width}" height="${height}">` +
|
|
101
|
+
`<title id="${uid}-title">${escapeHtml(title)}</title>`);
|
|
102
|
+
parts.push(`<defs><marker id="${uid}-arrow" viewBox="0 0 8 8" refX="7" refY="4" ` +
|
|
103
|
+
`markerWidth="6" markerHeight="6" orient="auto-start-reverse">` +
|
|
104
|
+
`<path d="M0,0 L8,4 L0,8 z" fill="currentColor"/></marker></defs>`);
|
|
105
|
+
// Edges first: a card drawn over a line is a card, a line drawn over a
|
|
106
|
+
// card is a line through it.
|
|
107
|
+
for (const edge of (laidOut.edges ?? [])) {
|
|
108
|
+
for (const section of (edge.sections ?? [])) {
|
|
109
|
+
parts.push(`<path class="doc-graph-edge" d="${sectionPath(section)}" fill="none" ` +
|
|
110
|
+
`stroke="currentColor" marker-end="url(#${uid}-arrow)"/>`);
|
|
111
|
+
}
|
|
112
|
+
const label = labelByEdgeId.get(edge.id);
|
|
113
|
+
const first = (edge.sections ?? [])[0];
|
|
114
|
+
if (label && first) {
|
|
115
|
+
const mid = (first.bendPoints ?? [])[0] ?? {
|
|
116
|
+
x: ((first.startPoint?.x ?? 0) + (first.endPoint?.x ?? 0)) / 2,
|
|
117
|
+
y: ((first.startPoint?.y ?? 0) + (first.endPoint?.y ?? 0)) / 2,
|
|
118
|
+
};
|
|
119
|
+
parts.push(`<text class="doc-graph-edge-label" x="${round(mid.x)}" y="${round(mid.y)}" ` +
|
|
120
|
+
`font-size="11" fill="currentColor">${escapeHtml(label)}</text>`);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
for (const child of (laidOut.children ?? [])) {
|
|
124
|
+
const node = byKey.get(child.id);
|
|
125
|
+
if (!node)
|
|
126
|
+
continue;
|
|
127
|
+
parts.push(nodeSvg(node, round(child.x), round(child.y)));
|
|
128
|
+
}
|
|
129
|
+
parts.push(`</svg><figcaption>${escapeHtml(title)}</figcaption></figure>`);
|
|
130
|
+
return parts.join('');
|
|
131
|
+
}
|
|
132
|
+
//# sourceMappingURL=graph.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export interface DocsHighlighter {
|
|
2
|
+
/** The highlighted block, or null when the grammar was not loaded. */
|
|
3
|
+
render(code: string, lang: string): string | null;
|
|
4
|
+
dispose(): void;
|
|
5
|
+
}
|
|
6
|
+
export declare function createDocsHighlighter(languages: string[]): Promise<DocsHighlighter>;
|
|
7
|
+
//# sourceMappingURL=highlight.d.ts.map
|