Skip to content

Commit d77bcc4

Browse files
committed
Switch back to shiki
Now that `shikiji` is merged back into `shiki`, we can switch back to using it.
1 parent dd37557 commit d77bcc4

9 files changed

+34
-56
lines changed

package-lock.json

Lines changed: 11 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@
3030
"ansi-regex": "^6.0.1",
3131
"chalk": "^5.3.0",
3232
"diff": "^5.1.0",
33-
"shikiji": "^0.9.13",
33+
"shiki": "^1.10.1",
3434
"terminal-size": "^4.0.0",
3535
"wcwidth": "^1.0.1"
3636
},
3737
"scripts": {
3838
"clean": "rimraf './build'",
39-
"build": "npm run clean && patch-package && node ./scripts/build.mjs",
39+
"build": "npm run clean && node ./scripts/build.mjs",
4040
"build:dev": "npm run build -- --watch",
4141
"build:publish": "npm run build -- --prod",
4242
"test": "cross-env ./node_modules/.bin/tsc && cross-env NODE_OPTIONS=\"--experimental-vm-modules --no-warnings\" ./node_modules/.bin/jest",
@@ -55,7 +55,6 @@
5555
"esbuild": "^0.17.18",
5656
"jest": "^29.7.0",
5757
"nodemon": "^2.0.7",
58-
"patch-package": "^8.0.0",
5958
"prettier": "^2.2.1",
6059
"rimraf": "^5.0.5",
6160
"ts-jest": "^29.1.0",

patches/shikiji-core+0.9.13.patch

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/context.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as shikiji from 'shikiji';
1+
import * as shiki from 'shiki';
22
import { Config } from './getConfig';
33
import { FormattedString, T } from './formattedString';
44
import { ChalkInstance } from 'chalk';
@@ -11,7 +11,7 @@ export type Context = Config & {
1111
CHALK: ChalkInstance;
1212
SCREEN_WIDTH: number;
1313
HORIZONTAL_SEPARATOR: FormattedString;
14-
HIGHLIGHTER?: shikiji.Highlighter;
14+
HIGHLIGHTER?: shiki.Highlighter;
1515
};
1616

1717
export async function getContextForConfig(
@@ -27,7 +27,7 @@ export async function getContextForConfig(
2727

2828
let HIGHLIGHTER = undefined;
2929
if (config.SYNTAX_HIGHLIGHTING_THEME) {
30-
HIGHLIGHTER = await shikiji.getHighlighter({
30+
HIGHLIGHTER = await shiki.createHighlighter({
3131
themes: [config.SYNTAX_HIGHLIGHTING_THEME],
3232
langs: [],
3333
});

src/formatAndFitHunkLine.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,12 @@ export async function* formatAndFitHunkLine(
6767
let isFirstLine = true;
6868
const formattedLine = T().appendString(lineText);
6969
highlightChangesInLine(context, linePrefix, formattedLine, changes);
70-
if (context.HIGHLIGHTER) {
70+
if (context.HIGHLIGHTER && context.SYNTAX_HIGHLIGHTING_THEME) {
7171
await highlightSyntaxInLine(
7272
formattedLine,
7373
fileName,
74-
context.HIGHLIGHTER
74+
context.HIGHLIGHTER,
75+
context.SYNTAX_HIGHLIGHTING_THEME
7576
);
7677
}
7778

src/getConfig.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { GitConfig } from './getGitConfig';
22
import { Theme, loadTheme } from './themes';
3-
import * as shikiji from 'shikiji';
3+
import * as shiki from 'shiki';
44

55
export type Config = Theme & {
66
MIN_LINE_WIDTH: number;
@@ -24,6 +24,6 @@ export function getConfig(gitConfig: GitConfig): Config {
2424
...theme,
2525
...gitConfig,
2626
SYNTAX_HIGHLIGHTING_THEME: (theme.SYNTAX_HIGHLIGHTING_THEME ??
27-
gitConfig.SYNTAX_HIGHLIGHTING_THEME) as shikiji.BundledTheme,
27+
gitConfig.SYNTAX_HIGHLIGHTING_THEME) as shiki.BundledTheme,
2828
};
2929
}

src/highlightSyntaxInLine.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import * as shikiji from 'shikiji';
1+
import * as shiki from 'shiki';
22
import { T } from './formattedString';
33
import { highlightSyntaxInLine } from './highlightSyntaxInLine';
44

55
test('highlighting should load languages on-demand', async () => {
66
const string = 'int main {}';
77
const referenceString = T().appendString(string);
88

9-
const highlighter = await shikiji.getHighlighter({
9+
const highlighter = await shiki.createHighlighter({
1010
themes: ['dark-plus'],
1111
langs: [],
1212
});

src/highlightSyntaxInLine.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
11
import path from 'path';
2-
import * as shikiji from 'shikiji';
2+
import * as shiki from 'shiki';
33
import { FormattedString } from './formattedString';
44
import { parseColorDefinition, ThemeColor } from './themes';
55
export type HighlightedText = [string, ThemeColor | null];
66

77
export async function highlightSyntaxInLine(
88
line: FormattedString,
99
fileName: string,
10-
highlighter: shikiji.Highlighter
10+
highlighter: shiki.Highlighter,
11+
theme: shiki.BundledTheme
1112
): Promise<void> {
12-
const language = path.extname(fileName).slice(1) as shikiji.BundledLanguage;
13-
if (!shikiji.bundledLanguages[language]) {
13+
const language = path.extname(fileName).slice(1) as shiki.BundledLanguage;
14+
if (!shiki.bundledLanguages[language]) {
1415
return;
1516
}
1617

1718
await highlighter.loadLanguage(language);
1819

19-
const [tokens] = highlighter.codeToThemedTokens(line.getString(), {
20+
const { tokens } = highlighter.codeToTokens(line.getString(), {
2021
includeExplanation: false,
2122
lang: language,
23+
theme,
2224
});
2325

2426
let index = 0;
25-
for (const { content, color } of tokens) {
27+
for (const { content, color } of tokens.flat()) {
2628
if (color) {
2729
const syntaxColor = parseColorDefinition({
2830
color: color,

src/themes.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as assert from 'assert';
22
import * as path from 'path';
33
import * as fs from 'fs';
44
import { fileURLToPath } from 'url';
5-
import * as shikiji from 'shikiji';
5+
import * as shiki from 'shiki';
66

77
const THEMES_DIR = path.resolve(
88
path.dirname(fileURLToPath(import.meta.url)),
@@ -55,7 +55,7 @@ export enum ThemeColorName {
5555
}
5656

5757
export type ThemeDefinition = {
58-
SYNTAX_HIGHLIGHTING_THEME?: shikiji.BundledTheme;
58+
SYNTAX_HIGHLIGHTING_THEME?: shiki.BundledTheme;
5959
} & {
6060
[key in ThemeColorName]: ColorDefinition;
6161
};
@@ -147,7 +147,7 @@ export function reduceThemeColors(colors: ThemeColor[]): ThemeColor {
147147
}
148148

149149
export type Theme = {
150-
SYNTAX_HIGHLIGHTING_THEME?: shikiji.BundledTheme;
150+
SYNTAX_HIGHLIGHTING_THEME?: shiki.BundledTheme;
151151
} & {
152152
[key in ThemeColorName]: ThemeColor;
153153
};

0 commit comments

Comments
 (0)