Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use slim shiki build and pre-compiled languages #1519

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
336 changes: 273 additions & 63 deletions package-lock.json

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@
},
"dependencies": {
"@emoji-mart/data": "^1.2.1",
"@shikijs/core": "3.2.1",
"@shikijs/engine-javascript": "3.2.1",
"@shikijs/langs": "3.2.1",
"@shikijs/langs-precompiled": "3.2.1",
"@shikijs/themes": "3.2.1",
"@shikijs/types": "3.2.1",
"@tiptap/core": "^2.11.5",
"@tiptap/extension-bold": "^2.11.5",
"@tiptap/extension-code": "^2.11.5",
Expand Down Expand Up @@ -99,7 +105,6 @@
"remark-parse": "^10.0.1",
"remark-rehype": "^10.1.0",
"remark-stringify": "^10.0.2",
"shiki": "^1.22.0",
"unified": "^10.1.2",
"uuid": "^8.3.2",
"y-prosemirror": "1.2.17",
Expand Down
21 changes: 11 additions & 10 deletions packages/core/src/blocks/CodeBlockContent/CodeBlockContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@ import { InputRule, isTextSelection } from "@tiptap/core";
import { TextSelection } from "@tiptap/pm/state";
import { createHighlightPlugin, Parser } from "prosemirror-highlight";
import { createParser } from "prosemirror-highlight/shiki";
import {
BundledLanguage,
bundledLanguagesInfo,
createHighlighter,
Highlighter,
} from "shiki";
import {
createBlockSpecFromStronglyTypedTiptapNode,
createStronglyTypedTiptapNode,
Expand All @@ -18,6 +12,12 @@ import {
defaultSupportedLanguages,
SupportedLanguageConfig,
} from "./defaultSupportedLanguages.js";
import {
bundledLanguages,
Highlighter,
BundledLanguage,
createHighlighter,
} from "./shiki.bundle.js";

interface CodeBlockOptions {
defaultLanguage: string;
Expand All @@ -32,7 +32,7 @@ export const shikiHighlighterPromiseSymbol = Symbol.for(
export const defaultCodeBlockPropSchema = {
language: {
default: "javascript",
values: [...defaultSupportedLanguages.map((lang) => lang.id)],
values: defaultSupportedLanguages.map((lang) => lang.id),
},
} satisfies PropSchema;

Expand Down Expand Up @@ -218,8 +218,8 @@ const CodeBlockContent = createStronglyTypedTiptapNode({
globalThisForShiki[shikiHighlighterPromiseSymbol] =
globalThisForShiki[shikiHighlighterPromiseSymbol] ||
createHighlighter({
themes: ["github-dark"],
langs: [],
themes: ["github-dark"],
});

return globalThisForShiki[shikiHighlighterPromiseSymbol].then(
Expand All @@ -236,14 +236,15 @@ const CodeBlockContent = createStronglyTypedTiptapNode({
language !== "text" &&
!highlighter.getLoadedLanguages().includes(language) &&
supportedLanguages.find(({ id }) => id === language) &&
bundledLanguagesInfo.find(({ id }) => id === language)
language in bundledLanguages
) {
return highlighter.loadLanguage(language as BundledLanguage);
}

if (!parser) {
parser =
globalThisForShiki[shikiParserSymbol] || createParser(highlighter);
globalThisForShiki[shikiParserSymbol] ||
createParser(highlighter as any);
globalThisForShiki[shikiParserSymbol] = parser;
}

Expand Down
158 changes: 51 additions & 107 deletions packages/core/src/blocks/CodeBlockContent/defaultSupportedLanguages.ts
Original file line number Diff line number Diff line change
@@ -1,116 +1,60 @@
import { bundledLanguagesInfo } from "shiki";

export type SupportedLanguageConfig = {
id: string;
name: string;
match: string[];
};

export const defaultSupportedLanguages: SupportedLanguageConfig[] = [
{
id: "text",
name: "Plain Text",
match: ["text", "txt", "plain"],
},
...bundledLanguagesInfo
.filter((lang) => {
return [
"c",
"cpp",
"css",
"glsl",
"graphql",
"haml",
"html",
"java",
"javascript",
"json",
"jsonc",
"jsonl",
"jsx",
"julia",
"less",
"markdown",
"mdx",
"php",
"postcss",
"pug",
"python",
"r",
"regexp",
"sass",
"scss",
"shellscript",
"sql",
"svelte",
"typescript",
"vue",
"vue-html",
"wasm",
"wgsl",
"xml",
"yaml",
].includes(lang.id);
})
.map((lang) => ({
match: [lang.id, ...(lang.aliases || [])],
id: lang.id,
name: lang.name,
})),
{ id: "text", name: "Plain Text", match: ["text", "txt", "plain"] },
{ match: ["c"], id: "c", name: "C" },
{ match: ["cpp", "c++"], id: "cpp", name: "C++" },
{ match: ["css"], id: "css", name: "CSS" },
{ match: ["glsl"], id: "glsl", name: "GLSL" },
{ match: ["graphql", "gql"], id: "graphql", name: "GraphQL" },
{ match: ["haml"], id: "haml", name: "Ruby Haml" },
{ match: ["html"], id: "html", name: "HTML" },
{ match: ["java"], id: "java", name: "Java" },
{ match: ["javascript", "js"], id: "javascript", name: "JavaScript" },
{ match: ["json"], id: "json", name: "JSON" },
{ match: ["jsonc"], id: "jsonc", name: "JSON with Comments" },
{ match: ["jsonl"], id: "jsonl", name: "JSON Lines" },
{ match: ["jsx"], id: "jsx", name: "JSX" },
{ match: ["julia", "jl"], id: "julia", name: "Julia" },
{ match: ["less"], id: "less", name: "Less" },
{ match: ["markdown", "md"], id: "markdown", name: "Markdown" },
{ match: ["mdx"], id: "mdx", name: "MDX" },
{ match: ["php"], id: "php", name: "PHP" },
{ match: ["postcss"], id: "postcss", name: "PostCSS" },
{ match: ["pug", "jade"], id: "pug", name: "Pug" },
{ match: ["python", "py"], id: "python", name: "Python" },
{ match: ["r"], id: "r", name: "R" },
{ match: ["regexp", "regex"], id: "regexp", name: "RegExp" },
{ match: ["sass"], id: "sass", name: "Sass" },
{ match: ["scss"], id: "scss", name: "SCSS" },
{
match: ["shellscript", "bash", "sh", "shell", "zsh"],
id: "shellscript",
name: "Shell",
},
{ match: ["sql"], id: "sql", name: "SQL" },
{ match: ["svelte"], id: "svelte", name: "Svelte" },
{ match: ["typescript", "ts"], id: "typescript", name: "TypeScript" },
{ match: ["vue"], id: "vue", name: "Vue" },
{ match: ["vue-html"], id: "vue-html", name: "Vue HTML" },
{ match: ["wasm"], id: "wasm", name: "WebAssembly" },
{ match: ["wgsl"], id: "wgsl", name: "WGSL" },
{ match: ["xml"], id: "xml", name: "XML" },
{ match: ["yaml", "yml"], id: "yaml", name: "YAML" },
{ id: "tsx", name: "TSX", match: ["tsx", "typescriptreact"] },
{
id: "haskell",
name: "Haskell",
match: ["haskell", "hs"],
},
{
id: "csharp",
name: "C#",
match: ["c#", "csharp", "cs"],
},
{
id: "latex",
name: "LaTeX",
match: ["latex"],
},
{
id: "lua",
name: "Lua",
match: ["lua"],
},
{
id: "mermaid",
name: "Mermaid",
match: ["mermaid", "mmd"],
},
{
id: "ruby",
name: "Ruby",
match: ["ruby", "rb"],
},
{
id: "rust",
name: "Rust",
match: ["rust", "rs"],
},
{
id: "scala",
name: "Scala",
match: ["scala"],
},
{
id: "swift",
name: "Swift",
match: ["swift"],
},
{
id: "kotlin",
name: "Kotlin",
match: ["kotlin", "kt", "kts"],
},
{
id: "objective-c",
name: "Objective C",
match: ["objective-c", "objc"],
},
{ id: "haskell", name: "Haskell", match: ["haskell", "hs"] },
{ id: "csharp", name: "C#", match: ["c#", "csharp", "cs"] },
{ id: "latex", name: "LaTeX", match: ["latex"] },
{ id: "lua", name: "Lua", match: ["lua"] },
{ id: "mermaid", name: "Mermaid", match: ["mermaid", "mmd"] },
{ id: "ruby", name: "Ruby", match: ["ruby", "rb"] },
{ id: "rust", name: "Rust", match: ["rust", "rs"] },
{ id: "scala", name: "Scala", match: ["scala"] },
{ id: "swift", name: "Swift", match: ["swift"] },
{ id: "kotlin", name: "Kotlin", match: ["kotlin", "kt", "kts"] },
{ id: "objective-c", name: "Objective C", match: ["objective-c", "objc"] },
];
129 changes: 129 additions & 0 deletions packages/core/src/blocks/CodeBlockContent/shiki.bundle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/* Generate by @shikijs/codegen */
import type {
DynamicImportLanguageRegistration,
DynamicImportThemeRegistration,
HighlighterGeneric,
} from "@shikijs/types";
import {
createSingletonShorthands,
createdBundledHighlighter,
} from "@shikijs/core";
import { createJavaScriptRawEngine } from "@shikijs/engine-javascript/raw";

type BundledLanguage = keyof typeof bundledLanguages;
type BundledTheme = "github-dark";
type Highlighter = HighlighterGeneric<BundledLanguage, BundledTheme>;

const bundledLanguages = {
c: () => import("@shikijs/langs-precompiled/c"),
cpp: () => import("@shikijs/langs-precompiled/cpp"),
"c++": () => import("@shikijs/langs-precompiled/cpp"),
css: () => import("@shikijs/langs-precompiled/css"),
glsl: () => import("@shikijs/langs-precompiled/glsl"),
graphql: () => import("@shikijs/langs-precompiled/graphql"),
gql: () => import("@shikijs/langs-precompiled/graphql"),
haml: () => import("@shikijs/langs-precompiled/haml"),
html: () => import("@shikijs/langs-precompiled/html"),
java: () => import("@shikijs/langs-precompiled/java"),
javascript: () => import("@shikijs/langs-precompiled/javascript"),
js: () => import("@shikijs/langs-precompiled/javascript"),
json: () => import("@shikijs/langs-precompiled/json"),
jsonc: () => import("@shikijs/langs-precompiled/jsonc"),
jsonl: () => import("@shikijs/langs-precompiled/jsonl"),
jsx: () => import("@shikijs/langs-precompiled/jsx"),
julia: () => import("@shikijs/langs-precompiled/julia"),
jl: () => import("@shikijs/langs-precompiled/julia"),
less: () => import("@shikijs/langs-precompiled/less"),
markdown: () => import("@shikijs/langs-precompiled/markdown"),
md: () => import("@shikijs/langs-precompiled/markdown"),
mdx: () => import("@shikijs/langs-precompiled/mdx"),
php: () => import("@shikijs/langs-precompiled/php"),
postcss: () => import("@shikijs/langs-precompiled/postcss"),
pug: () => import("@shikijs/langs-precompiled/pug"),
jade: () => import("@shikijs/langs-precompiled/pug"),
python: () => import("@shikijs/langs-precompiled/python"),
py: () => import("@shikijs/langs-precompiled/python"),
r: () => import("@shikijs/langs-precompiled/r"),
regexp: () => import("@shikijs/langs-precompiled/regexp"),
regex: () => import("@shikijs/langs-precompiled/regexp"),
sass: () => import("@shikijs/langs-precompiled/sass"),
scss: () => import("@shikijs/langs-precompiled/scss"),
shellscript: () => import("@shikijs/langs-precompiled/shellscript"),
bash: () => import("@shikijs/langs-precompiled/shellscript"),
sh: () => import("@shikijs/langs-precompiled/shellscript"),
shell: () => import("@shikijs/langs-precompiled/shellscript"),
zsh: () => import("@shikijs/langs-precompiled/shellscript"),
sql: () => import("@shikijs/langs-precompiled/sql"),
svelte: () => import("@shikijs/langs-precompiled/svelte"),
typescript: () => import("@shikijs/langs-precompiled/typescript"),
ts: () => import("@shikijs/langs-precompiled/typescript"),
vue: () => import("@shikijs/langs-precompiled/vue"),
"vue-html": () => import("@shikijs/langs-precompiled/vue-html"),
wasm: () => import("@shikijs/langs-precompiled/wasm"),
wgsl: () => import("@shikijs/langs-precompiled/wgsl"),
xml: () => import("@shikijs/langs-precompiled/xml"),
yaml: () => import("@shikijs/langs-precompiled/yaml"),
yml: () => import("@shikijs/langs-precompiled/yaml"),
tsx: () => import("@shikijs/langs-precompiled/tsx"),
typescriptreact: () => import("@shikijs/langs-precompiled/tsx"),
haskell: () => import("@shikijs/langs-precompiled/haskell"),
hs: () => import("@shikijs/langs-precompiled/haskell"),
"c#": () => import("@shikijs/langs-precompiled/csharp"),
csharp: () => import("@shikijs/langs-precompiled/csharp"),
cs: () => import("@shikijs/langs-precompiled/csharp"),
latex: () => import("@shikijs/langs-precompiled/latex"),
lua: () => import("@shikijs/langs-precompiled/lua"),
mermaid: () => import("@shikijs/langs-precompiled/mermaid"),
mmd: () => import("@shikijs/langs-precompiled/mermaid"),
ruby: () => import("@shikijs/langs-precompiled/ruby"),
rb: () => import("@shikijs/langs-precompiled/ruby"),
rust: () => import("@shikijs/langs-precompiled/rust"),
rs: () => import("@shikijs/langs-precompiled/rust"),
scala: () => import("@shikijs/langs-precompiled/scala"),
// Swift does not support pre-compilation right now
swift: () => import("@shikijs/langs/swift"),
kotlin: () => import("@shikijs/langs-precompiled/kotlin"),
kt: () => import("@shikijs/langs-precompiled/kotlin"),
kts: () => import("@shikijs/langs-precompiled/kotlin"),
"objective-c": () => import("@shikijs/langs-precompiled/objective-c"),
objc: () => import("@shikijs/langs-precompiled/objective-c"),
} satisfies Record<string, DynamicImportLanguageRegistration>;

const bundledThemes = {
"github-dark": () => import("@shikijs/themes/github-dark"),
} as Record<BundledTheme, DynamicImportThemeRegistration>;

const createHighlighter = /* @__PURE__ */ createdBundledHighlighter<
BundledLanguage,
BundledTheme
>({
langs: bundledLanguages,
themes: bundledThemes,
engine: () => createJavaScriptRawEngine(),
});

const {
codeToHtml,
codeToHast,
codeToTokensBase,
codeToTokens,
codeToTokensWithThemes,
getSingletonHighlighter,
getLastGrammarState,
} = /* @__PURE__ */ createSingletonShorthands<BundledLanguage, BundledTheme>(
createHighlighter
);

export {
bundledLanguages,
bundledThemes,
codeToHast,
codeToHtml,
codeToTokens,
codeToTokensBase,
codeToTokensWithThemes,
createHighlighter,
getLastGrammarState,
getSingletonHighlighter,
};
export type { BundledLanguage, BundledTheme, Highlighter };
2 changes: 1 addition & 1 deletion packages/core/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"useDefineForClassFields": true,
"module": "ESNext",
"lib": ["ESNext", "DOM"],
"moduleResolution": "Node",
"moduleResolution": "bundler",
"jsx": "react-jsx",
"strict": true,
"sourceMap": true,
Expand Down
Loading
Loading