Core: Enable experiment Shiki JS engine by default

This commit is contained in:
fuma-nama
2024-09-01 22:17:48 +08:00
parent 3401e3270e
commit 3b561702ad
14 changed files with 436 additions and 341 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'fumadocs-core': patch
---
Enable experiment Shiki JS engine by default
+4 -4
View File
@@ -18,7 +18,7 @@
"@radix-ui/react-popover": "^1.1.1",
"@radix-ui/react-tooltip": "^1.1.2",
"@resvg/resvg-js": "^2.6.2",
"@shikijs/transformers": "^1.15.1",
"@shikijs/transformers": "^1.15.2",
"algoliasearch": "^4.24.0",
"class-variance-authority": "^0.7.0",
"fumadocs-core": "workspace:*",
@@ -38,7 +38,7 @@
"rehype-katex": "^7.0.1",
"remark-math": "^6.0.0",
"satori": "^0.10.14",
"shiki": "^1.15.1",
"shiki": "^1.15.2",
"tailwind-merge": "^2.5.2",
"zod": "^3.23.8"
},
@@ -46,14 +46,14 @@
"@next/bundle-analyzer": "^14.2.7",
"@next/eslint-plugin-next": "^14.2.7",
"@types/mdx": "^2.0.13",
"@types/node": "22.5.1",
"@types/node": "22.5.2",
"@types/react": "^18.3.5",
"@types/react-dom": "^18.3.0",
"autoprefixer": "^10.4.20",
"eslint-config-custom": "workspace:*",
"glslify": "^7.1.1",
"glslx": "^0.3.0",
"postcss": "^8.4.41",
"postcss": "^8.4.42",
"tailwindcss": "^3.4.10",
"tailwindcss-animate": "^1.0.7",
"tsconfig": "workspace:*",
+1 -1
View File
@@ -14,7 +14,7 @@
"fumadocs-openapi": "workspace:*",
"fumadocs-ui": "workspace:*",
"next": "^14.2.7",
"postcss": "^8.4.41",
"postcss": "^8.4.42",
"react": "18.3.1",
"react-dom": "18.3.1",
"tailwindcss": "^3.4.10",
+1 -1
View File
@@ -47,7 +47,7 @@
"devDependencies": {
"@content-collections/core": "^0.6.4",
"@content-collections/mdx": "^0.1.3",
"@types/node": "22.5.1",
"@types/node": "22.5.2",
"eslint-config-custom": "workspace:*",
"fumadocs-core": "workspace:*",
"tsconfig": "workspace:*",
+4 -4
View File
@@ -134,8 +134,8 @@
},
"dependencies": {
"@formatjs/intl-localematcher": "^0.5.4",
"@shikijs/rehype": "^1.15.1",
"@shikijs/transformers": "^1.15.1",
"@shikijs/rehype": "^1.15.2",
"@shikijs/transformers": "^1.15.2",
"flexsearch": "0.7.21",
"github-slugger": "^2.0.0",
"image-size": "^1.1.1",
@@ -146,7 +146,7 @@
"remark-gfm": "^4.0.0",
"remark-mdx": "^3.0.1",
"scroll-into-view-if-needed": "^3.1.0",
"shiki": "^1.15.1",
"shiki": "^1.15.2",
"swr": "^2.2.5",
"unist-util-visit": "^5.0.0"
},
@@ -158,7 +158,7 @@
"@types/hast": "^3.0.4",
"@types/mdast": "^4.0.3",
"@types/negotiator": "^0.6.3",
"@types/node": "22.5.1",
"@types/node": "22.5.2",
"@types/react": "^18.3.5",
"@types/react-dom": "^18.3.0",
"algoliasearch": "^4.24.0",
+41 -3
View File
@@ -1,11 +1,18 @@
import type { Root, RootContent } from 'hast';
import rehypeShiki, { type RehypeShikiOptions } from '@shikijs/rehype';
import { type RehypeShikiOptions } from '@shikijs/rehype';
import rehypeShikiFromHighlighter from '@shikijs/rehype/core';
import {
transformerNotationHighlight,
transformerNotationWordHighlight,
} from '@shikijs/transformers';
import type { Processor, Transformer } from 'unified';
import type { ShikiTransformer } from 'shiki';
import {
getSingletonHighlighter,
createJavaScriptRegexEngine,
type ShikiTransformer,
bundledLanguages,
type BuiltinTheme,
} from 'shiki';
import type { IconOptions, CodeBlockIcon } from './transformer-icon';
import { transformerIcon } from './transformer-icon';
@@ -64,6 +71,7 @@ export const rehypeCodeDefaultOptions: RehypeCodeOptions = {
return replaced;
},
experimentalJSEngine: true,
};
export type RehypeCodeOptions = RehypeShikiOptions & {
@@ -83,6 +91,11 @@ export type RehypeCodeOptions = RehypeShikiOptions & {
* @defaultValue true
*/
tab?: false;
/**
* @defaultValue true
*/
experimentalJSEngine?: boolean;
};
/**
@@ -133,7 +146,32 @@ export function rehypeCode(
codeOptions.transformers = [...codeOptions.transformers, transformerTab()];
}
return rehypeShiki.call(this, codeOptions) as Transformer<Root, Root>;
let themeItems: unknown[] = [];
if ('themes' in codeOptions) {
themeItems = Object.values(codeOptions.themes);
} else if ('theme' in codeOptions) {
themeItems = [codeOptions.theme];
}
const highlighter = getSingletonHighlighter({
engine: codeOptions.experimentalJSEngine
? createJavaScriptRegexEngine()
: undefined,
themes: themeItems.filter(Boolean) as BuiltinTheme[],
langs: codeOptions.langs ?? Object.keys(bundledLanguages),
});
return async (tree, file) => {
const transformer = rehypeShikiFromHighlighter(
await highlighter,
codeOptions,
);
await transformer(tree, file, () => {
// nothing
});
};
}
function transformerTab(): ShikiTransformer {
+2 -2
View File
@@ -9,12 +9,12 @@
"@content-collections/mdx": "^0.1.3",
"@content-collections/next": "^0.2.0",
"@types/mdx": "^2.0.13",
"@types/node": "22.5.1",
"@types/node": "22.5.2",
"@types/react": "^18.3.5",
"@types/react-dom": "^18.3.0",
"autoprefixer": "^10.4.20",
"next": "^14.2.7",
"postcss": "^8.4.41",
"postcss": "^8.4.42",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"tailwindcss": "^3.4.10",
+1 -1
View File
@@ -33,7 +33,7 @@
},
"devDependencies": {
"@types/cross-spawn": "^6.0.6",
"@types/node": "22.5.1",
"@types/node": "22.5.2",
"eslint-config-custom": "workspace:*",
"fast-glob": "^3.3.1",
"tsconfig": "workspace:*"
+1 -1
View File
@@ -38,7 +38,7 @@
"@types/estree": "^1.0.5",
"@types/hast": "^3.0.4",
"@types/mdast": "^4.0.3",
"@types/node": "22.5.1",
"@types/node": "22.5.2",
"eslint-config-custom": "workspace:*",
"remark": "^15.0.0",
"tsconfig": "workspace:*",
+1 -1
View File
@@ -72,7 +72,7 @@
"devDependencies": {
"@types/mdx": "^2.0.13",
"@types/micromatch": "^4.0.9",
"@types/node": "22.5.1",
"@types/node": "22.5.2",
"@types/react": "^18.3.5",
"@types/ws": "^8.5.12",
"eslint-config-custom": "workspace:*",
+4 -4
View File
@@ -52,7 +52,7 @@
},
"dependencies": {
"@apidevtools/json-schema-ref-parser": "^11.7.0",
"@fumari/json-schema-to-typescript": "^1.0.1",
"@fumari/json-schema-to-typescript": "^1.1.0",
"@mdx-js/mdx": "^3.0.1",
"@radix-ui/react-select": "^2.1.1",
"@radix-ui/react-slot": "^1.1.0",
@@ -68,15 +68,15 @@
"openapi-sampler": "^1.5.1",
"react-hook-form": "^7.53.0",
"remark": "^15.0.0",
"shiki": "^1.15.1",
"shiki": "^1.15.2",
"swr": "^2.2.5"
},
"devDependencies": {
"@types/js-yaml": "^4.0.9",
"@types/node": "22.5.1",
"@types/node": "22.5.2",
"@types/openapi-sampler": "^1.0.3",
"@types/react": "^18.3.5",
"bunchee": "^5.3.2",
"bunchee": "^5.4.0",
"eslint-config-custom": "workspace:*",
"next": "^14.2.7",
"openapi-types": "^12.1.3",
+2 -2
View File
@@ -25,7 +25,7 @@
"types:check": "tsc --noEmit"
},
"dependencies": {
"@shikijs/twoslash": "^1.15.1",
"@shikijs/twoslash": "^1.15.2",
"mdast-util-from-markdown": "^2.0.1",
"mdast-util-gfm": "^3.0.0",
"mdast-util-to-hast": "^13.2.0"
@@ -35,7 +35,7 @@
"@types/mdast": "^4.0.3",
"eslint-config-custom": "workspace:*",
"fumadocs-ui": "workspace:*",
"shiki": "^1.15.1",
"shiki": "^1.15.2",
"tsconfig": "workspace:*"
},
"peerDependencies": {
+1 -1
View File
@@ -131,7 +131,7 @@
"algoliasearch": "^4.24.0",
"eslint-config-custom": "workspace:*",
"next": "^14.2.7",
"postcss": "^8.4.41",
"postcss": "^8.4.42",
"postcss-cli": "^11.0.0",
"postcss-lightningcss": "^1.0.1",
"tailwindcss": "^3.4.10",
+368 -316
View File
File diff suppressed because it is too large Load Diff