@clarusjs/content 0.7.0 → 0.9.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/dist/highlight.d.ts +1 -1
- package/dist/highlight.js +38 -11
- package/dist/index.d.ts +24 -3
- package/dist/index.js +51 -6
- package/package.json +6 -6
package/dist/highlight.d.ts
CHANGED
|
@@ -4,5 +4,5 @@ import { type LanguageRegistration } from 'shiki/core';
|
|
|
4
4
|
* of your own. A language already loaded is left as is.
|
|
5
5
|
*/
|
|
6
6
|
export declare function addLanguages(langs: (string | LanguageRegistration | LanguageRegistration[])[]): void;
|
|
7
|
-
/** Highlighted <pre> for `code` in `lang` (
|
|
7
|
+
/** Highlighted <pre> for `code` in `lang` (any language Shiki has, loaded on first use; others are plain text); `meta` carries `{1,3-5}`. */
|
|
8
8
|
export declare function highlight(code: string, lang: string, meta: string): string;
|
package/dist/highlight.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { createRequire } from 'node:module';
|
|
2
2
|
import { createHighlighterCoreSync } from 'shiki/core';
|
|
3
3
|
import { createJavaScriptRegexEngine } from 'shiki/engine/javascript';
|
|
4
|
+
import { bundledLanguagesInfo } from 'shiki/langs';
|
|
4
5
|
import { transformerMetaHighlight, transformerNotationDiff, transformerNotationErrorLevel, transformerNotationFocus, transformerNotationHighlight, } from '@shikijs/transformers';
|
|
5
6
|
import angularHtml from 'shiki/langs/angular-html.mjs';
|
|
6
7
|
import bash from 'shiki/langs/bash.mjs';
|
|
@@ -17,7 +18,8 @@ import yaml from 'shiki/langs/yaml.mjs';
|
|
|
17
18
|
import zig from 'shiki/langs/zig.mjs';
|
|
18
19
|
import githubDark from 'shiki/themes/github-dark-default.mjs';
|
|
19
20
|
import githubLight from 'shiki/themes/github-light-default.mjs';
|
|
20
|
-
const ALIASES = { tscl: 'angular-html', ts: 'typescript', js: 'javascript', sh: 'bash', shell: 'bash', zsh: 'bash', md: 'markdown', yml: 'yaml'
|
|
21
|
+
const ALIASES = { tscl: 'angular-html', ts: 'typescript', js: 'javascript', sh: 'bash', shell: 'bash', zsh: 'bash', md: 'markdown', yml: 'yaml',
|
|
22
|
+
h: 'c', hh: 'cpp', hpp: 'cpp', hxx: 'cpp', cc: 'cpp', cxx: 'cpp' };
|
|
21
23
|
const light = JSON.parse(JSON.stringify(githubLight).replace(/#6e7781/gi, '#59636e'));
|
|
22
24
|
let highlighter;
|
|
23
25
|
const get = () => (highlighter ??= createHighlighterCoreSync({
|
|
@@ -25,6 +27,7 @@ const get = () => (highlighter ??= createHighlighterCoreSync({
|
|
|
25
27
|
langs: [angularHtml, bash, css, diff, html, javascript, json, markdown, toml, tsx, typescript, yaml, zig],
|
|
26
28
|
engine: createJavaScriptRegexEngine(),
|
|
27
29
|
}));
|
|
30
|
+
const done = new Map();
|
|
28
31
|
const load = createRequire(import.meta.url);
|
|
29
32
|
function grammar(name) {
|
|
30
33
|
const fail = () => { throw new Error(`@clarusjs/content: Shiki has no grammar "${name}"`); };
|
|
@@ -41,8 +44,27 @@ export function addLanguages(langs) {
|
|
|
41
44
|
const hl = get();
|
|
42
45
|
const loaded = new Set(hl.getLoadedLanguages());
|
|
43
46
|
const add = langs.flatMap((l) => (typeof l === 'string' ? (loaded.has(l) ? [] : grammar(l)) : l)).filter((l) => !loaded.has(l.name));
|
|
44
|
-
if (add.length)
|
|
47
|
+
if (add.length) {
|
|
45
48
|
hl.loadLanguageSync(add);
|
|
49
|
+
done.clear();
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
const SHIKI = new Map(bundledLanguagesInfo.flatMap((l) => [l.id, ...(l.aliases ?? [])].map((name) => [name, l.id])));
|
|
53
|
+
const unavailable = new Set();
|
|
54
|
+
function language(hl, id) {
|
|
55
|
+
if (hl.getLoadedLanguages().includes(id))
|
|
56
|
+
return id;
|
|
57
|
+
const shiki = SHIKI.get(id.toLowerCase());
|
|
58
|
+
if (!shiki || unavailable.has(shiki))
|
|
59
|
+
return undefined;
|
|
60
|
+
try {
|
|
61
|
+
addLanguages([shiki]);
|
|
62
|
+
return shiki;
|
|
63
|
+
}
|
|
64
|
+
catch {
|
|
65
|
+
unavailable.add(shiki);
|
|
66
|
+
return undefined;
|
|
67
|
+
}
|
|
46
68
|
}
|
|
47
69
|
const transformers = [
|
|
48
70
|
transformerNotationDiff(),
|
|
@@ -53,13 +75,18 @@ const transformers = [
|
|
|
53
75
|
];
|
|
54
76
|
export function highlight(code, lang, meta) {
|
|
55
77
|
const hl = get();
|
|
56
|
-
const
|
|
57
|
-
const
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
78
|
+
const resolved = language(hl, Object.hasOwn(ALIASES, lang) ? ALIASES[lang] : lang) ?? 'text';
|
|
79
|
+
const key = `${resolved}\0${meta}\0${code}`;
|
|
80
|
+
let html = done.get(key);
|
|
81
|
+
if (html === undefined) {
|
|
82
|
+
html = hl.codeToHtml(code, {
|
|
83
|
+
lang: resolved,
|
|
84
|
+
themes: { light: 'github-light-default', dark: 'github-dark-default' },
|
|
85
|
+
defaultColor: false,
|
|
86
|
+
meta: { __raw: meta },
|
|
87
|
+
transformers,
|
|
88
|
+
});
|
|
89
|
+
done.set(key, html);
|
|
90
|
+
}
|
|
91
|
+
return html;
|
|
65
92
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -25,6 +25,26 @@ export type MarkdownOptions = {
|
|
|
25
25
|
tag: string;
|
|
26
26
|
from: string;
|
|
27
27
|
};
|
|
28
|
+
/**
|
|
29
|
+
* Render ```mermaid blocks as diagrams: each becomes its source in `<pre class="mermaid">` (not highlighted) with your
|
|
30
|
+
* diagram component after it, `<tag attach="visible" />`, which draws it in the browser. theme-docs ships one:
|
|
31
|
+
* `{ tag: 'diagram', from: '@clarusjs/theme-docs/mermaid.tscl' }` (install `mermaid` beside it). Without this option
|
|
32
|
+
* a ```mermaid block is code, as any other.
|
|
33
|
+
*/
|
|
34
|
+
mermaid?: {
|
|
35
|
+
tag: string;
|
|
36
|
+
from: string;
|
|
37
|
+
};
|
|
38
|
+
/**
|
|
39
|
+
* Synced tabs: `::: code-group sdk` names a group, and groups with the same name switch together (tabs matched by
|
|
40
|
+
* their label). Your component goes in each named group, `<tag />`, and does the syncing in the browser; theme-docs
|
|
41
|
+
* ships one: `{ tag: 'codeTabs', from: '@clarusjs/theme-docs/code-tabs.tscl' }`. Without JavaScript, or without this
|
|
42
|
+
* option, each group works on its own.
|
|
43
|
+
*/
|
|
44
|
+
codeGroups?: {
|
|
45
|
+
tag: string;
|
|
46
|
+
from: string;
|
|
47
|
+
};
|
|
28
48
|
/**
|
|
29
49
|
* Render `::: quiz` blocks through your quiz component (an island): `<tag [name] [question] [options] [answer]>`
|
|
30
50
|
* with the explanation as its content. Without it they are static HTML that works without JavaScript.
|
|
@@ -41,9 +61,10 @@ export type MarkdownOptions = {
|
|
|
41
61
|
*/
|
|
42
62
|
lineNumbers?: boolean;
|
|
43
63
|
/**
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
64
|
+
* Every language Shiki has is highlighted already, loaded the first time a page uses it (by name or alias:
|
|
65
|
+
* ```` ```rust ````, ```` ```py ````), from this package's own Shiki (nothing to install). Add grammars of your own
|
|
66
|
+
* here, or names of Shiki's (`markdown({ langs: ['rust'] })`) to load them up front, where a typo is a build error.
|
|
67
|
+
* A language Shiki doesn't have is plain text.
|
|
47
68
|
* @see https://clarusjs.com/guide/markdown#code
|
|
48
69
|
*/
|
|
49
70
|
langs?: (string | LanguageRegistration | LanguageRegistration[])[];
|
package/dist/index.js
CHANGED
|
@@ -13,6 +13,14 @@ function globToRegex(glob) {
|
|
|
13
13
|
.replace(/\0A/g, '(?:.*/)?').replace(/\0B/g, '(?:/.*)?').replace(/\0C/g, '.*');
|
|
14
14
|
return new RegExp(`^${pattern}$`);
|
|
15
15
|
}
|
|
16
|
+
function frontmatterJson(data, rel) {
|
|
17
|
+
try {
|
|
18
|
+
return JSON.stringify(data, (_, v) => (typeof v === 'bigint' ? String(v) : v));
|
|
19
|
+
}
|
|
20
|
+
catch (error) {
|
|
21
|
+
throw new Error(`${rel}: frontmatter can't be passed to the layout as JSON: ${error.message}`);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
16
24
|
function validateFrontmatter(data, schema, file, root) {
|
|
17
25
|
if (!schema)
|
|
18
26
|
return data;
|
|
@@ -71,7 +79,11 @@ function renderer(options) {
|
|
|
71
79
|
}
|
|
72
80
|
const open = state.push(m[2] === 'code-group' ? 'cl_group_open' : 'cl_container_open', 'div', 1);
|
|
73
81
|
open.info = m[2];
|
|
74
|
-
|
|
82
|
+
const text = (m[3] ?? '').trim();
|
|
83
|
+
const flag = m[2] === 'code-group' ? /(?:^|\s+):(scroll|wrap)(?=\s|$)/ : undefined;
|
|
84
|
+
const tabs = flag ? flag.exec(text)?.[1] : undefined;
|
|
85
|
+
const rest = flag ? text.replace(new RegExp(flag.source, 'g'), '').trim() : text;
|
|
86
|
+
open.meta = { title: rest || titles[m[2]], name: rest || undefined, ...(tabs ? { tabs } : {}) };
|
|
75
87
|
const oldMax = state.lineMax;
|
|
76
88
|
state.lineMax = close;
|
|
77
89
|
state.md.block.tokenize(state, startLine + 1, close);
|
|
@@ -179,14 +191,36 @@ function renderer(options) {
|
|
|
179
191
|
return `<p class="quiz-question" id="${name}-q">${html}</p><div class="quiz-options" role="radiogroup" aria-labelledby="${name}-q">${labels}</div>`
|
|
180
192
|
+ `<details class="quiz-answer"><summary>Show the answer</summary><p class="quiz-correct">Answer: ${escapeHtml(choices[answer])}</p>\n`;
|
|
181
193
|
};
|
|
182
|
-
md.renderer.rules.cl_group_open = (
|
|
194
|
+
md.renderer.rules.cl_group_open = (tokens, i, _o, env) => {
|
|
183
195
|
const e = env;
|
|
184
|
-
|
|
185
|
-
|
|
196
|
+
const { name: sync, tabs } = tokens[i].meta;
|
|
197
|
+
e.group = { id: e.groups++, n: 0, ...(sync ? { sync } : {}) };
|
|
198
|
+
let count = 0;
|
|
199
|
+
for (let j = i + 1, depth = 0; j < tokens.length; j++) {
|
|
200
|
+
const t = tokens[j];
|
|
201
|
+
if (t.type === 'cl_group_open' || t.type === 'cl_container_open')
|
|
202
|
+
depth++;
|
|
203
|
+
else if (t.type === 'cl_container_close')
|
|
204
|
+
depth--;
|
|
205
|
+
else if (t.type === 'cl_group_close') {
|
|
206
|
+
if (depth === 0)
|
|
207
|
+
break;
|
|
208
|
+
depth--;
|
|
209
|
+
}
|
|
210
|
+
else if (t.type === 'fence' && depth === 0)
|
|
211
|
+
count++;
|
|
212
|
+
}
|
|
213
|
+
return `<div class="code-group${tabs ? ` ${tabs}` : ''}" style="--tabs: ${count}"${sync ? ` data-sync="${inert(sync)}"` : ''}>`;
|
|
186
214
|
};
|
|
187
215
|
md.renderer.rules.cl_group_close = (_t, _i, _o, env) => {
|
|
188
|
-
|
|
189
|
-
|
|
216
|
+
const e = env;
|
|
217
|
+
let out = '';
|
|
218
|
+
if (e.group?.sync && options.codeGroups) {
|
|
219
|
+
e.tags.push(`<${options.codeGroups.tag} />`);
|
|
220
|
+
out = `<!--cl-tag-${e.tags.length - 1}-->`;
|
|
221
|
+
}
|
|
222
|
+
delete e.group;
|
|
223
|
+
return out + '</div>\n';
|
|
190
224
|
};
|
|
191
225
|
md.renderer.rules.table_open = (tokens, i, options, _env, self) => {
|
|
192
226
|
tokens[i].attrSet('tabindex', '0');
|
|
@@ -196,6 +230,10 @@ function renderer(options) {
|
|
|
196
230
|
const e = env;
|
|
197
231
|
const info = tokens[i].info.trim();
|
|
198
232
|
const lang = /^[^\s{[:]+/.exec(info)?.[0] ?? 'text';
|
|
233
|
+
if (options.mermaid && lang === 'mermaid' && !e.group) {
|
|
234
|
+
e.tags.push(`<${options.mermaid.tag} attach="visible" />`);
|
|
235
|
+
return `<div class="mermaid-diagram"><pre class="mermaid" tabindex="0">${inert(tokens[i].content.replace(/\n$/, ''))}</pre><!--cl-tag-${e.tags.length - 1}--></div>\n`;
|
|
236
|
+
}
|
|
199
237
|
const title = /\[([^\]]+)\]/.exec(info)?.[1];
|
|
200
238
|
const code = tokens[i].content;
|
|
201
239
|
const command = SHELL.has(lang.toLowerCase()) && !title && !code.startsWith('#!');
|
|
@@ -419,6 +457,12 @@ export default function markdown(options = {}) {
|
|
|
419
457
|
if (options.quiz && /^\s*:{3,}\s*quiz\b/m.test(body) && !imports.some((line) => new RegExp(`^@import\\s+${options.quiz.tag}\\s+from`).test(line))) {
|
|
420
458
|
imports.unshift(`@import ${options.quiz.tag} from '${options.quiz.from}'`);
|
|
421
459
|
}
|
|
460
|
+
if (options.codeGroups && /^\s*:{3,}\s*code-group[^\S\n]+\S/m.test(body) && !imports.some((line) => new RegExp(`^@import\\s+${options.codeGroups.tag}\\s+from`).test(line))) {
|
|
461
|
+
imports.unshift(`@import ${options.codeGroups.tag} from '${options.codeGroups.from}'`);
|
|
462
|
+
}
|
|
463
|
+
if (options.mermaid && /^\s*(`{3,}|~{3,})\s*mermaid\b/m.test(body) && !imports.some((line) => new RegExp(`^@import\\s+${options.mermaid.tag}\\s+from`).test(line))) {
|
|
464
|
+
imports.unshift(`@import ${options.mermaid.tag} from '${options.mermaid.from}'`);
|
|
465
|
+
}
|
|
422
466
|
if (options.copy && !imports.some((line) => new RegExp(`^@import\\s+${options.copy.tag}\\s+from`).test(line))) {
|
|
423
467
|
imports.unshift(`@import ${options.copy.tag} from '${options.copy.from}'`);
|
|
424
468
|
}
|
|
@@ -459,6 +503,7 @@ export default function markdown(options = {}) {
|
|
|
459
503
|
typeof data.description === 'string' ? `[description]="${quote(data.description)}"` : '',
|
|
460
504
|
`[path]="${quote(route)}"`,
|
|
461
505
|
headings.length ? `[headings]="[${headings.map((h) => `{ id: ${quote(h.id)}, text: ${quote(h.text)}, level: ${h.level} }`).join(', ')}]"` : '',
|
|
506
|
+
Object.keys(data).length ? `[...]="{ frontmatter: JSON.parse(${quote(frontmatterJson(data, relative(context.root, file).split(sep).join('/')))}) }"` : '',
|
|
462
507
|
].filter(Boolean).join(' ');
|
|
463
508
|
return [`@import cl-layout from '${layout}'`, ...imports, `<cl-layout ${props}>${html}</cl-layout>`, ''].join('\n');
|
|
464
509
|
},
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@clarusjs/content",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.0",
|
|
4
4
|
"description": "Markdown pages for clarusjs: frontmatter, layouts, components in Markdown, rendered to static HTML at build time.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
"access": "public"
|
|
20
20
|
},
|
|
21
21
|
"peerDependencies": {
|
|
22
|
-
"clarusjs": "^0.
|
|
22
|
+
"clarusjs": "^0.20.0"
|
|
23
23
|
},
|
|
24
24
|
"engines": {
|
|
25
25
|
"node": "^22.12.0 || ^24.0.0 || >=26.0.0"
|
|
@@ -49,11 +49,11 @@
|
|
|
49
49
|
},
|
|
50
50
|
"devDependencies": {
|
|
51
51
|
"@types/markdown-it": "^14.2.0",
|
|
52
|
-
"@types/node": "^26.6.
|
|
53
|
-
"clarusjs": "^0.
|
|
52
|
+
"@types/node": "^26.6.11",
|
|
53
|
+
"clarusjs": "^0.20.0",
|
|
54
54
|
"typescript": "^7.0.2",
|
|
55
|
-
"vite": "^8.
|
|
56
|
-
"vitest": "^5.1.
|
|
55
|
+
"vite": "^8.4.0",
|
|
56
|
+
"vitest": "^5.1.2"
|
|
57
57
|
},
|
|
58
58
|
"scripts": {
|
|
59
59
|
"build": "node ../../scripts/build-package.mjs",
|