@clarusjs/content 0.8.0 → 0.9.1

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.js CHANGED
@@ -18,7 +18,8 @@ import yaml from 'shiki/langs/yaml.mjs';
18
18
  import zig from 'shiki/langs/zig.mjs';
19
19
  import githubDark from 'shiki/themes/github-dark-default.mjs';
20
20
  import githubLight from 'shiki/themes/github-light-default.mjs';
21
- 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' };
22
23
  const light = JSON.parse(JSON.stringify(githubLight).replace(/#6e7781/gi, '#59636e'));
23
24
  let highlighter;
24
25
  const get = () => (highlighter ??= createHighlighterCoreSync({
@@ -26,6 +27,7 @@ const get = () => (highlighter ??= createHighlighterCoreSync({
26
27
  langs: [angularHtml, bash, css, diff, html, javascript, json, markdown, toml, tsx, typescript, yaml, zig],
27
28
  engine: createJavaScriptRegexEngine(),
28
29
  }));
30
+ const done = new Map();
29
31
  const load = createRequire(import.meta.url);
30
32
  function grammar(name) {
31
33
  const fail = () => { throw new Error(`@clarusjs/content: Shiki has no grammar "${name}"`); };
@@ -42,8 +44,10 @@ export function addLanguages(langs) {
42
44
  const hl = get();
43
45
  const loaded = new Set(hl.getLoadedLanguages());
44
46
  const add = langs.flatMap((l) => (typeof l === 'string' ? (loaded.has(l) ? [] : grammar(l)) : l)).filter((l) => !loaded.has(l.name));
45
- if (add.length)
47
+ if (add.length) {
46
48
  hl.loadLanguageSync(add);
49
+ done.clear();
50
+ }
47
51
  }
48
52
  const SHIKI = new Map(bundledLanguagesInfo.flatMap((l) => [l.id, ...(l.aliases ?? [])].map((name) => [name, l.id])));
49
53
  const unavailable = new Set();
@@ -71,11 +75,18 @@ const transformers = [
71
75
  ];
72
76
  export function highlight(code, lang, meta) {
73
77
  const hl = get();
74
- return hl.codeToHtml(code, {
75
- lang: language(hl, Object.hasOwn(ALIASES, lang) ? ALIASES[lang] : lang) ?? 'text',
76
- themes: { light: 'github-light-default', dark: 'github-dark-default' },
77
- defaultColor: false,
78
- meta: { __raw: meta },
79
- transformers,
80
- });
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;
81
92
  }
package/dist/index.d.ts CHANGED
@@ -57,7 +57,7 @@ export type MarkdownOptions = {
57
57
  * Number the lines of every code block except shell commands (sh, bash, shell, zsh, console, powershell, ps1): a
58
58
  * command is copied, not referred to by line. A shell script is numbered: a block with a file title
59
59
  * (```bash [deploy.sh]) or a `#!` first line. `:no-line-numbers` turns one block off, `:line-numbers` one on.
60
- * @see https://clarusjs.com/guide/markdown#code
60
+ * @see https://clarusjs.com/docs-sites/code-blocks#code
61
61
  */
62
62
  lineNumbers?: boolean;
63
63
  /**
@@ -65,7 +65,7 @@ export type MarkdownOptions = {
65
65
  * ```` ```rust ````, ```` ```py ````), from this package's own Shiki (nothing to install). Add grammars of your own
66
66
  * here, or names of Shiki's (`markdown({ langs: ['rust'] })`) to load them up front, where a typo is a build error.
67
67
  * A language Shiki doesn't have is plain text.
68
- * @see https://clarusjs.com/guide/markdown#code
68
+ * @see https://clarusjs.com/docs-sites/code-blocks#code
69
69
  */
70
70
  langs?: (string | LanguageRegistration | LanguageRegistration[])[];
71
71
  /** `::: quiz` is for lessons (frontmatter `steps: true`); 'everywhere' allows it on any page. */
@@ -75,7 +75,7 @@ export type MarkdownOptions = {
75
75
  * one per folder under src/pages (`{ 'guide/**': Guide, 'blog/*.md': Post }`, the first matching glob wins). A
76
76
  * page that doesn't match fails the build; the schema's output (defaults, coercions) is what the page gets.
77
77
  * Synchronous schemas only.
78
- * @see https://clarusjs.com/guide/markdown#frontmatter-schemas
78
+ * @see https://clarusjs.com/docs-sites/frontmatter
79
79
  */
80
80
  /**
81
81
  * Folders of components a page can use by tag without importing them: `'/src/components'` (root-relative), or
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
- open.meta = { title: m[3]?.trim() || titles[m[2]], name: m[3]?.trim() || undefined };
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);
@@ -181,9 +193,24 @@ function renderer(options) {
181
193
  };
182
194
  md.renderer.rules.cl_group_open = (tokens, i, _o, env) => {
183
195
  const e = env;
184
- const sync = tokens[i].meta.name;
196
+ const { name: sync, tabs } = tokens[i].meta;
185
197
  e.group = { id: e.groups++, n: 0, ...(sync ? { sync } : {}) };
186
- return `<div class="code-group"${sync ? ` data-sync="${inert(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)}"` : ''}>`;
187
214
  };
188
215
  md.renderer.rules.cl_group_close = (_t, _i, _o, env) => {
189
216
  const e = env;
@@ -205,7 +232,7 @@ function renderer(options) {
205
232
  const lang = /^[^\s{[:]+/.exec(info)?.[0] ?? 'text';
206
233
  if (options.mermaid && lang === 'mermaid' && !e.group) {
207
234
  e.tags.push(`<${options.mermaid.tag} attach="visible" />`);
208
- 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`;
235
+ return `<div class="mermaid-diagram"><!--cl-tag-${e.tags.length - 1}--><pre class="mermaid" tabindex="0">${inert(tokens[i].content.replace(/\n$/, ''))}</pre></div>\n`;
209
236
  }
210
237
  const title = /\[([^\]]+)\]/.exec(info)?.[1];
211
238
  const code = tokens[i].content;
@@ -476,6 +503,7 @@ export default function markdown(options = {}) {
476
503
  typeof data.description === 'string' ? `[description]="${quote(data.description)}"` : '',
477
504
  `[path]="${quote(route)}"`,
478
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('/')))}) }"` : '',
479
507
  ].filter(Boolean).join(' ');
480
508
  return [`@import cl-layout from '${layout}'`, ...imports, `<cl-layout ${props}>${html}</cl-layout>`, ''].join('\n');
481
509
  },
@@ -17,6 +17,6 @@ export declare function highlightBlocks(source: string, warn?: (message: string)
17
17
  *
18
18
  * export default defineConfig({ plugins: [highlightTemplates(), clarusjs({ sources: [markdown()] })] })
19
19
  * ```
20
- * @see https://clarusjs.com/guide/markdown#code-in-templates
20
+ * @see https://clarusjs.com/docs-sites/code-blocks#code-in-templates
21
21
  */
22
22
  export declare function highlightTemplates(): Plugin;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clarusjs/content",
3
- "version": "0.8.0",
3
+ "version": "0.9.1",
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.19.0"
22
+ "clarusjs": "^0.21.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.6",
53
- "clarusjs": "^0.19.0",
52
+ "@types/node": "^26.6.11",
53
+ "clarusjs": "^0.21.0",
54
54
  "typescript": "^7.0.2",
55
- "vite": "^8.3.8",
56
- "vitest": "^5.1.1"
55
+ "vite": "^8.4.0",
56
+ "vitest": "^5.1.2"
57
57
  },
58
58
  "scripts": {
59
59
  "build": "node ../../scripts/build-package.mjs",