diff options
| author | おさむのひと <46447427+samunohito@users.noreply.github.com> | 2024-03-20 10:30:45 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-03-20 10:30:45 +0900 |
| commit | 115d91812e4bc25a56126f23b4ad13b07d451552 (patch) | |
| tree | c5013bce69c9f9898921a5f1c32e0bfa57138092 /packages/frontend/src | |
| parent | Add missing credit (for #13586) (diff) | |
| download | misskey-115d91812e4bc25a56126f23b4ad13b07d451552.tar.gz misskey-115d91812e4bc25a56126f23b4ad13b07d451552.tar.bz2 misskey-115d91812e4bc25a56126f23b4ad13b07d451552.zip | |
fix(frontend): shikiの言語・テーマの定義ファイルをCDN(esm.sh)から取るようにする (#13598)
* fix(frontend): shikiの言語・テーマの定義ファイルをCDN(esm.sh)から取るようにする
* fix CHANGELOG.md
Diffstat (limited to 'packages/frontend/src')
| -rw-r--r-- | packages/frontend/src/components/MkCode.core.vue | 10 | ||||
| -rw-r--r-- | packages/frontend/src/index.html | 2 | ||||
| -rw-r--r-- | packages/frontend/src/scripts/code-highlighter.ts | 18 | ||||
| -rw-r--r-- | packages/frontend/src/scripts/theme.ts | 4 | ||||
| -rw-r--r-- | packages/frontend/src/store.ts | 1 |
5 files changed, 17 insertions, 18 deletions
diff --git a/packages/frontend/src/components/MkCode.core.vue b/packages/frontend/src/components/MkCode.core.vue index 872517b6aa..c0e7df5dac 100644 --- a/packages/frontend/src/components/MkCode.core.vue +++ b/packages/frontend/src/components/MkCode.core.vue @@ -9,9 +9,9 @@ SPDX-License-Identifier: AGPL-3.0-only </template> <script lang="ts" setup> -import { ref, computed, watch } from 'vue'; -import { bundledLanguagesInfo } from 'shiki'; -import type { BuiltinLanguage } from 'shiki'; +import { computed, ref, watch } from 'vue'; +import { bundledLanguagesInfo } from 'shiki/langs'; +import type { BundledLanguage } from 'shiki/langs'; import { getHighlighter, getTheme } from '@/scripts/code-highlighter.js'; import { defaultStore } from '@/store.js'; @@ -23,7 +23,7 @@ const props = defineProps<{ const highlighter = await getHighlighter(); const darkMode = defaultStore.reactiveState.darkMode; -const codeLang = ref<BuiltinLanguage | 'aiscript'>('js'); +const codeLang = ref<BundledLanguage | 'aiscript'>('js'); const [lightThemeName, darkThemeName] = await Promise.all([ getTheme('light', true), @@ -42,7 +42,7 @@ const html = computed(() => highlighter.codeToHtml(props.code, { })); async function fetchLanguage(to: string): Promise<void> { - const language = to as BuiltinLanguage; + const language = to as BundledLanguage; // Check for the loaded languages, and load the language if it's not loaded yet. if (!highlighter.getLoadedLanguages().includes(language)) { diff --git a/packages/frontend/src/index.html b/packages/frontend/src/index.html index cd84145f40..08ff0c58dd 100644 --- a/packages/frontend/src/index.html +++ b/packages/frontend/src/index.html @@ -18,7 +18,7 @@ http-equiv="Content-Security-Policy" content="default-src 'self' https://newassets.hcaptcha.com/ https://challenges.cloudflare.com/ http://localhost:7493/; worker-src 'self'; - script-src 'self' 'unsafe-eval' https://*.hcaptcha.com https://challenges.cloudflare.com; + script-src 'self' 'unsafe-eval' https://*.hcaptcha.com https://challenges.cloudflare.com https://esm.sh; style-src 'self' 'unsafe-inline'; img-src 'self' data: blob: www.google.com xn--931a.moe localhost:3000 localhost:5173 127.0.0.1:5173 127.0.0.1:3000; media-src 'self' localhost:3000 localhost:5173 127.0.0.1:5173 127.0.0.1:3000; diff --git a/packages/frontend/src/scripts/code-highlighter.ts b/packages/frontend/src/scripts/code-highlighter.ts index 5dd0a3be78..e94027d302 100644 --- a/packages/frontend/src/scripts/code-highlighter.ts +++ b/packages/frontend/src/scripts/code-highlighter.ts @@ -3,18 +3,19 @@ * SPDX-License-Identifier: AGPL-3.0-only */ -import { bundledThemesInfo } from 'shiki'; import { getHighlighterCore, loadWasm } from 'shiki/core'; import darkPlus from 'shiki/themes/dark-plus.mjs'; +import { bundledThemesInfo } from 'shiki/themes'; +import { bundledLanguagesInfo } from 'shiki/langs'; import { unique } from './array.js'; import { deepClone } from './clone.js'; import { deepMerge } from './merge.js'; -import type { Highlighter, LanguageRegistration, ThemeRegistration, ThemeRegistrationRaw } from 'shiki'; +import type { HighlighterCore, LanguageRegistration, ThemeRegistration, ThemeRegistrationRaw } from 'shiki/core'; import { ColdDeviceStorage } from '@/store.js'; import lightTheme from '@/themes/_light.json5'; import darkTheme from '@/themes/_dark.json5'; -let _highlighter: Highlighter | null = null; +let _highlighter: HighlighterCore | null = null; export async function getTheme(mode: 'light' | 'dark', getName: true): Promise<string>; export async function getTheme(mode: 'light' | 'dark', getName?: false): Promise<ThemeRegistration | ThemeRegistrationRaw>; @@ -51,16 +52,14 @@ export async function getTheme(mode: 'light' | 'dark', getName = false): Promise return darkPlus; } -export async function getHighlighter(): Promise<Highlighter> { +export async function getHighlighter(): Promise<HighlighterCore> { if (!_highlighter) { return await initHighlighter(); } return _highlighter; } -export async function initHighlighter() { - const aiScriptGrammar = await import('aiscript-vscode/aiscript/syntaxes/aiscript.tmLanguage.json'); - +async function initHighlighter() { await loadWasm(import('shiki/onig.wasm?init')); // テーマの重複を消す @@ -69,11 +68,12 @@ export async function initHighlighter() { ...(await Promise.all([getTheme('light'), getTheme('dark')])), ]); + const jsLangInfo = bundledLanguagesInfo.find(t => t.id === 'javascript'); const highlighter = await getHighlighterCore({ themes, langs: [ - import('shiki/langs/javascript.mjs'), - aiScriptGrammar.default as unknown as LanguageRegistration, + ...(jsLangInfo ? [async () => await jsLangInfo.import()] : []), + async () => (await import('aiscript-vscode/aiscript/syntaxes/aiscript.tmLanguage.json')).default as unknown as LanguageRegistration, ], }); diff --git a/packages/frontend/src/scripts/theme.ts b/packages/frontend/src/scripts/theme.ts index 5f7e88bd9f..c7f8b3d596 100644 --- a/packages/frontend/src/scripts/theme.ts +++ b/packages/frontend/src/scripts/theme.ts @@ -6,7 +6,7 @@ import { ref } from 'vue'; import tinycolor from 'tinycolor2'; import { deepClone } from './clone.js'; -import type { BuiltinTheme } from 'shiki'; +import type { BundledTheme } from 'shiki/themes'; import { globalEvents } from '@/events.js'; import lightTheme from '@/themes/_light.json5'; import darkTheme from '@/themes/_dark.json5'; @@ -20,7 +20,7 @@ export type Theme = { base?: 'dark' | 'light'; props: Record<string, string>; codeHighlighter?: { - base: BuiltinTheme; + base: BundledTheme; overrides?: Record<string, any>; } | { base: '_none_'; diff --git a/packages/frontend/src/store.ts b/packages/frontend/src/store.ts index a335ed33bb..7742acc60d 100644 --- a/packages/frontend/src/store.ts +++ b/packages/frontend/src/store.ts @@ -7,7 +7,6 @@ import { markRaw, ref } from 'vue'; import * as Misskey from 'misskey-js'; import { miLocalStorage } from './local-storage.js'; import type { SoundType } from '@/scripts/sound.js'; -import type { BuiltinTheme as ShikiBuiltinTheme } from 'shiki'; import { Storage } from '@/pizzax.js'; import { hemisphere } from '@/scripts/intl-const.js'; |