Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
},
"packageManager": "pnpm@11.22.0",
"dependencies": {
"@shikijs/themes": "^4.4.3",
"shiki": "^4.4.3"
}
}
2 changes: 1 addition & 1 deletion packages/comment-widget/src/comment-content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class CommentContent extends LitElement {
const content = codeblock.textContent || '';

try {
const { codeToHtml } = await import('shiki/bundle/full');
const { codeToHtml } = await import('./shiki-bundle');

const html = await codeToHtml(content, {
lang,
Expand Down
40 changes: 40 additions & 0 deletions packages/comment-widget/src/shiki-bundle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import githubDark from '@shikijs/themes/github-dark';
import {
type CodeToHastOptions,
type CreateHighlighterFactory,
createBundledHighlighter,
createSingletonShorthands,
guessEmbeddedLanguages,
type ShorthandsBundle,
type ThemeRegistration,
} from 'shiki/core';
import { createJavaScriptRegexEngine } from 'shiki/engine/javascript';
import { bundledLanguages } from 'shiki/langs';

type BundledLanguage = keyof typeof bundledLanguages;
type BundledTheme = 'github-dark';

const bundledThemes: Record<BundledTheme, ThemeRegistration> = {
'github-dark': githubDark,
};

const createHighlighter: CreateHighlighterFactory<
BundledLanguage,
BundledTheme
> = createBundledHighlighter({
langs: bundledLanguages,
themes: bundledThemes,
engine: () => createJavaScriptRegexEngine(),
});

const shorthands: ShorthandsBundle<BundledLanguage, BundledTheme> =
createSingletonShorthands(createHighlighter, { guessEmbeddedLanguages });

function codeToHtml(
code: string,
options: CodeToHastOptions<BundledLanguage, BundledTheme>
): Promise<string> {
return shorthands.codeToHtml(code, options);
}

export { bundledLanguages, bundledThemes, codeToHtml, createHighlighter };
37 changes: 37 additions & 0 deletions packages/comment-widget/tests/shiki-bundle.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import {
bundledLanguages,
bundledThemes,
codeToHtml,
createHighlighter,
} from '../src/shiki-bundle.ts';

test('highlights code with the JavaScript regex engine', async () => {
const html = await codeToHtml('const answer = 42;', {
lang: 'javascript',
theme: 'github-dark',
});

assert.match(html, /<pre class="shiki github-dark"/);
assert.match(html, /<span style="color:/);
});

test('provides the API used by the Tiptap Shiki extension', async () => {
assert.ok('javascript' in bundledLanguages);
assert.ok('github-dark' in bundledThemes);

const highlighter = await createHighlighter({
langs: ['javascript'],
themes: ['github-dark'],
});

assert.match(
highlighter.codeToHtml('const answer = 42;', {
lang: 'javascript',
theme: 'github-dark',
}),
/<span style="color:/
);
highlighter.dispose();
});
11 changes: 11 additions & 0 deletions packages/comment-widget/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
import { fileURLToPath, URL } from 'node:url';
import UnoCSS from 'unocss/vite';
import { defineConfig, type Plugin } from 'vite';
import dts from 'vite-plugin-dts';

export default defineConfig({
resolve: {
alias: [
{
find: /^shiki$/,
replacement: fileURLToPath(
new URL('./src/shiki-bundle.ts', import.meta.url)
),
},
],
},
plugins: [
dts() as Plugin,
UnoCSS({
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading