✨ 迁移 Markdown 渲染并添加 Mermaid 预览和文章目录
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
<script setup lang="ts">
|
||||
import type { Collections } from '@nuxt/content';
|
||||
import ProsePre from '~/components/theme/classic/markdown/ProsePre.vue';
|
||||
import ProseCode from '~/components/theme/classic/markdown/ProseCode.vue';
|
||||
|
||||
type Article = Collections['content'] & {};
|
||||
defineProps<{
|
||||
article: Article | null | undefined;
|
||||
noPadding?: boolean;
|
||||
}>();
|
||||
const themeComponents = {
|
||||
classic: {
|
||||
pre: ProsePre,
|
||||
code: ProseCode,
|
||||
},
|
||||
};
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<ContentRenderer
|
||||
v-if="article" class="px-6 pb-6 pt-0.5 bg-old-neutral-200 dark:bg-old-neutral-800" :value="article!"
|
||||
:class="{ 'p-0! [&_p]:m-0! [&_p]:leading-6': true }"
|
||||
:components="themeComponents['classic']"/>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
|
||||
</style>
|
||||
@@ -0,0 +1,59 @@
|
||||
<script setup lang="ts">
|
||||
import type { TocLink } from '@nuxt/content';
|
||||
import TocList from '~/pages/index/article/[articleID]/components/TocList.vue';
|
||||
|
||||
const props = defineProps<{ links: TocLink[] }>();
|
||||
const activeIds = ref<string[]>([]);
|
||||
const { height } = useWindowSize();
|
||||
const navBarHeight = injectNavbarHeight();
|
||||
const flattenedLinks = ref<TocLink[]>(flatten(props.links));
|
||||
|
||||
function flatten(links: TocLink[]): TocLink[] {
|
||||
return links.flatMap((l) => [l, ...(l.children ? flatten(l.children as TocLink[]) : [])]);
|
||||
}
|
||||
|
||||
function onScroll() {
|
||||
const localActiveIds: string[] = [];
|
||||
flattenedLinks.value = flatten(props.links);
|
||||
const links = flattenedLinks.value;
|
||||
const topBias = 66;
|
||||
const navBarHeightVal = navBarHeight.value + topBias;
|
||||
for (let i = 0; i < links.length; i++) {
|
||||
const element = document.getElementById(links[i]!.id);
|
||||
if (!element) {
|
||||
continue;
|
||||
}
|
||||
const elTop = element.getBoundingClientRect().top;
|
||||
if (i == links.length - 1) {
|
||||
if ((elTop > navBarHeightVal && elTop < height.value)
|
||||
|| (elTop < navBarHeightVal)) {
|
||||
localActiveIds.push(links[i]!.id);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
const nextElement = document.getElementById(links[i + 1]!.id);
|
||||
if (!element || !nextElement) {
|
||||
continue;
|
||||
}
|
||||
const nextElTop = nextElement.getBoundingClientRect().top;
|
||||
if ((elTop > navBarHeightVal && elTop < height.value)
|
||||
|| (elTop < navBarHeightVal && nextElTop > navBarHeightVal)
|
||||
|| (nextElTop > navBarHeightVal && nextElTop < height.value)) {
|
||||
localActiveIds.push(links[i]!.id);
|
||||
}
|
||||
}
|
||||
activeIds.value = localActiveIds;
|
||||
}
|
||||
|
||||
onNuxtReady(() => {
|
||||
window.addEventListener('scroll', onScroll);
|
||||
onScroll();
|
||||
});
|
||||
onUnmounted(() => window.removeEventListener('scroll', onScroll));
|
||||
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<!-- 把整棵树和算好的 activeId 交给递归渲染组件 -->
|
||||
<TocList :links="flattenedLinks" :active-ids="activeIds"/>
|
||||
</template>
|
||||
@@ -0,0 +1,207 @@
|
||||
<script setup lang="ts">
|
||||
import type { TocLink } from '@nuxt/content';
|
||||
|
||||
// 2 6
|
||||
// 24 20
|
||||
// 2 6
|
||||
// 24 20
|
||||
// 2 6
|
||||
|
||||
const props = defineProps<{ links: TocLink[]; activeIds: string[] }>();
|
||||
|
||||
// const flattenedLinks = computed(() => {
|
||||
// props.links.forEach(link => {
|
||||
// })
|
||||
function getClassNames(index: number) {
|
||||
if (index < 0 || index > props.links.length) {
|
||||
return '';
|
||||
}
|
||||
const link = props.links[index]!;
|
||||
let className = '';
|
||||
if (props.activeIds.includes(link.id)) {
|
||||
className += 'text-sky-400 dark:text-[#cccaff] font-medium border-l-sky-400 dark:border-l-[#cccaff]';
|
||||
} else {
|
||||
className += 'text-old-neutral-500 border-l-old-neutral-800';
|
||||
}
|
||||
return className;
|
||||
}
|
||||
|
||||
let currentStart = 0;
|
||||
let currentEnd = 0;
|
||||
let targetStart = 0;
|
||||
let targetEnd = 0;
|
||||
const tocHeight = 26;
|
||||
const maxedHeight = computed(() => props.links.length * tocHeight);
|
||||
const targetPoints: Ref<number[]> = ref([]);
|
||||
|
||||
const points = ref('');
|
||||
let animationFrameId = 0;
|
||||
|
||||
const startCx = ref(0), startCy = ref(0), endCx = ref(0), endCy = ref(0);
|
||||
|
||||
function updateSvgPoints() {
|
||||
if (props.activeIds.length == 0) {
|
||||
return [];
|
||||
}
|
||||
const points: number[] = [];
|
||||
|
||||
const gapHeight = 6, tocHeight = 20;
|
||||
const yBias = -3;
|
||||
const xBias = 5;
|
||||
|
||||
function isIntersect(ab: number, ae: number, bb: number, be: number): boolean {
|
||||
return Math.max(ab, bb) < Math.min(ae, be);
|
||||
}
|
||||
|
||||
function getIntersects(ab: number, ae: number, bb: number, be: number): [number, number] {
|
||||
return [Math.max(ab, bb), Math.min(ae, be)];
|
||||
}
|
||||
|
||||
for (let i = 0; i < props.links.length; i++) {
|
||||
const link = props.links[i]!;
|
||||
const linkStart = i * tocHeight + (i + 1) * gapHeight + yBias;
|
||||
const linkEnd = (i + 1) * tocHeight + (i + 1) * gapHeight + yBias;
|
||||
if (isIntersect(linkStart, linkEnd, currentStart, currentEnd)) {
|
||||
const [yStart, yEnd] = getIntersects(linkStart, linkEnd, currentStart, currentEnd);
|
||||
|
||||
points.push((link.depth - 2) * 12 + xBias);
|
||||
points.push(yStart);
|
||||
points.push((link.depth - 2) * 12 + xBias);
|
||||
points.push(yEnd);
|
||||
}
|
||||
|
||||
if (i < props.links.length - 1) {
|
||||
const nextLinkStart = (i + 1) * tocHeight + (i + 2) * gapHeight + yBias;
|
||||
if (isIntersect(currentStart, currentEnd, linkEnd, nextLinkStart)) {
|
||||
const [xFullStart, xFullEnd] = [(link.depth - 2) * 12 + xBias, (props.links[i + 1]!.depth - 2) * 12 + xBias];
|
||||
if (currentStart > linkEnd && currentStart < nextLinkStart) {
|
||||
const x = xFullStart + (xFullEnd - xFullStart) * (currentStart - linkEnd) / (nextLinkStart - linkEnd);
|
||||
points.push(x);
|
||||
points.push(currentStart);
|
||||
}
|
||||
if (currentEnd > linkEnd && currentEnd < nextLinkStart) {
|
||||
const x = xFullEnd - (xFullEnd - xFullStart) * (nextLinkStart - currentEnd) / (nextLinkStart - linkEnd);
|
||||
points.push(x);
|
||||
points.push(currentEnd);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
[startCx.value, startCy.value] = [points[0] ?? -100, points[1] ?? -100];
|
||||
[endCx.value, endCy.value] = [points[points.length - 2] ?? -100, points[points.length - 1] ?? -100];
|
||||
return points;
|
||||
}
|
||||
|
||||
function getTarget(): [number, number] {
|
||||
if (props.activeIds.length == 0) {
|
||||
return [-1, -1];
|
||||
}
|
||||
const points: number[] = [];
|
||||
|
||||
const gapHeight = 6, tocHeight = 20;
|
||||
const bias = -3;
|
||||
|
||||
for (let i = 0; i < props.links.length; i++) {
|
||||
const link = props.links[i]!;
|
||||
if (props.activeIds.includes(link.id)) {
|
||||
points.push((link.depth - 2) * 12 + 1);
|
||||
points.push(i * tocHeight + (i + 1) * gapHeight);
|
||||
points.push((link.depth - 2) * 12 + 1);
|
||||
points.push((i + 1) * tocHeight + (i + 1) * gapHeight);
|
||||
}
|
||||
}
|
||||
|
||||
const edgeBias = 2;
|
||||
if (points.length > 1 && props.activeIds.includes(props.links[0]!.id)) {
|
||||
points[1]! += edgeBias;
|
||||
}
|
||||
if (points.length > 1 && props.activeIds.includes(props.links[props.links.length - 1]!.id)) {
|
||||
points[points.length - 1]! -= edgeBias;
|
||||
}
|
||||
|
||||
points.forEach((x, i, arr) => {
|
||||
arr[i] = i % 2 == 0 ? x : x + bias;
|
||||
});
|
||||
targetPoints.value = points;
|
||||
if (points.length >= 2) {
|
||||
return [points[1]!, points[points.length - 1]!];
|
||||
}
|
||||
return [-1, -1];
|
||||
}
|
||||
|
||||
function doUpdate() {
|
||||
[targetStart, targetEnd] = getTarget();
|
||||
const factor = 0.1;
|
||||
|
||||
function updateSvg() {
|
||||
points.value = updateSvgPoints().join(',');
|
||||
const startDelta = (targetStart - currentStart) * factor;
|
||||
const endDelta = (targetEnd - currentEnd) * factor;
|
||||
currentStart += startDelta;
|
||||
currentEnd += endDelta;
|
||||
if (Math.abs(currentStart - targetStart) < 0.2 && Math.abs(currentEnd - targetEnd) < 0.2) {
|
||||
currentStart = targetStart;
|
||||
currentEnd = targetEnd;
|
||||
points.value = updateSvgPoints().join(',');
|
||||
return;
|
||||
}
|
||||
animationFrameId = requestAnimationFrame(updateSvg);
|
||||
}
|
||||
|
||||
animationFrameId = requestAnimationFrame(updateSvg);
|
||||
}
|
||||
|
||||
// 外面在 onNuxtReady 更新数据 里面在onMounted时设置钩子
|
||||
onMounted(() => {
|
||||
watchEffect(() => {
|
||||
doUpdate();
|
||||
});
|
||||
});
|
||||
onUnmounted(() => {
|
||||
cancelAnimationFrame(animationFrameId);
|
||||
});
|
||||
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="relative">
|
||||
<svg
|
||||
class="absolute pointer-events-none" :viewBox="`0 0 100 ${maxedHeight}`" width="100"
|
||||
:height="`${links.length * tocHeight}`">
|
||||
<circle
|
||||
v-if="startCx != -100 || startCy != -100"
|
||||
:cx="startCx" :cy="startCy"
|
||||
r="1" fill="none"
|
||||
stroke-width="3"
|
||||
class="stroke-old-neutral-400 dark:stroke-white"/>
|
||||
<circle
|
||||
v-if="endCx != -100 || endCy == -100"
|
||||
:cx="endCx" :cy="endCy"
|
||||
r="1" fill="none"
|
||||
stroke-width="3"
|
||||
class="stroke-old-neutral-400 dark:stroke-white"/>
|
||||
<polyline
|
||||
:points="points" fill="none" class="stroke-old-neutral-400 dark:stroke-white" stroke-width="2"
|
||||
stroke-linejoin="round"></polyline>
|
||||
</svg>
|
||||
<ul>
|
||||
<li
|
||||
v-for="(link,idx) in links" :key="link.id"
|
||||
class="not-last:mb-0.5 not-first:mt-0.5"
|
||||
:style="{ marginLeft: `${ (link.depth - 2) * 12}px` }"
|
||||
>
|
||||
<a
|
||||
:href="`#${link.id}`"
|
||||
class="transition-colors duration-300"
|
||||
:class="getClassNames(idx)">
|
||||
<div class="text-sm text-nowrap truncate max-w-full pl-4 h-6 pt-0.75">
|
||||
{{ link.text }}
|
||||
</div>
|
||||
</a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
</style>
|
||||
@@ -1,11 +1,12 @@
|
||||
<script setup lang="ts">
|
||||
import { toMetaDataType } from '~/types/PostMetaData';
|
||||
import { MdCatalog } from 'md-editor-v3';
|
||||
import ArticleHeader from '~/pages/index/article/[articleID]/components/ArticleHeader.vue';
|
||||
import ArticleToc from './components/ArticleToc.vue';
|
||||
import ArticleContent from '~/pages/index/article/[articleID]/components/ArticleContent.vue';
|
||||
|
||||
const articleId = useRoute().params.articleID as string;
|
||||
const { data: article } = useAsyncData(async () => await queryCollection('content').where('id', '=', articleId).first());
|
||||
const editorId = 'article-previewer';
|
||||
|
||||
</script>
|
||||
|
||||
<template>
|
||||
@@ -15,14 +16,13 @@ const editorId = 'article-previewer';
|
||||
<div class="relative duration-500 transition-[width] xl:w-80 w-0 mr-2/3 overflow-hidden">
|
||||
<div class="w-80 top-0 left-0 text-gray-800 dark:text-white p-5">
|
||||
<div class="text-3xl mb-2">目录</div>
|
||||
<MdCatalog :editor-id="editorId" :scroll-element="'html'" class=""/>
|
||||
<ArticleToc :links="article?.body?.toc?.links ?? []" />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="transition-[width] duration-500 float-right xl:w-[calc(100%-20rem-40px)] w-full max-w-full">
|
||||
<ArticleHeader v-if="article" class="w-full" :meta-data="toMetaDataType(article)"/>
|
||||
<!-- <ArticleCard v-if="article" class=" w-full" :article="toArticleMetaDataType(article)"/>-->
|
||||
<ReadonlyMdEditor :editor-id="editorId" :markdown="article?.rawbody" class="p-5 max-w-full"/>
|
||||
<ArticleContent :article="article"/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -9,7 +9,7 @@ const props = withDefaults(defineProps<{
|
||||
{
|
||||
metaData: () => defaultMetaData,
|
||||
});
|
||||
const { data: rawbody } = useAsyncData('simpleCard:' + props.metaData.id, async () => (await queryCollection('content').where('id', '=', props.metaData.id).first())?.rawbody);
|
||||
const { data: article } = useAsyncData('simpleCard:' + props.metaData.id, async () => (await queryCollection('content').where('id', '=', props.metaData.id).first()));
|
||||
const collapsed = ref(true);
|
||||
const typeChinese = new Map<string | undefined, string>([
|
||||
['rambling', '絮语'],
|
||||
@@ -47,12 +47,6 @@ function getCostTime(length: number | DataAnomaly) {
|
||||
}
|
||||
}
|
||||
|
||||
const safeEditorId = computed(() => {
|
||||
const encoded = btoa(encodeURIComponent(props.metaData.id))
|
||||
.replace(/[+/=]/g, '_'); // 替换 Base64 中的特殊字符
|
||||
return `rambling_${encoded}`;
|
||||
});
|
||||
|
||||
const showShadow = ref(true);
|
||||
|
||||
function reverseCollapsed() {
|
||||
@@ -76,7 +70,7 @@ function reverseCollapsed() {
|
||||
<div class="text-4xl">
|
||||
{{ (typeChinese.get(metaData?.type) || 'unknown Type') + ':' }}{{ props.metaData.title }}
|
||||
</div>
|
||||
<div class="flex items-center mt-2 max-w-[400px] overflow-hidden">
|
||||
<div class="flex items-center mt-2 max-w-100 overflow-hidden">
|
||||
|
||||
<div title="发布时间" class="flex items-center">
|
||||
<Icon name="lucide:clock-arrow-up"/>
|
||||
@@ -117,9 +111,7 @@ function reverseCollapsed() {
|
||||
<div
|
||||
class="relative flex mt-2 justify-between overflow-hidden duration-300 ease-in-out min-h-34"
|
||||
:class="{'max-h-34' : collapsed, 'max-h-screen':!collapsed}">
|
||||
<ReadonlyMdEditor
|
||||
v-if="rawbody" :editor-id="safeEditorId" :markdown="rawbody!"
|
||||
class="w-full"/>
|
||||
<ArticleContent v-if="article" :article="article" class="w-full"/>
|
||||
<div
|
||||
class="absolute bottom-0 left-0 right-0 h-14 bg-linear-to-t from-old-neutral-200 dark:from-old-neutral-800 to-transparent pointer-events-none transition-opacity duration-300"
|
||||
:class="collapsed && showShadow?'opacity-100':'opacity-0'"
|
||||
|
||||
Reference in New Issue
Block a user