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

enhance(frontend): shiki v1に移行 #13138

Merged
merged 8 commits into from
Feb 2, 2024
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
2 changes: 1 addition & 1 deletion packages/frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"rollup": "4.9.6",
"sanitize-html": "2.11.0",
"sass": "1.70.0",
"shiki": "0.14.7",
"shiki": "1.0.0-beta.3",
"strict-event-emitter-types": "2.0.0",
"textarea-caret": "3.1.0",
"three": "0.160.1",
Expand Down
21 changes: 11 additions & 10 deletions packages/frontend/src/components/MkCode.core.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ SPDX-License-Identifier: AGPL-3.0-only

<!-- eslint-disable vue/no-v-html -->
<template>
<div :class="['codeBlockRoot', { 'codeEditor': codeEditor }]" v-html="html"></div>
<div :class="[$style.codeBlockRoot, { [$style.codeEditor]: codeEditor }]" v-html="html"></div>
</template>

<script lang="ts" setup>
import { ref, computed, watch } from 'vue';
import { BUNDLED_LANGUAGES } from 'shiki';
import type { Lang as ShikiLang } from 'shiki';
import { bundledLanguagesInfo } from 'shiki';
import type { BuiltinLanguage } from 'shiki';
import { getHighlighter } from '@/scripts/code-highlighter.js';

const props = defineProps<{
Expand All @@ -22,24 +22,25 @@ const props = defineProps<{

const highlighter = await getHighlighter();

const codeLang = ref<ShikiLang | 'aiscript'>('js');
const codeLang = ref<BuiltinLanguage | 'aiscript'>('js');
const html = computed(() => highlighter.codeToHtml(props.code, {
lang: codeLang.value,
theme: 'dark-plus',
}));

async function fetchLanguage(to: string): Promise<void> {
const language = to as ShikiLang;
const language = to as BuiltinLanguage;

// Check for the loaded languages, and load the language if it's not loaded yet.
if (!highlighter.getLoadedLanguages().includes(language)) {
// Check if the language is supported by Shiki
const bundles = BUNDLED_LANGUAGES.filter((bundle) => {
const bundles = bundledLanguagesInfo.filter((bundle) => {
// Languages are specified by their id, they can also have aliases (i. e. "js" and "javascript")
return bundle.id === language || bundle.aliases?.includes(language);
});
if (bundles.length > 0) {
await highlighter.loadLanguage(language);
console.log(`Loading language: ${language}`);
await highlighter.loadLanguage(bundles[0].import);
codeLang.value = language;
} else {
codeLang.value = 'js';
Expand All @@ -57,8 +58,8 @@ watch(() => props.lang, (to) => {
}, { immediate: true });
</script>

<style scoped lang="scss">
.codeBlockRoot :deep(.shiki) {
<style module lang="scss">
.codeBlockRoot :global(.shiki) {
padding: 1em;
margin: .5em 0;
overflow: auto;
Expand All @@ -74,7 +75,7 @@ watch(() => props.lang, (to) => {
min-width: 100%;
height: 100%;

& :deep(.shiki) {
& :global(.shiki) {
padding: 12px;
margin: 0;
border-radius: 6px;
Expand Down
28 changes: 15 additions & 13 deletions packages/frontend/src/scripts/code-highlighter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { setWasm, setCDN, Highlighter, getHighlighter as _getHighlighter } from 'shiki';

setWasm('/assets/shiki/dist/onig.wasm');
setCDN('/assets/shiki/');
import { getHighlighterCore, loadWasm } from 'shiki/core';
import darkPlus from 'shiki/themes/dark-plus.mjs';
import type { Highlighter, LanguageRegistration } from 'shiki';

let _highlighter: Highlighter | null = null;

Expand All @@ -13,16 +12,19 @@ export async function getHighlighter(): Promise<Highlighter> {
}

export async function initHighlighter() {
const highlighter = await _getHighlighter({
theme: 'dark-plus',
langs: ['js'],
});
const aiScriptGrammar = await import('aiscript-vscode/aiscript/syntaxes/aiscript.tmLanguage.json');

await loadWasm(import('shiki/onig.wasm?init'));

await highlighter.loadLanguage({
path: 'languages/aiscript.tmLanguage.json',
id: 'aiscript',
scopeName: 'source.aiscript',
aliases: ['is', 'ais'],
const highlighter = await getHighlighterCore({
themes: [darkPlus],
langs: [
import('shiki/langs/javascript.mjs'),
{
aliases: ['is', 'ais'],
...aiScriptGrammar.default,
} as unknown as LanguageRegistration,
],
});

_highlighter = highlighter;
Expand Down
30 changes: 10 additions & 20 deletions pnpm-lock.yaml

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

8 changes: 0 additions & 8 deletions scripts/build-assets.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,6 @@ async function copyFrontendLocales() {
}
}

async function copyFrontendShikiAssets() {
await fs.cp('./packages/frontend/node_modules/shiki/dist', './built/_frontend_dist_/shiki/dist', { dereference: true, recursive: true });
await fs.cp('./packages/frontend/node_modules/shiki/languages', './built/_frontend_dist_/shiki/languages', { dereference: true, recursive: true });
await fs.cp('./packages/frontend/node_modules/aiscript-vscode/aiscript/syntaxes', './built/_frontend_dist_/shiki/languages', { dereference: true, recursive: true });
await fs.cp('./packages/frontend/node_modules/shiki/themes', './built/_frontend_dist_/shiki/themes', { dereference: true, recursive: true });
}

async function copyBackendViews() {
await fs.cp('./packages/backend/src/server/web/views', './packages/backend/built/server/web/views', { recursive: true });
}
Expand Down Expand Up @@ -81,7 +74,6 @@ async function build() {
copyFrontendFonts(),
copyFrontendTablerIcons(),
copyFrontendLocales(),
copyFrontendShikiAssets(),
copyBackendViews(),
buildBackendScript(),
buildBackendStyle(),
Expand Down
Loading