@mui/internal-docs-infra
Version:
MUI Infra - internal documentation creation tools.
35 lines (34 loc) • 1.27 kB
JavaScript
import { resolveGrammarScope } from "./grammarMaps.mjs";
/**
* Enumerates the unique grammar scopes a code block needs to highlight every
* file across all of its variants — the main file plus each extra file, by
* extension or explicit `language`. Cheap and synchronous (it reads only the
* lightweight metadata, never the grammar JSON), so it can run on every render
* to drive a speculative grammar preload.
*
* Variants that are bare source strings (no file metadata) or `undefined`, and
* files whose extension/language maps to no supported grammar, contribute no
* scope.
*/
export function detectGrammarScopes(code) {
const scopes = new Set();
for (const variant of Object.values(code)) {
if (!variant || typeof variant === 'string') {
continue;
}
const mainScope = resolveGrammarScope(variant.fileName, variant.language);
if (mainScope) {
scopes.add(mainScope);
}
if (variant.extraFiles) {
for (const [fileName, extra] of Object.entries(variant.extraFiles)) {
const language = typeof extra === 'object' ? extra.language : undefined;
const scope = resolveGrammarScope(fileName, language);
if (scope) {
scopes.add(scope);
}
}
}
}
return [...scopes];
}