-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add syntax highlighting with Shikiji
At long last, we have syntax highlighting with acceptable tradeoffs. Shikiji is intended to be the next version of the longstanding Shiki. It's fundamentally better in that it simply uses ESM and dynamic imports to break out the wasm blob and the many language grammars into their own JS assets, instead of an ad-hoc asset fetching meechanism. This means it does the right thing when integrating with SvelteKit's vite build tooling, for free. I have had to make minor patches to Shikiji to get it to have the exact APIs that I want. I could have used the stock ones but they would result in a jankier (and slightly less performant) integration. I will propose upstreaming these changes, but I doubt they will all be acceptable. The exact approach to highlighting is discussed in the new LineGroup component. The data returned by content-parser in the API also had to change to more easily merge with Shikiji's highlights.
- Loading branch information
Showing
15 changed files
with
824 additions
and
599 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"neogrok": minor | ||
--- | ||
|
||
Add syntax highlighting with shikiji |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
diff --git a/dist/chunk-types.d.mts b/dist/chunk-types.d.mts | ||
index 2992357a1d0e670afce9ec11133a58a4e17593da..788cab9adb4134796ab6cd91fa9fd25b3a971dc5 100644 | ||
--- a/dist/chunk-types.d.mts | ||
+++ b/dist/chunk-types.d.mts | ||
@@ -1124,10 +1124,8 @@ interface ThemedTokenExplanation { | ||
interface ThemedToken extends TokenStyles, TokenBase { | ||
} | ||
interface TokenBase { | ||
- /** | ||
- * The content of the token | ||
- */ | ||
- content: string; | ||
+ start: number; | ||
+ end: number; | ||
/** | ||
* Explanation of | ||
* | ||
diff --git a/dist/index.mjs b/dist/index.mjs | ||
index 6f891f5e37470cb673f8bcb8914c44634ca47091..1b52e8c87fa19d739e66f824715a18f37ae05fc6 100644 | ||
--- a/dist/index.mjs | ||
+++ b/dist/index.mjs | ||
@@ -3709,7 +3709,7 @@ function dimColor(color) { | ||
function codeToThemedTokens(internal, code, options = {}) { | ||
const { lang = 'text', theme: themeName = internal.getLoadedThemes()[0], } = options; | ||
if (isPlaintext(lang)) { | ||
- const lines = code.split(/\r\n|\r|\n/); | ||
+ const lines = code; | ||
return [...lines.map(line => [{ content: line }])]; | ||
} | ||
const { theme, colorMap } = internal.setTheme(themeName); | ||
@@ -3723,7 +3723,7 @@ function tokenizeWithTheme(code, grammar, theme, colorMap, options) { | ||
...theme.colorReplacements, | ||
...options?.colorReplacements, | ||
}; | ||
- const lines = code.split(/\r\n|\r|\n/); | ||
+ const lines = code; | ||
let ruleStack = INITIAL; | ||
let actual = []; | ||
const final = []; | ||
@@ -3754,7 +3754,8 @@ function tokenizeWithTheme(code, grammar, theme, colorMap, options) { | ||
const foregroundColor = applyColorReplacements(colorMap[foreground], colorReplacements); | ||
const fontStyle = StackElementMetadata.getFontStyle(metadata); | ||
const token = { | ||
- content: line.substring(startIndex, nextStartIndex), | ||
+ start: startIndex, | ||
+ end: nextStartIndex, | ||
color: foregroundColor, | ||
fontStyle, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
diff --git a/dist/bundle-full.d.mts b/dist/bundle-full.d.mts | ||
index b8e655052e4a1b6e10e617eb06008a0782ff3a70..db1121f519c64bcada055f10f07500c27afca436 100644 | ||
--- a/dist/bundle-full.d.mts | ||
+++ b/dist/bundle-full.d.mts | ||
@@ -12,7 +12,7 @@ type Highlighter = HighlighterGeneric<BundledLanguage, BundledTheme>; | ||
declare const getHighlighter: shikiji_core.GetHighlighterFactory<BundledLanguage, BundledTheme>; | ||
declare const codeToHtml: (code: string, options: shikiji_core.CodeToHastOptions<BundledLanguage, BundledTheme>) => Promise<string>; | ||
declare const codeToHast: (code: string, options: shikiji_core.CodeToHastOptions<BundledLanguage, BundledTheme>) => Promise<Root>; | ||
-declare const codeToThemedTokens: (code: string, options: shikiji_core.RequireKeys<shikiji_core.CodeToThemedTokensOptions<BundledLanguage, BundledTheme>, "lang" | "theme">) => Promise<shikiji_core.ThemedToken[][]>; | ||
+declare const codeToThemedTokens: (code: string[], options: shikiji_core.RequireKeys<shikiji_core.CodeToThemedTokensOptions<BundledLanguage, BundledTheme>, "lang" | "theme">) => Promise<shikiji_core.ThemedToken[][]>; | ||
declare const codeToTokensWithThemes: (code: string, options: shikiji_core.RequireKeys<shikiji_core.CodeToTokensWithThemesOptions<BundledLanguage, BundledTheme>, "lang" | "themes">) => Promise<shikiji_core.ThemedTokenWithVariants[][]>; | ||
declare const getSingletonHighlighter: () => Promise<HighlighterGeneric<BundledLanguage, BundledTheme>>; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.