2023-07-27 07:31:52 +02:00
|
|
|
<!--
|
|
|
|
SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
-->
|
|
|
|
|
2022-07-05 04:21:59 +02:00
|
|
|
<!-- eslint-disable vue/no-v-html -->
|
2019-01-27 06:34:52 +01:00
|
|
|
<template>
|
2023-10-29 06:12:40 +01:00
|
|
|
<div :class="['codeBlockRoot', { 'codeEditor': codeEditor }]" v-html="html"></div>
|
2019-01-27 06:34:52 +01:00
|
|
|
</template>
|
|
|
|
|
2022-01-10 16:05:18 +01:00
|
|
|
<script lang="ts" setup>
|
2023-10-29 06:12:40 +01:00
|
|
|
import { ref, computed, watch } from 'vue';
|
|
|
|
import { BUNDLED_LANGUAGES } from 'shiki';
|
|
|
|
import type { Lang as ShikiLang } from 'shiki';
|
|
|
|
import { getHighlighter } from '@/scripts/code-highlighter.js';
|
2020-10-17 13:12:00 +02:00
|
|
|
|
2022-01-10 16:05:18 +01:00
|
|
|
const props = defineProps<{
|
|
|
|
code: string;
|
|
|
|
lang?: string;
|
2023-10-29 06:12:40 +01:00
|
|
|
codeEditor?: boolean;
|
2022-01-10 16:05:18 +01:00
|
|
|
}>();
|
|
|
|
|
2023-10-29 06:12:40 +01:00
|
|
|
const highlighter = await getHighlighter();
|
|
|
|
|
|
|
|
const codeLang = ref<ShikiLang | '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;
|
|
|
|
|
|
|
|
// 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) => {
|
|
|
|
// 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);
|
|
|
|
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 12:31:23 +01:00
|
|
|
}, { immediate: true });
|
2019-01-27 06:34:52 +01:00
|
|
|
</script>
|
2023-10-29 06:12:40 +01:00
|
|
|
|
|
|
|
<style scoped lang="scss">
|
|
|
|
.codeBlockRoot :deep(.shiki) {
|
|
|
|
padding: 1em;
|
|
|
|
margin: .5em 0;
|
|
|
|
overflow: auto;
|
2023-12-03 02:58:42 +01:00
|
|
|
border-radius: 8px;
|
2023-10-29 06:12:40 +01:00
|
|
|
|
|
|
|
& pre,
|
|
|
|
& code {
|
|
|
|
font-family: Consolas, Monaco, Andale Mono, Ubuntu Mono, monospace;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
.codeBlockRoot.codeEditor {
|
|
|
|
min-width: 100%;
|
|
|
|
height: 100%;
|
|
|
|
|
|
|
|
& :deep(.shiki) {
|
|
|
|
padding: 12px;
|
|
|
|
margin: 0;
|
2023-11-01 21:00:55 +01:00
|
|
|
border-radius: var(--radius-sm);
|
2023-10-29 06:12:40 +01: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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</style>
|