@lexibase/rdx-core 3.0.1 → 3.1.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,6 +1,7 @@
1
1
  // src/config/index.ts
2
2
  import path7 from "path";
3
- import { existsSync, readdirSync } from "fs";
3
+ import { existsSync, readFileSync, readdirSync, statSync } from "fs";
4
+ import { bundledThemes } from "shiki/themes";
4
5
 
5
6
  // src/config/error.ts
6
7
  var RdxConfigError = class extends Error {
@@ -716,7 +717,8 @@ function resolveConfig(input) {
716
717
  "homepage",
717
718
  "TOC",
718
719
  "search",
719
- "editPage"
720
+ "editPage",
721
+ "syntaxHighlighting"
720
722
  ],
721
723
  ""
722
724
  );
@@ -728,6 +730,44 @@ function resolveConfig(input) {
728
730
  text(config.description, "description", true);
729
731
  if (config.lang !== void 0) text(config.lang, "lang");
730
732
  validateMetadata(config.metadata);
733
+ if (config.syntaxHighlighting !== void 0) {
734
+ const highlighting = object(config.syntaxHighlighting, "syntaxHighlighting");
735
+ keys(highlighting, ["languages", "themes"], "syntaxHighlighting.");
736
+ if (highlighting.themes !== void 0) {
737
+ const themes = object(highlighting.themes, "syntaxHighlighting.themes");
738
+ keys(themes, ["light", "dark"], "syntaxHighlighting.themes.");
739
+ for (const [mode, value] of Object.entries(themes)) {
740
+ const field = `syntaxHighlighting.themes.${mode}`;
741
+ const name = text(value, field);
742
+ if (!Object.hasOwn(bundledThemes, name))
743
+ throw new RdxConfigError(
744
+ `${field} must name a bundled Shiki theme. Unknown theme "${name}".`
745
+ );
746
+ }
747
+ }
748
+ const languages = object(
749
+ highlighting.languages === void 0 ? {} : highlighting.languages,
750
+ "syntaxHighlighting.languages"
751
+ );
752
+ for (const [name, filename] of Object.entries(languages)) {
753
+ const field = `syntaxHighlighting.languages.${name}`;
754
+ if (!/^[a-z0-9][a-z0-9_+-]*$/.test(name) || [
755
+ "ansi",
756
+ "diff",
757
+ "folder",
758
+ "mermaid",
759
+ "plain",
760
+ "plaintext",
761
+ "text",
762
+ "txt"
763
+ ].includes(name))
764
+ throw new RdxConfigError(
765
+ `${field} must use a lowercase, non-reserved fence name.`
766
+ );
767
+ if (!text(filename, field).endsWith(".json"))
768
+ throw new RdxConfigError(`${field} must point to a TextMate JSON file.`);
769
+ }
770
+ }
731
771
  if (config.editPage !== void 0 && config.editPage !== false) {
732
772
  const editPage = object(config.editPage, "editPage");
733
773
  keys(editPage, ["baseUrl"], "editPage.");
@@ -882,6 +922,45 @@ async function createRdxProject(input, {
882
922
  ];
883
923
  const commands = [];
884
924
  const runtimeFiles = /* @__PURE__ */ new Set();
925
+ const syntaxGrammars = Object.entries(
926
+ config.syntaxHighlighting?.languages ?? {}
927
+ ).map(([name, filename]) => {
928
+ const field = `syntaxHighlighting.languages.${name}`;
929
+ const resolved = boundedProjectPath(rootDir, filename, field);
930
+ let grammar;
931
+ try {
932
+ if (!statSync(
933
+ /* turbopackIgnore: true */
934
+ resolved
935
+ ).isFile())
936
+ throw new Error("Expected a regular file");
937
+ grammar = JSON.parse(
938
+ readFileSync(
939
+ /* turbopackIgnore: true */
940
+ resolved,
941
+ "utf8"
942
+ )
943
+ );
944
+ } catch (error) {
945
+ throw new RdxConfigError(
946
+ `${field} could not read "${filename}": ${error.message}`
947
+ );
948
+ }
949
+ if (!record(grammar) || typeof grammar.scopeName !== "string" || !grammar.scopeName.trim() || !Array.isArray(grammar.patterns))
950
+ throw new RdxConfigError(
951
+ `${field} must contain a TextMate grammar with scopeName and patterns.`
952
+ );
953
+ runtimeFiles.add(
954
+ path7.relative(rootDir, resolveProjectPath(rootDir, filename, field))
955
+ );
956
+ runtimeFiles.add(path7.relative(rootDir, resolved));
957
+ return {
958
+ ...grammar,
959
+ name,
960
+ scopeName: grammar.scopeName,
961
+ patterns: grammar.patterns
962
+ };
963
+ });
885
964
  let localization;
886
965
  const commandNames = /* @__PURE__ */ new Set([
887
966
  "dev",
@@ -1040,6 +1119,7 @@ async function createRdxProject(input, {
1040
1119
  sources,
1041
1120
  commands,
1042
1121
  runtimeFiles: [...runtimeFiles],
1122
+ ...syntaxGrammars.length ? { syntaxGrammars } : {},
1043
1123
  ...localization ? { localization } : {}
1044
1124
  };
1045
1125
  }
@@ -11,7 +11,7 @@ import {
11
11
  resolveConfig,
12
12
  resolveProjectPath,
13
13
  snapshotDatesFilename
14
- } from "../chunk-FZGWKA74.js";
14
+ } from "../chunk-KPVVN2MN.js";
15
15
  import {
16
16
  getContentSources
17
17
  } from "../chunk-SNNEXRLQ.js";
package/dist/index.js CHANGED
@@ -12,7 +12,7 @@ import {
12
12
  defineConfig,
13
13
  resolveConfig,
14
14
  resolveProjectPath
15
- } from "./chunk-FZGWKA74.js";
15
+ } from "./chunk-KPVVN2MN.js";
16
16
  import {
17
17
  getContentSources,
18
18
  getDocsHref,
@@ -2,10 +2,12 @@ import { D as DocumentLinkOptions } from '../document-links-fo6ul9xz.js';
2
2
  export { r as remarkDocumentLinks } from '../document-links-fo6ul9xz.js';
3
3
  export { default as rehypeDocumentationHeadings } from 'rehype-slug';
4
4
  import rehypeShiki from '@shikijs/rehype';
5
+ import { RdxProject, RDXConfig } from '@lexibase/rdx-types';
5
6
  import remarkDirective from 'remark-directive';
6
7
 
7
- /** shadcn's Shiki/GitHub palette, applied to MDX on the server. */
8
- declare function rehypeDocumentationCode(this: ThisParameterType<typeof rehypeShiki>): ReturnType<typeof rehypeShiki>;
8
+ type SyntaxGrammars = NonNullable<RdxProject['syntaxGrammars']>;
9
+ /** Apply syntax highlighting to Markdown code blocks on the server. */
10
+ declare function rehypeDocumentationCode(this: ThisParameterType<typeof rehypeShiki>, grammars?: SyntaxGrammars, configuredThemes?: NonNullable<RDXConfig['syntaxHighlighting']>['themes']): ReturnType<typeof rehypeShiki>;
9
11
 
10
12
  /** Markdown authoring adapters for the shared shadcn/Base UI components. */
11
13
  declare function remarkDocumentationComponents(this: ThisParameterType<typeof remarkDirective>): (tree: unknown, file: {
package/dist/mdx/index.js CHANGED
@@ -10,6 +10,10 @@ import { default as default2 } from "rehype-slug";
10
10
 
11
11
  // src/mdx/code-highlighting.ts
12
12
  import rehypeShiki from "@shikijs/rehype";
13
+ import rehypeShikiFromHighlighter from "@shikijs/rehype/core";
14
+ import {
15
+ createHighlighter
16
+ } from "shiki";
13
17
 
14
18
  // src/mdx/folder-tree.ts
15
19
  var branch = /^([ \t|│]*)(\|-|\|_|├──|└──)(?:[ \t]+(.*))?$/;
@@ -124,10 +128,15 @@ function prepareCodeBlocks(node) {
124
128
  }
125
129
  node.children?.forEach(prepareCodeBlocks);
126
130
  }
127
- function rehypeDocumentationCode() {
131
+ var customHighlighter;
132
+ function rehypeDocumentationCode(grammars, configuredThemes) {
133
+ const themes = {
134
+ light: configuredThemes?.light ?? "github-light",
135
+ dark: configuredThemes?.dark ?? "github-dark"
136
+ };
128
137
  const diffVariants = /* @__PURE__ */ new WeakMap();
129
- const highlight = rehypeShiki.call(this, {
130
- themes: { light: "github-light", dark: "github-dark" },
138
+ const options = {
139
+ themes,
131
140
  defaultColor: false,
132
141
  addLanguageClass: true,
133
142
  // Load grammars only when a document uses them, not the whole bundle.
@@ -169,7 +178,23 @@ function rehypeDocumentationCode() {
169
178
  }
170
179
  }
171
180
  ]
172
- });
181
+ };
182
+ const highlight = grammars?.length ? async (tree, ...args) => {
183
+ const key = JSON.stringify([grammars, themes]);
184
+ if (customHighlighter?.key !== key)
185
+ customHighlighter = {
186
+ key,
187
+ value: createHighlighter({
188
+ themes: Object.values(themes),
189
+ langs: grammars
190
+ })
191
+ };
192
+ const highlighter = await customHighlighter.value;
193
+ const transform = rehypeShikiFromHighlighter(highlighter, options);
194
+ const result = await transform(tree, ...args);
195
+ if (result instanceof Error) throw result;
196
+ return result;
197
+ } : rehypeShiki.call(this, options);
173
198
  if (typeof highlight !== "function") return highlight;
174
199
  return (tree, ...args) => {
175
200
  prepareCodeBlocks(tree);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lexibase/rdx-core",
3
- "version": "3.0.1",
3
+ "version": "3.1.0",
4
4
  "description": "Content indexing and document utilities for RDX documentation projects.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -54,14 +54,15 @@
54
54
  "remark-mdx": "^3.1.1",
55
55
  "remark-parse": "^11.0.0",
56
56
  "remark-stringify": "^11.0.0",
57
+ "shiki": "^4.5.0",
57
58
  "unified": "^11.0.5",
58
- "@lexibase/rdx-types": "2.3.0"
59
+ "@lexibase/rdx-types": "2.4.0"
59
60
  },
60
61
  "devDependencies": {
61
62
  "@types/node": "^22.15.3",
62
63
  "@types/react": "19.3.0",
63
- "@rdx/eslint-config": "1.0.0",
64
- "@rdx/typescript-config": "1.0.0"
64
+ "@rdx/typescript-config": "1.0.0",
65
+ "@rdx/eslint-config": "1.0.0"
65
66
  },
66
67
  "engines": {
67
68
  "node": "^22.18.0 || ^24.11.0"