2024-12-22 12:22:47 -08:00
|
|
|
<script setup lang="ts">
|
2025-01-03 21:50:12 -08:00
|
|
|
import { globalMarkdown, MarkdownInput } from '~/assets/markdown_conf';
|
2024-12-22 12:22:47 -08:00
|
|
|
import { useSlots } from 'vue';
|
2025-01-03 21:50:12 -08:00
|
|
|
import fm, { type FrontMatterResult } from 'front-matter';
|
|
|
|
import '~/assets/style/markdown.scss';
|
|
|
|
|
|
|
|
// External inputs (reactive)
|
|
|
|
const input = defineProps({
|
|
|
|
input: {
|
|
|
|
type: String,
|
|
|
|
required: true
|
|
|
|
},
|
|
|
|
type: {
|
|
|
|
type: String,
|
|
|
|
default: "markdown",
|
|
|
|
validator: (value: string) => ["markdown", "html"].includes(value)
|
|
|
|
}
|
|
|
|
});
|
2024-12-22 12:22:47 -08:00
|
|
|
|
2025-01-03 21:50:12 -08:00
|
|
|
// Internal state
|
|
|
|
const contents = ref("");
|
|
|
|
const loading = ref(false);
|
|
|
|
const error = ref("");
|
2024-12-22 12:22:47 -08:00
|
|
|
|
2025-01-03 21:50:12 -08:00
|
|
|
const emit = defineEmits(["metadata"]);
|
2024-12-22 12:22:47 -08:00
|
|
|
|
2025-01-03 21:50:12 -08:00
|
|
|
function updateContents(input: string | undefined, type: "markdown" | "html" | undefined) {
|
|
|
|
if (input === "" || input === undefined) {
|
|
|
|
contents.value = "";
|
|
|
|
error.value = "";
|
2024-12-22 12:22:47 -08:00
|
|
|
return;
|
|
|
|
}
|
2025-01-03 21:50:12 -08:00
|
|
|
const markdown_input: MarkdownInput = type ?
|
|
|
|
type === "markdown" ?
|
|
|
|
MarkdownInput.from_markdown(input) : type == "html" ?
|
|
|
|
MarkdownInput.from_html(input) :
|
|
|
|
MarkdownInput.from_markdown(input) : MarkdownInput.from_markdown(input);
|
|
|
|
|
|
|
|
console.log("Rendering...")
|
|
|
|
const render = globalMarkdown.render(markdown_input);
|
|
|
|
console.log("Metadata: ", render.metadata);
|
|
|
|
contents.value = render.contents;
|
|
|
|
emit("metadata", render.metadata);
|
|
|
|
|
2024-12-22 12:22:47 -08:00
|
|
|
}
|
|
|
|
|
2025-01-03 21:50:12 -08:00
|
|
|
watch(() => input.input, (newVal) => {
|
|
|
|
updateContents(newVal, input.type as "markdown" | "html");
|
|
|
|
}, { immediate: true });
|
|
|
|
|
2024-12-22 12:22:47 -08:00
|
|
|
</script>
|
|
|
|
|
|
|
|
<template>
|
|
|
|
<div v-if="loading" class="text-center animate-pulse">
|
|
|
|
<h2>Loading...</h2>
|
|
|
|
</div>
|
2025-01-03 21:50:12 -08:00
|
|
|
<div v-else-if="error" class="text-center">
|
|
|
|
<h2>Error: {{ error }}</h2>
|
|
|
|
</div>
|
|
|
|
<div v-else class="md-contents">
|
|
|
|
<div class="md-contents" v-html="contents"></div>
|
|
|
|
</div>
|
|
|
|
</template>
|