2023-07-27 05:31:52 +00:00
|
|
|
<!--
|
2024-02-12 02:37:45 +00:00
|
|
|
SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 05:31:52 +00:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
-->
|
|
|
|
|
2022-07-05 02:21:59 +00:00
|
|
|
<!-- eslint-disable vue/no-v-html -->
|
2019-01-27 05:34:52 +00:00
|
|
|
<template>
|
2024-02-06 06:03:07 +00:00
|
|
|
<div :class="[$style.codeBlockRoot, { [$style.codeEditor]: codeEditor }, (darkMode ? $style.dark : $style.light)]" v-html="html"></div>
|
2019-01-27 05:34:52 +00:00
|
|
|
</template>
|
|
|
|
|
2022-01-10 15:05:18 +00:00
|
|
|
<script lang="ts" setup>
|
2023-10-29 05:12:40 +00:00
|
|
|
import { ref, computed, watch } from 'vue';
|
2024-02-02 06:05:18 +00:00
|
|
|
import { bundledLanguagesInfo } from 'shiki';
|
|
|
|
import type { BuiltinLanguage } from 'shiki';
|
2024-02-06 06:03:07 +00:00
|
|
|
import { getHighlighter, getTheme } from '@/scripts/code-highlighter.js';
|
|
|
|
import { defaultStore } from '@/store.js';
|
2020-10-17 11:12:00 +00:00
|
|
|
|
2022-01-10 15:05:18 +00:00
|
|
|
const props = defineProps<{
|
|
|
|
code: string;
|
|
|
|
lang?: string;
|
2023-10-29 05:12:40 +00:00
|
|
|
codeEditor?: boolean;
|
2022-01-10 15:05:18 +00:00
|
|
|
}>();
|
|
|
|
|
2023-10-29 05:12:40 +00:00
|
|
|
const highlighter = await getHighlighter();
|
2024-02-06 06:03:07 +00:00
|
|
|
const darkMode = defaultStore.reactiveState.darkMode;
|
2024-02-02 06:05:18 +00:00
|
|
|
const codeLang = ref<BuiltinLanguage | 'aiscript'>('js');
|
2024-02-06 06:03:07 +00:00
|
|
|
|
|
|
|
const [lightThemeName, darkThemeName] = await Promise.all([
|
|
|
|
getTheme('light', true),
|
|
|
|
getTheme('dark', true),
|
|
|
|
]);
|
|
|
|
|
2023-10-29 05:12:40 +00:00
|
|
|
const html = computed(() => highlighter.codeToHtml(props.code, {
|
|
|
|
lang: codeLang.value,
|
2024-02-06 06:03:07 +00:00
|
|
|
themes: {
|
|
|
|
fallback: 'dark-plus',
|
|
|
|
light: lightThemeName,
|
|
|
|
dark: darkThemeName,
|
|
|
|
},
|
|
|
|
defaultColor: false,
|
|
|
|
cssVariablePrefix: '--shiki-',
|
2023-10-29 05:12:40 +00:00
|
|
|
}));
|
|
|
|
|
|
|
|
async function fetchLanguage(to: string): Promise<void> {
|
2024-02-02 06:05:18 +00:00
|
|
|
const language = to as BuiltinLanguage;
|
2023-10-29 05:12:40 +00:00
|
|
|
|
|
|
|
// 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
|
2024-02-02 06:05:18 +00:00
|
|
|
const bundles = bundledLanguagesInfo.filter((bundle) => {
|
2023-10-29 05:12:40 +00:00
|
|
|
// 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) {
|
2024-02-02 06:05:18 +00:00
|
|
|
console.log(`Loading language: ${language}`);
|
|
|
|
await highlighter.loadLanguage(bundles[0].import);
|
2023-10-29 05:12:40 +00:00
|
|
|
codeLang.value = language;
|
|
|
|
} else {
|
|
|
|
codeLang.value = 'js';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
codeLang.value = language;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
watch(() => props.lang, (to) => {
|
|
|
|
if (codeLang.value === to || !to) return;
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
fetchLanguage(to).then(() => resolve);
|
|
|
|
});
|
2023-12-11 11:31:23 +00:00
|
|
|
}, { immediate: true });
|
2019-01-27 05:34:52 +00:00
|
|
|
</script>
|
2023-10-29 05:12:40 +00:00
|
|
|
|
2024-02-02 06:05:18 +00:00
|
|
|
<style module lang="scss">
|
|
|
|
.codeBlockRoot :global(.shiki) {
|
2023-10-29 05:12:40 +00:00
|
|
|
padding: 1em;
|
|
|
|
margin: .5em 0;
|
|
|
|
overflow: auto;
|
2023-12-03 01:58:42 +00:00
|
|
|
border-radius: 8px;
|
2024-02-06 06:03:07 +00:00
|
|
|
border: 1px solid var(--divider);
|
2024-02-07 10:58:21 +00:00
|
|
|
font-family: Consolas, Monaco, Andale Mono, Ubuntu Mono, monospace;
|
2024-02-06 06:03:07 +00:00
|
|
|
|
|
|
|
color: var(--shiki-fallback);
|
|
|
|
background-color: var(--shiki-fallback-bg);
|
|
|
|
|
|
|
|
& span {
|
|
|
|
color: var(--shiki-fallback);
|
|
|
|
background-color: var(--shiki-fallback-bg);
|
|
|
|
}
|
2023-10-29 05:12:40 +00:00
|
|
|
|
|
|
|
& pre,
|
|
|
|
& code {
|
|
|
|
font-family: Consolas, Monaco, Andale Mono, Ubuntu Mono, monospace;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-06 06:03:07 +00:00
|
|
|
.light.codeBlockRoot :global(.shiki) {
|
|
|
|
color: var(--shiki-light);
|
|
|
|
background-color: var(--shiki-light-bg);
|
|
|
|
|
|
|
|
& span {
|
|
|
|
color: var(--shiki-light);
|
|
|
|
background-color: var(--shiki-light-bg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
.dark.codeBlockRoot :global(.shiki) {
|
|
|
|
color: var(--shiki-dark);
|
|
|
|
background-color: var(--shiki-dark-bg);
|
|
|
|
|
|
|
|
& span {
|
|
|
|
color: var(--shiki-dark);
|
|
|
|
background-color: var(--shiki-dark-bg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-29 05:12:40 +00:00
|
|
|
.codeBlockRoot.codeEditor {
|
|
|
|
min-width: 100%;
|
|
|
|
height: 100%;
|
|
|
|
|
2024-02-02 06:05:18 +00:00
|
|
|
& :global(.shiki) {
|
2023-10-29 05:12:40 +00:00
|
|
|
padding: 12px;
|
|
|
|
margin: 0;
|
|
|
|
border-radius: 6px;
|
2024-02-06 06:03:07 +00:00
|
|
|
border: none;
|
2023-10-29 05:12:40 +00:00
|
|
|
min-height: 130px;
|
|
|
|
pointer-events: none;
|
|
|
|
min-width: calc(100% - 24px);
|
|
|
|
height: 100%;
|
|
|
|
display: inline-block;
|
|
|
|
line-height: 1.5em;
|
|
|
|
font-size: 1em;
|
|
|
|
overflow: visible;
|
|
|
|
text-rendering: inherit;
|
|
|
|
text-transform: inherit;
|
|
|
|
white-space: pre;
|
2024-02-06 06:03:07 +00:00
|
|
|
|
|
|
|
& span {
|
|
|
|
display: inline-block;
|
|
|
|
min-height: 1em;
|
|
|
|
}
|
2023-10-29 05:12:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
</style>
|