@mui/internal-docs-infra 0.12.1-canary.46 → 0.12.1-canary.47

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mui/internal-docs-infra",
3
- "version": "0.12.1-canary.46",
3
+ "version": "0.12.1-canary.47",
4
4
  "author": "MUI Team",
5
5
  "description": "MUI Infra - internal documentation creation tools.",
6
6
  "license": "MIT",
@@ -815,5 +815,5 @@
815
815
  "bin": {
816
816
  "docs-infra": "./cli/index.mjs"
817
817
  },
818
- "gitSha": "0534275eee6a8935af28abb295f1ed280d459e95"
818
+ "gitSha": "81765a8f621d2dc77469da42ae427c0b7e680e4f"
819
819
  }
@@ -1,2 +1,2 @@
1
- export * from "./parseSource.mjs";
1
+ export { createParseSource, parsePlainText, parseSource, registerAllGrammars, registerGrammars, resetStarryNight } from "./parseSource.mjs";
2
2
  export { getGrammarFromLanguage, languageToGrammarMap, extensionMap } from "./grammarMaps.mjs";
@@ -1,2 +1,2 @@
1
- export * from "./parseSource.mjs";
1
+ export { createParseSource, parsePlainText, parseSource, registerAllGrammars, registerGrammars, resetStarryNight } from "./parseSource.mjs";
2
2
  export { getGrammarFromLanguage, languageToGrammarMap, extensionMap } from "./grammarMaps.mjs";
@@ -1,4 +1,6 @@
1
+ import { createStarryNight } from '@wooorm/starry-night';
1
2
  import type { ParseSource } from "../../CodeHighlighter/types.mjs";
3
+ type StarryNight = Awaited<ReturnType<typeof createStarryNight>>;
2
4
  /**
3
5
  * Parses source into a line-guttered HAST **without** syntax highlighting — the
4
6
  * raw text wrapped in the same `.line`/`.frame` structure `parseSource` produces,
@@ -51,8 +53,16 @@ export declare function registerAllGrammars(): Promise<void>;
51
53
  * @returns A Promise that resolves to the initialized `parseSource` function
52
54
  */
53
55
  export declare const createParseSource: (initialScopes?: string[]) => Promise<ParseSource>;
56
+ /**
57
+ * Returns the shared Starry Night instance after registering every grammar.
58
+ *
59
+ * Inline highlighting can follow code that initialized a lazy subset, so it must
60
+ * initialize the complete registry before using the shared instance.
61
+ */
62
+ export declare function getStarryNightInstance(): Promise<StarryNight>;
54
63
  /**
55
64
  * Clears the global Starry Night singleton and registration state. Intended for
56
65
  * tests exercising lazy registration from a known-empty registry.
57
66
  */
58
- export declare function resetStarryNight(): void;
67
+ export declare function resetStarryNight(): void;
68
+ export {};
@@ -232,6 +232,21 @@ export const createParseSource = async initialScopes => {
232
232
  return parseSource;
233
233
  };
234
234
 
235
+ /**
236
+ * Returns the shared Starry Night instance after registering every grammar.
237
+ *
238
+ * Inline highlighting can follow code that initialized a lazy subset, so it must
239
+ * initialize the complete registry before using the shared instance.
240
+ */
241
+ export async function getStarryNightInstance() {
242
+ await createParseSource();
243
+ const instance = getInstance();
244
+ if (!instance) {
245
+ throw new Error('Starry Night failed to initialize.');
246
+ }
247
+ return instance;
248
+ }
249
+
235
250
  /**
236
251
  * Clears the global Starry Night singleton and registration state. Intended for
237
252
  * tests exercising lazy registration from a known-empty registry.
@@ -1,29 +1,27 @@
1
- import { createStarryNight } from '@wooorm/starry-night';
2
1
  import { visit } from 'unist-util-visit';
3
- import { grammars } from "../parseSource/grammars.mjs";
4
- import { extensionMap } from "../parseSource/grammarMaps.mjs";
2
+ import { getStarryNightInstance } from "../parseSource/parseSource.mjs";
3
+ import { getGrammarFromLanguage } from "../parseSource/grammarMaps.mjs";
5
4
  import { extendSyntaxTokens } from "../parseSource/extendSyntaxTokens.mjs";
6
5
  import { getLanguageCapabilitiesFromScope } from "../parseSource/languageCapabilities.mjs";
7
6
  import { getHastTextContent } from "../hastUtils/index.mjs";
8
7
  import { removePrefixFromHighlightedNodes } from "./removePrefixFromHighlightedNodes.mjs";
9
8
  import { removeSuffixFromHighlightedNodes } from "./removeSuffixFromHighlightedNodes.mjs";
10
- const STARRY_NIGHT_KEY = '__docs_infra_starry_night_instance__';
9
+ function getGrammarScope(node) {
10
+ const className = node.properties?.className;
11
+ if (!Array.isArray(className)) {
12
+ return undefined;
13
+ }
14
+ const languageClass = className.find(classNameValue => typeof classNameValue === 'string' && classNameValue.startsWith('language-'));
15
+ if (typeof languageClass !== 'string') {
16
+ return undefined;
17
+ }
18
+ return getGrammarFromLanguage(languageClass.replace('language-', ''));
19
+ }
11
20
 
12
21
  /**
13
22
  * Options for the transformHtmlCodeInline plugin.
14
23
  */
