@ngrok/mantle-server-syntax-highlighter 1.0.3 → 1.0.4
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/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -145,7 +145,7 @@ async function highlightWithMantleShiki(input) {
|
|
|
145
145
|
const resolvedLanguage = isSupportedLanguage(input.language) ? input.language : parseLanguage(input.language);
|
|
146
146
|
const indentation = input.indentation ?? inferIndentation(resolvedLanguage, void 0);
|
|
147
147
|
const normalizedCode = normalizeIndentation(input.code, { indentation });
|
|
148
|
-
const showLineNumbers = input.showLineNumbers ??
|
|
148
|
+
const showLineNumbers = input.showLineNumbers ?? true;
|
|
149
149
|
const highlightLines = input.highlightLines ?? [];
|
|
150
150
|
const lineNumberStart = input.lineNumberStart ?? 1;
|
|
151
151
|
const cacheKey = createHash("sha1").update(normalizedCode).update(`\0${resolvedLanguage}\0${showLineNumbers}\0${lineNumberStart}\0${JSON.stringify(highlightLines)}`).digest("hex");
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../src/lru-cache.ts","../src/engine.ts"],"sourcesContent":["/** A simple least-recently-used cache backed by insertion-order `Map` semantics. */\ntype LruCache<K, V> = {\n\tdelete: (key: K) => boolean;\n\tget: (key: K) => V | undefined;\n\tset: (key: K, value: V) => void;\n\tsize: () => number;\n};\n\n/** Creates an LRU cache that evicts the least-recently-used entry when `maxEntries` is exceeded. */\nfunction createLruCache<K, V>(maxEntries: number): LruCache<K, V> {\n\tif (!Number.isInteger(maxEntries) || maxEntries <= 0) {\n\t\tthrow new Error(\"LRU cache maxEntries must be a positive integer.\");\n\t}\n\n\tconst map = new Map<K, V>();\n\n\treturn {\n\t\tdelete(key) {\n\t\t\treturn map.delete(key);\n\t\t},\n\t\tget(key) {\n\t\t\tif (!map.has(key)) {\n\t\t\t\treturn undefined;\n\t\t\t}\n\n\t\t\tconst value = map.get(key) as V;\n\t\t\tmap.delete(key);\n\t\t\tmap.set(key, value);\n\t\t\treturn value;\n\t\t},\n\t\tset(key, value) {\n\t\t\tif (map.has(key)) {\n\t\t\t\tmap.delete(key);\n\t\t\t}\n\n\t\t\tmap.set(key, value);\n\t\t\tif (map.size <= maxEntries) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconst oldestKey = map.keys().next().value as K | undefined;\n\t\t\tif (oldestKey != null) {\n\t\t\t\tmap.delete(oldestKey);\n\t\t\t}\n\t\t},\n\t\tsize() {\n\t\t\treturn map.size;\n\t\t},\n\t};\n}\n\nexport { createLruCache };\nexport type { LruCache };\n","import { createHash } from \"node:crypto\";\nimport {\n\tdecorateHighlightedHtml,\n\tinferIndentation,\n\tisSupportedLanguage,\n\tnormalizeIndentation,\n\tparseLanguage,\n\ttype Indentation,\n\ttype LineRange,\n\ttype SupportedLanguage,\n} from \"@ngrok/mantle/highlight-utils\";\nimport { createLruCache } from \"./lru-cache.js\";\n\n/**\n * Shiki grammar IDs preloaded by Mantle's highlighting engine.\n * This list is shared by both Vite plugins and server-side highlighting routes.\n */\nconst mantleShikiLanguageGrammarIds = [\n\t\"bash\",\n\t\"csharp\",\n\t\"css\",\n\t\"go\",\n\t\"html\",\n\t\"java\",\n\t\"javascript\",\n\t\"json\",\n\t\"jsx\",\n\t\"python\",\n\t\"ruby\",\n\t\"rust\",\n\t\"shell\",\n\t\"tsx\",\n\t\"typescript\",\n\t\"xml\",\n\t\"yaml\",\n] as const satisfies SupportedLanguage[];\n\n/**\n * Shared Shiki theme name used across Mantle highlighting.\n */\nconst mantleShikiThemeName = \"mantle-css-variables\" as const;\nconst mantleHighlightCacheMaxEntries = 1000;\n\ntype ShikiHighlighter = Awaited<ReturnType<(typeof import(\"shiki\"))[\"createHighlighter\"]>>;\n\ntype MantleHighlightInput = {\n\t/**\n\t * Raw code string to highlight.\n\t */\n\tcode: string;\n\t/**\n\t * Language hint. Accepts supported language aliases and markdown-style values.\n\t */\n\tlanguage: SupportedLanguage | (string & {});\n\t/**\n\t * Optional indentation preference. Defaults to language-inferred indentation.\n\t */\n\tindentation?: Indentation;\n\t/**\n\t * Whether to render line numbers into the returned HTML.\n\t * @default false\n\t */\n\tshowLineNumbers?: boolean;\n\t/**\n\t * Optional list of highlighted line numbers/ranges to render into the returned HTML.\n\t * @default []\n\t */\n\thighlightLines?: (LineRange | number)[];\n\t/**\n\t * Start line number for rendered line numbers.\n\t * @default 1\n\t */\n\tlineNumberStart?: number;\n};\n\ntype MantleHighlightResult = {\n\t/**\n\t * Normalized source code used as the highlight input.\n\t */\n\tcode: string;\n\t/**\n\t * Highlighted HTML extracted from Shiki's `<code>...</code>`.\n\t */\n\thtml: string;\n\t/**\n\t * Resolved supported language used for highlighting.\n\t */\n\tlanguage: SupportedLanguage;\n\t/**\n\t * Whether returned HTML includes line numbers.\n\t */\n\tshowLineNumbers: boolean;\n\t/**\n\t * Highlighted line numbers/ranges applied to the returned HTML.\n\t */\n\thighlightLines: (LineRange | number)[];\n\t/**\n\t * Start line number used for rendered line numbers.\n\t */\n\tlineNumberStart: number;\n};\n\ntype MantleServerHighlighter = {\n\t/**\n\t * Highlight a code snippet and return normalized code + highlighted HTML.\n\t */\n\thighlight: (input: MantleHighlightInput) => Promise<MantleHighlightResult>;\n};\n\nlet highlighterPromise: Promise<ShikiHighlighter> | undefined;\nconst mantleHighlightCache = createLruCache<string, Promise<MantleHighlightResult>>(\n\tmantleHighlightCacheMaxEntries,\n);\n\n/**\n * Creates (and caches) Mantle's shared Shiki highlighter instance.\n */\nfunction getMantleShikiHighlighter(): Promise<ShikiHighlighter> {\n\tif (highlighterPromise != null) {\n\t\treturn highlighterPromise;\n\t}\n\n\thighlighterPromise = (async () => {\n\t\tconst { createCssVariablesTheme, createHighlighter } = await import(\"shiki\");\n\t\tconst cssVarsTheme = createCssVariablesTheme({ name: mantleShikiThemeName });\n\n\t\t// Append more granular token scope → CSS variable mappings.\n\t\t// More specific scopes override the defaults from createCssVariablesTheme.\n\t\t// Each uses a fallback to the original grouping variable for backwards compat.\n\t\tcssVarsTheme.tokenColors = [\n\t\t\t...(cssVarsTheme.tokenColors ?? []),\n\t\t\t{\n\t\t\t\tscope: [\"entity.name.type\", \"entity.name.class\", \"support.type\", \"support.class\"],\n\t\t\t\tsettings: { foreground: \"var(--shiki-token-type, var(--shiki-token-function))\" },\n\t\t\t},\n\t\t\t{\n\t\t\t\tscope: [\"variable\", \"variable.parameter\", \"variable.other\"],\n\t\t\t\tsettings: { foreground: \"var(--shiki-token-variable, var(--shiki-foreground))\" },\n\t\t\t},\n\t\t\t{\n\t\t\t\tscope: [\"keyword.operator\", \"keyword.operator.assignment\", \"keyword.operator.comparison\"],\n\t\t\t\tsettings: { foreground: \"var(--shiki-token-operator, var(--shiki-token-keyword))\" },\n\t\t\t},\n\t\t\t{\n\t\t\t\tscope: [\"entity.other.attribute-name\"],\n\t\t\t\tsettings: { foreground: \"var(--shiki-token-attribute, var(--shiki-token-constant))\" },\n\t\t\t},\n\t\t\t{\n\t\t\t\tscope: [\"meta.object-literal.key\", \"support.type.property-name\", \"entity.name.tag.yaml\"],\n\t\t\t\tsettings: { foreground: \"var(--shiki-token-property, var(--shiki-token-function))\" },\n\t\t\t},\n\t\t\t{\n\t\t\t\tscope: [\"constant.character.escape\"],\n\t\t\t\tsettings: { foreground: \"var(--shiki-token-escape, var(--shiki-token-constant))\" },\n\t\t\t},\n\t\t\t{\n\t\t\t\tscope: [\"string.unquoted.plain.out.yaml\", \"string.unquoted.plain.in.yaml\"],\n\t\t\t\tsettings: { foreground: \"var(--shiki-token-variable, var(--shiki-foreground))\" },\n\t\t\t},\n\t\t\t{\n\t\t\t\tscope: [\"punctuation.definition.block.sequence.item.yaml\"],\n\t\t\t\tsettings: { foreground: \"var(--shiki-token-variable, var(--shiki-foreground))\" },\n\t\t\t},\n\t\t];\n\n\t\treturn createHighlighter({\n\t\t\tthemes: [cssVarsTheme],\n\t\t\tlangs: [...mantleShikiLanguageGrammarIds],\n\t\t});\n\t})().catch((error) => {\n\t\thighlighterPromise = undefined;\n\t\tthrow error;\n\t});\n\n\treturn highlighterPromise;\n}\n\n/**\n * Extracts the inner HTML from a Shiki `<code>...</code>` wrapper.\n */\nfunction extractHighlightedCodeInnerHtml(fullHtml: string): string | undefined {\n\treturn fullHtml.match(/<code[^>]*>([\\s\\S]*?)<\\/code>/)?.[1];\n}\n\n/**\n * Highlights code with Mantle's shared Shiki engine and normalization defaults.\n */\nasync function highlightWithMantleShiki(\n\tinput: MantleHighlightInput,\n): Promise<MantleHighlightResult> {\n\tconst resolvedLanguage = isSupportedLanguage(input.language)\n\t\t? input.language\n\t\t: parseLanguage(input.language);\n\tconst indentation = input.indentation ?? inferIndentation(resolvedLanguage, undefined);\n\tconst normalizedCode = normalizeIndentation(input.code, {\n\t\tindentation,\n\t});\n\tconst showLineNumbers = input.showLineNumbers ?? false;\n\tconst highlightLines = input.highlightLines ?? [];\n\tconst lineNumberStart = input.lineNumberStart ?? 1;\n\tconst cacheKey = createHash(\"sha1\")\n\t\t.update(normalizedCode)\n\t\t.update(\n\t\t\t`\\0${resolvedLanguage}\\0${showLineNumbers}\\0${lineNumberStart}\\0${JSON.stringify(highlightLines)}`,\n\t\t)\n\t\t.digest(\"hex\");\n\tconst cached = mantleHighlightCache.get(cacheKey);\n\tif (cached != null) {\n\t\treturn cached;\n\t}\n\n\tconst promise = (async () => {\n\t\tconst highlighter = await getMantleShikiHighlighter();\n\t\tconst fullHtml = highlighter.codeToHtml(normalizedCode, {\n\t\t\tlang: resolvedLanguage,\n\t\t\ttheme: mantleShikiThemeName,\n\t\t});\n\t\tconst baseHtml = extractHighlightedCodeInnerHtml(fullHtml);\n\n\t\tif (baseHtml == null) {\n\t\t\tthrow new Error(\"Failed to extract highlighted HTML from Shiki output\");\n\t\t}\n\t\tconst html = decorateHighlightedHtml({\n\t\t\thighlightLines,\n\t\t\thtml: baseHtml,\n\t\t\tlineNumberStart,\n\t\t\tshowLineNumbers,\n\t\t});\n\n\t\treturn {\n\t\t\tcode: normalizedCode,\n\t\t\thtml,\n\t\t\tlanguage: resolvedLanguage,\n\t\t\tshowLineNumbers,\n\t\t\thighlightLines,\n\t\t\tlineNumberStart,\n\t\t};\n\t})().catch((error) => {\n\t\tmantleHighlightCache.delete(cacheKey);\n\t\tthrow error;\n\t});\n\n\tmantleHighlightCache.set(cacheKey, promise);\n\treturn promise;\n}\n\n/**\n * Creates a reusable Mantle server highlighter facade for API routes.\n */\nfunction createMantleServerSyntaxHighlighter(): MantleServerHighlighter {\n\treturn {\n\t\thighlight: highlightWithMantleShiki,\n\t};\n}\n\nexport {\n\tcreateMantleServerSyntaxHighlighter,\n\textractHighlightedCodeInnerHtml,\n\tgetMantleShikiHighlighter,\n\thighlightWithMantleShiki,\n\tmantleHighlightCacheMaxEntries,\n\tmantleShikiLanguageGrammarIds,\n\tmantleShikiThemeName,\n};\nexport type { MantleHighlightInput, MantleHighlightResult, MantleServerHighlighter };\n"],"mappings":";;;;;AASA,SAAS,eAAqB,YAAoC;AACjE,KAAI,CAAC,OAAO,UAAU,WAAW,IAAI,cAAc,EAClD,OAAM,IAAI,MAAM,mDAAmD;CAGpE,MAAM,sBAAM,IAAI,KAAW;AAE3B,QAAO;EACN,OAAO,KAAK;AACX,UAAO,IAAI,OAAO,IAAI;;EAEvB,IAAI,KAAK;AACR,OAAI,CAAC,IAAI,IAAI,IAAI,CAChB;GAGD,MAAM,QAAQ,IAAI,IAAI,IAAI;AAC1B,OAAI,OAAO,IAAI;AACf,OAAI,IAAI,KAAK,MAAM;AACnB,UAAO;;EAER,IAAI,KAAK,OAAO;AACf,OAAI,IAAI,IAAI,IAAI,CACf,KAAI,OAAO,IAAI;AAGhB,OAAI,IAAI,KAAK,MAAM;AACnB,OAAI,IAAI,QAAQ,WACf;GAGD,MAAM,YAAY,IAAI,MAA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GAAG,8BAA8B;GACzC,CAAC;KACC,CAAC,OAAO,UAAU;AACrB,uBAAqB;AACrB,QAAM;GACL;AAEF,QAAO;;;;;AAMR,SAAS,gCAAgC,UAAsC;AAC9E,QAAO,SAAS,MAAM,gCAAgC,GAAG;;;;;AAM1D,eAAe,yBACd,OACiC;CACjC,MAAM,mBAAmB,oBAAoB,MAAM,SAAS,GACzD,MAAM,WACN,cAAc,MAAM,SAAS;CAChC,MAAM,cAAc,MAAM,eAAe,iBAAiB,kBAAkB,OAAU;CACtF,MAAM,iBAAiB,qBAAqB,MAAM,MAAM,EACvD,aACA,CAAC;CACF,MAAM,kBAAkB,MAAM,mBAAmB;CACjD,MAAM,iBAAiB,MAAM,kBAAkB,EAAE;CACjD,MAAM,kBAAkB,MAAM,mBAAmB;CACjD,MAAM,WAAW,WAAW,OAAO,CACjC,OAAO,eAAe,CACtB,OACA,KAAK,iBAAiB,IAAI,gBAAgB,IAAI,gBAAgB,IAAI,KAAK,UAAU,eAAe,GAChG,CACA,OAAO,MAAM;CACf,MAAM,SAAS,qBAAqB,IAAI,SAAS;AACjD,KAAI,UAAU,KACb,QAAO;CAGR,MAAM,WAAW,YAAY;EAM5B,MAAM,WAAW,iCALG,MAAM,2BAA2B,EACxB,WAAW,gBAAgB;GACvD,MAAM;GACN,OAAO;GACP,CAAC,CACwD;AAE1D,MAAI,YAAY,KACf,OAAM,IAAI,MAAM,uDAAuD;AASxE,SAAO;GACN,MAAM;GACN,MATY,wBAAwB;IACpC;IACA,MAAM;IACN;IACA;IACA,CAAC;GAKD,UAAU;GACV;GACA;GACA;GACA;KACE,CAAC,OAAO,UAAU;AACrB,uBAAqB,OAAO,SAAS;AACrC,QAAM;GACL;AAEF,sBAAqB,IAAI,UAAU,QAAQ;AAC3C,QAAO;;;;;AAMR,SAAS,sCAA+D;AACvE,QAAO,EACN,WAAW,0BACX"}
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/lru-cache.ts","../src/engine.ts"],"sourcesContent":["/** A simple least-recently-used cache backed by insertion-order `Map` semantics. */\ntype LruCache<K, V> = {\n\tdelete: (key: K) => boolean;\n\tget: (key: K) => V | undefined;\n\tset: (key: K, value: V) => void;\n\tsize: () => number;\n};\n\n/** Creates an LRU cache that evicts the least-recently-used entry when `maxEntries` is exceeded. */\nfunction createLruCache<K, V>(maxEntries: number): LruCache<K, V> {\n\tif (!Number.isInteger(maxEntries) || maxEntries <= 0) {\n\t\tthrow new Error(\"LRU cache maxEntries must be a positive integer.\");\n\t}\n\n\tconst map = new Map<K, V>();\n\n\treturn {\n\t\tdelete(key) {\n\t\t\treturn map.delete(key);\n\t\t},\n\t\tget(key) {\n\t\t\tif (!map.has(key)) {\n\t\t\t\treturn undefined;\n\t\t\t}\n\n\t\t\tconst value = map.get(key) as V;\n\t\t\tmap.delete(key);\n\t\t\tmap.set(key, value);\n\t\t\treturn value;\n\t\t},\n\t\tset(key, value) {\n\t\t\tif (map.has(key)) {\n\t\t\t\tmap.delete(key);\n\t\t\t}\n\n\t\t\tmap.set(key, value);\n\t\t\tif (map.size <= maxEntries) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconst oldestKey = map.keys().next().value as K | undefined;\n\t\t\tif (oldestKey != null) {\n\t\t\t\tmap.delete(oldestKey);\n\t\t\t}\n\t\t},\n\t\tsize() {\n\t\t\treturn map.size;\n\t\t},\n\t};\n}\n\nexport { createLruCache };\nexport type { LruCache };\n","import { createHash } from \"node:crypto\";\nimport {\n\tdecorateHighlightedHtml,\n\tinferIndentation,\n\tisSupportedLanguage,\n\tnormalizeIndentation,\n\tparseLanguage,\n\ttype Indentation,\n\ttype LineRange,\n\ttype SupportedLanguage,\n} from \"@ngrok/mantle/highlight-utils\";\nimport { createLruCache } from \"./lru-cache.js\";\n\n/**\n * Shiki grammar IDs preloaded by Mantle's highlighting engine.\n * This list is shared by both Vite plugins and server-side highlighting routes.\n */\nconst mantleShikiLanguageGrammarIds = [\n\t\"bash\",\n\t\"csharp\",\n\t\"css\",\n\t\"go\",\n\t\"html\",\n\t\"java\",\n\t\"javascript\",\n\t\"json\",\n\t\"jsx\",\n\t\"python\",\n\t\"ruby\",\n\t\"rust\",\n\t\"shell\",\n\t\"tsx\",\n\t\"typescript\",\n\t\"xml\",\n\t\"yaml\",\n] as const satisfies SupportedLanguage[];\n\n/**\n * Shared Shiki theme name used across Mantle highlighting.\n */\nconst mantleShikiThemeName = \"mantle-css-variables\" as const;\nconst mantleHighlightCacheMaxEntries = 1000;\n\ntype ShikiHighlighter = Awaited<ReturnType<(typeof import(\"shiki\"))[\"createHighlighter\"]>>;\n\ntype MantleHighlightInput = {\n\t/**\n\t * Raw code string to highlight.\n\t */\n\tcode: string;\n\t/**\n\t * Language hint. Accepts supported language aliases and markdown-style values.\n\t */\n\tlanguage: SupportedLanguage | (string & {});\n\t/**\n\t * Optional indentation preference. Defaults to language-inferred indentation.\n\t */\n\tindentation?: Indentation;\n\t/**\n\t * Whether to render line numbers into the returned HTML.\n\t * @default true\n\t */\n\tshowLineNumbers?: boolean;\n\t/**\n\t * Optional list of highlighted line numbers/ranges to render into the returned HTML.\n\t * @default []\n\t */\n\thighlightLines?: (LineRange | number)[];\n\t/**\n\t * Start line number for rendered line numbers.\n\t * @default 1\n\t */\n\tlineNumberStart?: number;\n};\n\ntype MantleHighlightResult = {\n\t/**\n\t * Normalized source code used as the highlight input.\n\t */\n\tcode: string;\n\t/**\n\t * Highlighted HTML extracted from Shiki's `<code>...</code>`.\n\t */\n\thtml: string;\n\t/**\n\t * Resolved supported language used for highlighting.\n\t */\n\tlanguage: SupportedLanguage;\n\t/**\n\t * Whether returned HTML includes line numbers.\n\t */\n\tshowLineNumbers: boolean;\n\t/**\n\t * Highlighted line numbers/ranges applied to the returned HTML.\n\t */\n\thighlightLines: (LineRange | number)[];\n\t/**\n\t * Start line number used for rendered line numbers.\n\t */\n\tlineNumberStart: number;\n};\n\ntype MantleServerHighlighter = {\n\t/**\n\t * Highlight a code snippet and return normalized code + highlighted HTML.\n\t */\n\thighlight: (input: MantleHighlightInput) => Promise<MantleHighlightResult>;\n};\n\nlet highlighterPromise: Promise<ShikiHighlighter> | undefined;\nconst mantleHighlightCache = createLruCache<string, Promise<MantleHighlightResult>>(\n\tmantleHighlightCacheMaxEntries,\n);\n\n/**\n * Creates (and caches) Mantle's shared Shiki highlighter instance.\n */\nfunction getMantleShikiHighlighter(): Promise<ShikiHighlighter> {\n\tif (highlighterPromise != null) {\n\t\treturn highlighterPromise;\n\t}\n\n\thighlighterPromise = (async () => {\n\t\tconst { createCssVariablesTheme, createHighlighter } = await import(\"shiki\");\n\t\tconst cssVarsTheme = createCssVariablesTheme({ name: mantleShikiThemeName });\n\n\t\t// Append more granular token scope → CSS variable mappings.\n\t\t// More specific scopes override the defaults from createCssVariablesTheme.\n\t\t// Each uses a fallback to the original grouping variable for backwards compat.\n\t\tcssVarsTheme.tokenColors = [\n\t\t\t...(cssVarsTheme.tokenColors ?? []),\n\t\t\t{\n\t\t\t\tscope: [\"entity.name.type\", \"entity.name.class\", \"support.type\", \"support.class\"],\n\t\t\t\tsettings: { foreground: \"var(--shiki-token-type, var(--shiki-token-function))\" },\n\t\t\t},\n\t\t\t{\n\t\t\t\tscope: [\"variable\", \"variable.parameter\", \"variable.other\"],\n\t\t\t\tsettings: { foreground: \"var(--shiki-token-variable, var(--shiki-foreground))\" },\n\t\t\t},\n\t\t\t{\n\t\t\t\tscope: [\"keyword.operator\", \"keyword.operator.assignment\", \"keyword.operator.comparison\"],\n\t\t\t\tsettings: { foreground: \"var(--shiki-token-operator, var(--shiki-token-keyword))\" },\n\t\t\t},\n\t\t\t{\n\t\t\t\tscope: [\"entity.other.attribute-name\"],\n\t\t\t\tsettings: { foreground: \"var(--shiki-token-attribute, var(--shiki-token-constant))\" },\n\t\t\t},\n\t\t\t{\n\t\t\t\tscope: [\"meta.object-literal.key\", \"support.type.property-name\", \"entity.name.tag.yaml\"],\n\t\t\t\tsettings: { foreground: \"var(--shiki-token-property, var(--shiki-token-function))\" },\n\t\t\t},\n\t\t\t{\n\t\t\t\tscope: [\"constant.character.escape\"],\n\t\t\t\tsettings: { foreground: \"var(--shiki-token-escape, var(--shiki-token-constant))\" },\n\t\t\t},\n\t\t\t{\n\t\t\t\tscope: [\"string.unquoted.plain.out.yaml\", \"string.unquoted.plain.in.yaml\"],\n\t\t\t\tsettings: { foreground: \"var(--shiki-token-variable, var(--shiki-foreground))\" },\n\t\t\t},\n\t\t\t{\n\t\t\t\tscope: [\"punctuation.definition.block.sequence.item.yaml\"],\n\t\t\t\tsettings: { foreground: \"var(--shiki-token-variable, var(--shiki-foreground))\" },\n\t\t\t},\n\t\t];\n\n\t\treturn createHighlighter({\n\t\t\tthemes: [cssVarsTheme],\n\t\t\tlangs: [...mantleShikiLanguageGrammarIds],\n\t\t});\n\t})().catch((error) => {\n\t\thighlighterPromise = undefined;\n\t\tthrow error;\n\t});\n\n\treturn highlighterPromise;\n}\n\n/**\n * Extracts the inner HTML from a Shiki `<code>...</code>` wrapper.\n */\nfunction extractHighlightedCodeInnerHtml(fullHtml: string): string | undefined {\n\treturn fullHtml.match(/<code[^>]*>([\\s\\S]*?)<\\/code>/)?.[1];\n}\n\n/**\n * Highlights code with Mantle's shared Shiki engine and normalization defaults.\n */\nasync function highlightWithMantleShiki(\n\tinput: MantleHighlightInput,\n): Promise<MantleHighlightResult> {\n\tconst resolvedLanguage = isSupportedLanguage(input.language)\n\t\t? input.language\n\t\t: parseLanguage(input.language);\n\tconst indentation = input.indentation ?? inferIndentation(resolvedLanguage, undefined);\n\tconst normalizedCode = normalizeIndentation(input.code, {\n\t\tindentation,\n\t});\n\tconst showLineNumbers = input.showLineNumbers ?? true;\n\tconst highlightLines = input.highlightLines ?? [];\n\tconst lineNumberStart = input.lineNumberStart ?? 1;\n\tconst cacheKey = createHash(\"sha1\")\n\t\t.update(normalizedCode)\n\t\t.update(\n\t\t\t`\\0${resolvedLanguage}\\0${showLineNumbers}\\0${lineNumberStart}\\0${JSON.stringify(highlightLines)}`,\n\t\t)\n\t\t.digest(\"hex\");\n\tconst cached = mantleHighlightCache.get(cacheKey);\n\tif (cached != null) {\n\t\treturn cached;\n\t}\n\n\tconst promise = (async () => {\n\t\tconst highlighter = await getMantleShikiHighlighter();\n\t\tconst fullHtml = highlighter.codeToHtml(normalizedCode, {\n\t\t\tlang: resolvedLanguage,\n\t\t\ttheme: mantleShikiThemeName,\n\t\t});\n\t\tconst baseHtml = extractHighlightedCodeInnerHtml(fullHtml);\n\n\t\tif (baseHtml == null) {\n\t\t\tthrow new Error(\"Failed to extract highlighted HTML from Shiki output\");\n\t\t}\n\t\tconst html = decorateHighlightedHtml({\n\t\t\thighlightLines,\n\t\t\thtml: baseHtml,\n\t\t\tlineNumberStart,\n\t\t\tshowLineNumbers,\n\t\t});\n\n\t\treturn {\n\t\t\tcode: normalizedCode,\n\t\t\thtml,\n\t\t\tlanguage: resolvedLanguage,\n\t\t\tshowLineNumbers,\n\t\t\thighlightLines,\n\t\t\tlineNumberStart,\n\t\t};\n\t})().catch((error) => {\n\t\tmantleHighlightCache.delete(cacheKey);\n\t\tthrow error;\n\t});\n\n\tmantleHighlightCache.set(cacheKey, promise);\n\treturn promise;\n}\n\n/**\n * Creates a reusable Mantle server highlighter facade for API routes.\n */\nfunction createMantleServerSyntaxHighlighter(): MantleServerHighlighter {\n\treturn {\n\t\thighlight: highlightWithMantleShiki,\n\t};\n}\n\nexport {\n\tcreateMantleServerSyntaxHighlighter,\n\textractHighlightedCodeInnerHtml,\n\tgetMantleShikiHighlighter,\n\thighlightWithMantleShiki,\n\tmantleHighlightCacheMaxEntries,\n\tmantleShikiLanguageGrammarIds,\n\tmantleShikiThemeName,\n};\nexport type { MantleHighlightInput, MantleHighlightResult, MantleServerHighlighter };\n"],"mappings":";;;;;AASA,SAAS,eAAqB,YAAoC;AACjE,KAAI,CAAC,OAAO,UAAU,WAAW,IAAI,cAAc,EAClD,OAAM,IAAI,MAAM,mDAAmD;CAGpE,MAAM,sBAAM,IA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1D,MAAI,YAAY,KACf,OAAM,IAAI,MAAM,uDAAuD;AASxE,SAAO;GACN,MAAM;GACN,MATY,wBAAwB;IACpC;IACA,MAAM;IACN;IACA;IACA,CAAC;GAKD,UAAU;GACV;GACA;GACA;GACA;KACE,CAAC,OAAO,UAAU;AACrB,uBAAqB,OAAO,SAAS;AACrC,QAAM;GACL;AAEF,sBAAqB,IAAI,UAAU,QAAQ;AAC3C,QAAO;;;;;AAMR,SAAS,sCAA+D;AACvE,QAAO,EACN,WAAW,0BACX"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ngrok/mantle-server-syntax-highlighter",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.4",
|
|
4
4
|
"description": "Server-side syntax highlighting for @ngrok/mantle, powered by Shiki.",
|
|
5
5
|
"homepage": "https://mantle.ngrok.com",
|
|
6
6
|
"license": "MIT",
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
"tsdown": "0.21.7",
|
|
32
32
|
"typescript": "6.0.2",
|
|
33
33
|
"@cfg/tsconfig": "1.0.0",
|
|
34
|
-
"@ngrok/mantle": "0.68.
|
|
34
|
+
"@ngrok/mantle": "0.68.3"
|
|
35
35
|
},
|
|
36
36
|
"peerDependencies": {
|
|
37
37
|
"@ngrok/mantle": "*"
|