@clarusjs/content 0.7.0 → 0.8.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.
@@ -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` (unknown languages are plain text); `meta` carries `{1,3-5}`. */
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';
@@ -44,6 +45,23 @@ export function addLanguages(langs) {
44
45
  if (add.length)
45
46
  hl.loadLanguageSync(add);
46
47
  }
48
+ const SHIKI = new Map(bundledLanguagesInfo.flatMap((l) => [l.id, ...(l.aliases ?? [])].map((name) => [name, l.id])));
49
+ const unavailable = new Set();
50
+ function language(hl, id) {
51
+ if (hl.getLoadedLanguages().includes(id))
52
+ return id;
53
+ const shiki = SHIKI.get(id.toLowerCase());
54
+ if (!shiki || unavailable.has(shiki))
55
+ return undefined;
56
+ try {
57
+ addLanguages([shiki]);
58
+ return shiki;
59
+ }
60
+ catch {
61
+ unavailable.add(shiki);
62
+ return undefined;
63
+ }
64
+ }
47
65
  const transformers = [
48
66
  transformerNotationDiff(),
49
67
  transformerNotationFocus(),
@@ -53,10 +71,8 @@ const transformers = [
53
71
  ];
54
72
  export function highlight(code, lang, meta) {
55
73
  const hl = get();
56
- const id = ALIASES[lang] ?? lang;
57
- const known = hl.getLoadedLanguages().includes(id);
58
74
  return hl.codeToHtml(code, {
59
- lang: known ? id : 'text',
75
+ lang: language(hl, Object.hasOwn(ALIASES, lang) ? ALIASES[lang] : lang) ?? 'text',
60
76
  themes: { light: 'github-light-default', dark: 'github-dark-default' },
61
77
  defaultColor: false,
62
78
  meta: { __raw: meta },
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
- * More languages to highlight, on top of the built-in ones (TypeScript, JavaScript, TSX, HTML, CSS, JSON, YAML, TOML,
45
- * Markdown, bash, diff, Zig, and tscl): names of Shiki's grammars, `markdown({ langs: ['rust', 'go'] })`, loaded from
46
- * this package's own Shiki (nothing to install), or grammars of your own. A language not loaded is plain text.
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
@@ -71,7 +71,7 @@ function renderer(options) {
71
71
  }
72
72
  const open = state.push(m[2] === 'code-group' ? 'cl_group_open' : 'cl_container_open', 'div', 1);
73
73
  open.info = m[2];
74
- open.meta = { title: m[3]?.trim() || titles[m[2]] };
74
+ open.meta = { title: m[3]?.trim() || titles[m[2]], name: m[3]?.trim() || undefined };
75
75
  const oldMax = state.lineMax;
76
76
  state.lineMax = close;
77
77
  state.md.block.tokenize(state, startLine + 1, close);
@@ -179,14 +179,21 @@ function renderer(options) {
179
179
  return `<p class="quiz-question" id="${name}-q">${html}</p><div class="quiz-options" role="radiogroup" aria-labelledby="${name}-q">${labels}</div>`
180
180
  + `<details class="quiz-answer"><summary>Show the answer</summary><p class="quiz-correct">Answer: ${escapeHtml(choices[answer])}</p>\n`;
181
181
  };
182
- md.renderer.rules.cl_group_open = (_t, _i, _o, env) => {
182
+ md.renderer.rules.cl_group_open = (tokens, i, _o, env) => {
183
183
  const e = env;
184
- e.group = { id: e.groups++, n: 0 };
185
- return '<div class="code-group">';
184
+ const sync = tokens[i].meta.name;
185
+ e.group = { id: e.groups++, n: 0, ...(sync ? { sync } : {}) };
186
+ return `<div class="code-group"${sync ? ` data-sync="${inert(sync)}"` : ''}>`;
186
187
  };
187
188
  md.renderer.rules.cl_group_close = (_t, _i, _o, env) => {
188
- delete env.group;
189
- return '</div>\n';
189
+ const e = env;
190
+ let out = '';
191
+ if (e.group?.sync && options.codeGroups) {
192
+ e.tags.push(`<${options.codeGroups.tag} />`);
193
+ out = `<!--cl-tag-${e.tags.length - 1}-->`;
194
+ }
195
+ delete e.group;
196
+ return out + '</div>\n';
190
197
  };
191
198
  md.renderer.rules.table_open = (tokens, i, options, _env, self) => {
192
199
  tokens[i].attrSet('tabindex', '0');
@@ -196,6 +203,10 @@ function renderer(options) {
196
203
  const e = env;
197
204
  const info = tokens[i].info.trim();
198
205
  const lang = /^[^\s{[:]+/.exec(info)?.[0] ?? 'text';
206
+ if (options.mermaid && lang === 'mermaid' && !e.group) {
207
+ 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`;
209
+ }
199
210
  const title = /\[([^\]]+)\]/.exec(info)?.[1];
200
211
  const code = tokens[i].content;
201
212
  const command = SHELL.has(lang.toLowerCase()) && !title && !code.startsWith('#!');
@@ -419,6 +430,12 @@ export default function markdown(options = {}) {
419
430
  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
431
  imports.unshift(`@import ${options.quiz.tag} from '${options.quiz.from}'`);
421
432
  }
433
+ 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))) {
434
+ imports.unshift(`@import ${options.codeGroups.tag} from '${options.codeGroups.from}'`);
435
+ }
436
+ 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))) {
437
+ imports.unshift(`@import ${options.mermaid.tag} from '${options.mermaid.from}'`);
438
+ }
422
439
  if (options.copy && !imports.some((line) => new RegExp(`^@import\\s+${options.copy.tag}\\s+from`).test(line))) {
423
440
  imports.unshift(`@import ${options.copy.tag} from '${options.copy.from}'`);
424
441
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clarusjs/content",
3
- "version": "0.7.0",
3
+ "version": "0.8.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.18.0"
22
+ "clarusjs": "^0.19.0"
23
23
  },
24
24
  "engines": {
25
25
  "node": "^22.12.0 || ^24.0.0 || >=26.0.0"
@@ -50,7 +50,7 @@
50
50
  "devDependencies": {
51
51
  "@types/markdown-it": "^14.2.0",
52
52
  "@types/node": "^26.6.6",
53
- "clarusjs": "^0.18.0",
53
+ "clarusjs": "^0.19.0",
54
54
  "typescript": "^7.0.2",
55
55
  "vite": "^8.3.8",
56
56
  "vitest": "^5.1.1"