support code view
This commit is contained in:
@ -14,6 +14,7 @@ defineOptions({
|
|||||||
});
|
});
|
||||||
|
|
||||||
const props = withDefaults(defineProps<{
|
const props = withDefaults(defineProps<{
|
||||||
|
code?: string;
|
||||||
content?: string;
|
content?: string;
|
||||||
showCodeBorder?: boolean;
|
showCodeBorder?: boolean;
|
||||||
maxHeight?: string;
|
maxHeight?: string;
|
||||||
@ -25,13 +26,27 @@ const props = withDefaults(defineProps<{
|
|||||||
const { content } = toRefs(props);
|
const { content } = toRefs(props);
|
||||||
const markdownHTML = ref("");
|
const markdownHTML = ref("");
|
||||||
|
|
||||||
|
const buildMarkdown = () => {
|
||||||
|
if (props.code) {
|
||||||
|
const codeRaw = props.code ?? "";
|
||||||
|
const sepIndex = codeRaw.indexOf(":");
|
||||||
|
const lang = sepIndex < 0 ? "" : codeRaw.slice(0, sepIndex).trim();
|
||||||
|
const codeText = sepIndex < 0 ? codeRaw : codeRaw.slice(sepIndex + 1);
|
||||||
|
const langTag = lang ? lang : "";
|
||||||
|
|
||||||
|
return `\`\`\`${langTag}\n${codeText}\n\`\`\``;
|
||||||
|
}
|
||||||
|
return content.value;
|
||||||
|
};
|
||||||
|
|
||||||
const doRender = async () => {
|
const doRender = async () => {
|
||||||
markdownHTML.value = await Markdown.getInstance().toHTML(content.value);
|
const markdownText = buildMarkdown();
|
||||||
|
markdownHTML.value = await Markdown.getInstance().toHTML(markdownText);
|
||||||
await nextTick();
|
await nextTick();
|
||||||
Prism.highlightAll();
|
Prism.highlightAll();
|
||||||
};
|
};
|
||||||
|
|
||||||
watch(() => props.content, doRender);
|
watch(() => [props.code, props.content], doRender);
|
||||||
onMounted(doRender);
|
onMounted(doRender);
|
||||||
</script>
|
</script>
|
||||||
<style lang="less" scoped>
|
<style lang="less" scoped>
|
||||||
|
|||||||
@ -423,4 +423,11 @@ export default class Toolkit {
|
|||||||
}
|
}
|
||||||
}, interval);
|
}, interval);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static formatJson(jsonStr?: string): string {
|
||||||
|
if (!jsonStr) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
return JSON.stringify(JSON.parse(jsonStr), null, 4);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user