✨ 迁移 Markdown 渲染并添加 Mermaid 预览和文章目录

This commit is contained in:
2026-11-15 22:05:13 +08:00
parent 293b743e50
commit 9d13e9aac2
14 changed files with 535 additions and 101 deletions
@@ -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>