15
24
 
16
- /**
17
- * Ensures Starry Night is initialized and returns the instance.
18
- * Uses a global singleton for efficiency across multiple plugin invocations.
19
- */
20
- async function getStarryNight() {
21
- if (!globalThis[STARRY_NIGHT_KEY]) {
22
- globalThis[STARRY_NIGHT_KEY] = await createStarryNight(grammars);
23
- }
24
- return globalThis[STARRY_NIGHT_KEY];
25
- }
26
-
27
25
  /**
28
26
  * A rehype plugin that applies inline syntax highlighting to code elements.
29
27
  * Unlike transformHtmlCodeBlock, this does NOT add line gutters or precomputed data.
@@ -39,7 +37,7 @@ export default function transformHtmlCodeInline(options = {}) {
39
37
  includePreElements = false
40
38
  } = options;
41
39
  return async tree => {
42
- const starryNight = await getStarryNight();
40
+ const starryNight = await getStarryNightInstance();
43
41
  visit(tree, 'element', (node, _index, parent) => {
44
42
  // Only process code elements (inline code or code blocks without special handling)
45
43
  if (node.tagName !== 'code') {
@@ -78,41 +76,11 @@ export default function transformHtmlCodeInline(options = {}) {
78
76
  // out the keys via `splitObjectKeys`. The wrapping characters are stripped after highlighting.
79
77
  let objectWrap = false;
80
78
 
81
- // Determine language from className (e.g., 'language-ts')
82
- const className = node.properties?.className;
83
- let fileType;
84
- if (Array.isArray(className)) {
85
- const langClass = className.find(c => typeof c === 'string' && c.startsWith('language-'));
86
- if (langClass && typeof langClass === 'string') {
87
- const lang = langClass.replace('language-', '');
88
- // Map common language names to file extensions
89
- const langToExt = {
90
- ts: '.ts',
91
- typescript: '.ts',
92
- js: '.js',
93
- javascript: '.js',
94
- jsx: '.jsx',
95
- tsx: '.tsx',
96
- css: '.css',
97
- html: '.html',
98
- json: '.json',
99
- md: '.md',
100
- markdown: '.md',
101
- sh: '.sh',
102
- shell: '.sh',
103
- bash: '.sh',
104
- yaml: '.yaml',
105
- yml: '.yaml'
106
- };
107
- fileType = langToExt[lang] || `.${lang}`;
108
- }
109
- }
110
-
111
79
  // Skip if no language specified or unsupported type
112
- if (!fileType || !extensionMap[fileType]) {
80
+ const grammarScope = getGrammarScope(node);
81
+ if (!grammarScope) {
113
82
  return;
114
83
  }
115
- const grammarScope = extensionMap[fileType];
116
84
  if (!highlightingPrefix && getLanguageCapabilitiesFromScope(grammarScope).semantics === 'js') {
117
85
  const trimmed = sourceToHighlight.trim();
118
86
  if (trimmed.length >= 2 && trimmed.startsWith('{') && trimmed.endsWith('}')) {
@@ -122,8 +90,8 @@ export default function transformHtmlCodeInline(options = {}) {
122
90
  }
123
91
 
124
92
  // Apply syntax highlighting
125
- const highlighted = starryNight.highlight(sourceToHighlight, extensionMap[fileType]);
126
- extendSyntaxTokens(highlighted, extensionMap[fileType]);
93
+ const highlighted = starryNight.highlight(sourceToHighlight, grammarScope);
94
+ extendSyntaxTokens(highlighted, grammarScope);
127
95
 
128
96
  // Replace the code element's children with the highlighted nodes
129
97
  if (highlighted.type === 'root' && highlighted.children) {