@clarusjs/content 0.6.0 → 0.7.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.
@@ -1,2 +1,8 @@
1
+ import { type LanguageRegistration } from 'shiki/core';
2
+ /**
3
+ * More languages: names of Shiki's grammars (`'rust'`, `'go'`), loaded from this package's own Shiki, or grammars
4
+ * of your own. A language already loaded is left as is.
5
+ */
6
+ export declare function addLanguages(langs: (string | LanguageRegistration | LanguageRegistration[])[]): void;
1
7
  /** Highlighted <pre> for `code` in `lang` (unknown languages are plain text); `meta` carries `{1,3-5}`. */
2
8
  export declare function highlight(code: string, lang: string, meta: string): string;
package/dist/highlight.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { createRequire } from 'node:module';
1
2
  import { createHighlighterCoreSync } from 'shiki/core';
2
3
  import { createJavaScriptRegexEngine } from 'shiki/engine/javascript';
3
4
  import { transformerMetaHighlight, transformerNotationDiff, transformerNotationErrorLevel, transformerNotationFocus, transformerNotationHighlight, } from '@shikijs/transformers';
@@ -13,6 +14,7 @@ import toml from 'shiki/langs/toml.mjs';
13
14
  import tsx from 'shiki/langs/tsx.mjs';
14
15
  import typescript from 'shiki/langs/typescript.mjs';
15
16
  import yaml from 'shiki/langs/yaml.mjs';
17
+ import zig from 'shiki/langs/zig.mjs';
16
18
  import githubDark from 'shiki/themes/github-dark-default.mjs';
17
19
  import githubLight from 'shiki/themes/github-light-default.mjs';
18
20
  const ALIASES = { tscl: 'angular-html', ts: 'typescript', js: 'javascript', sh: 'bash', shell: 'bash', zsh: 'bash', md: 'markdown', yml: 'yaml' };
@@ -20,9 +22,28 @@ const light = JSON.parse(JSON.stringify(githubLight).replace(/#6e7781/gi, '#5963
20
22
  let highlighter;
21
23
  const get = () => (highlighter ??= createHighlighterCoreSync({
22
24
  themes: [light, githubDark],
23
- langs: [angularHtml, bash, css, diff, html, javascript, json, markdown, toml, tsx, typescript, yaml],
25
+ langs: [angularHtml, bash, css, diff, html, javascript, json, markdown, toml, tsx, typescript, yaml, zig],
24
26
  engine: createJavaScriptRegexEngine(),
25
27
  }));
28
+ const load = createRequire(import.meta.url);
29
+ function grammar(name) {
30
+ const fail = () => { throw new Error(`@clarusjs/content: Shiki has no grammar "${name}"`); };
31
+ if (!/^[a-z0-9][a-z0-9+#.-]*$/i.test(name) || name.includes('..'))
32
+ fail();
33
+ try {
34
+ return load(`shiki/langs/${name}.mjs`).default;
35
+ }
36
+ catch {
37
+ return fail();
38
+ }
39
+ }
40
+ export function addLanguages(langs) {
41
+ const hl = get();
42
+ const loaded = new Set(hl.getLoadedLanguages());
43
+ const add = langs.flatMap((l) => (typeof l === 'string' ? (loaded.has(l) ? [] : grammar(l)) : l)).filter((l) => !loaded.has(l.name));
44
+ if (add.length)
45
+ hl.loadLanguageSync(add);
46
+ }
26
47
  const transformers = [
27
48
  transformerNotationDiff(),
28
49
  transformerNotationFocus(),
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import type { LanguageRegistration } from 'shiki/core';
1
2
  import type { Source } from 'clarusjs/vite';
2
3
  /**
3
4
  * `markdown()`'s options: the layout, components for blocks, code copy and quizzes, and frontmatter schemas.
@@ -39,6 +40,13 @@ export type MarkdownOptions = {
39
40
  * @see https://clarusjs.com/guide/markdown#code
40
41
  */
41
42
  lineNumbers?: boolean;
43
+ /**
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.
47
+ * @see https://clarusjs.com/guide/markdown#code
48
+ */
49
+ langs?: (string | LanguageRegistration | LanguageRegistration[])[];
42
50
  /** `::: quiz` is for lessons (frontmatter `steps: true`); 'everywhere' allows it on any page. */
43
51
  quizzes?: 'lessons' | 'everywhere';
44
52
  /**
package/dist/index.js CHANGED
@@ -2,7 +2,7 @@ import { existsSync } from 'node:fs';
2
2
  import { dirname, relative, resolve, sep } from 'node:path';
3
3
  import MarkdownIt from 'markdown-it';
4
4
  import { parse as parseYaml } from 'yaml';
5
- import { highlight } from './highlight.js';
5
+ import { addLanguages, highlight } from './highlight.js';
6
6
  import { expand } from './snippets.js';
7
7
  const SHELL = new Set(['sh', 'bash', 'shell', 'zsh', 'console', 'powershell', 'ps1']);
8
8
  function globToRegex(glob) {
@@ -387,6 +387,8 @@ function protectTags(body, components, saved) {
387
387
  return { body: out, restore: (html) => html.replace(/<!--cl-tag-(\d+)-->/g, (_, i) => saved[Number(i)]) };
388
388
  }
389
389
  export default function markdown(options = {}) {
390
+ if (options.langs?.length)
391
+ addLanguages(options.langs);
390
392
  const md = renderer(options);
391
393
  return {
392
394
  extension: '.md',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clarusjs/content",
3
- "version": "0.6.0",
3
+ "version": "0.7.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.17.0"
22
+ "clarusjs": "^0.18.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.17.0",
53
+ "clarusjs": "^0.18.0",
54
54
  "typescript": "^7.0.2",
55
55
  "vite": "^8.3.8",
56
56
  "vitest": "^5.1.1"