feat: 文章添加toc
This commit is contained in:
@@ -3,13 +3,14 @@ import "@/styles/github-markdown.css";
|
||||
import "@/styles/github-markdown-reset.css";
|
||||
import "photoswipe/dist/photoswipe.css";
|
||||
|
||||
import Layout from '@/layouts/Layout.astro';
|
||||
import {createHttp} from "@/lib/request.ts";
|
||||
import type {Post} from "@/types";
|
||||
import {processHtmlWithJsdom} from "@/lib/processHtml.ts";
|
||||
import {formatDatetime} from "@/lib/format.ts";
|
||||
import Layout from "@/layouts/Layout.astro";
|
||||
import Toc from "@/components/Post/Toc.astro";
|
||||
import { createHttp } from "@/lib/request.ts";
|
||||
import type { Post } from "@/types";
|
||||
import { processHtmlWithJsdom } from "@/lib/processHtml.ts";
|
||||
import { formatDatetime } from "@/lib/format.ts";
|
||||
|
||||
const {slug} = Astro.params;
|
||||
const { slug } = Astro.params;
|
||||
|
||||
const http = createHttp(Astro.request.headers);
|
||||
|
||||
@@ -22,49 +23,58 @@ try {
|
||||
return Astro.redirect("/404");
|
||||
}
|
||||
|
||||
const content = await processHtmlWithJsdom(data.content);
|
||||
const { html, toc } = await processHtmlWithJsdom(data.content);
|
||||
const published_at = formatDatetime(data.published_at, "YYYY.MM.DD");
|
||||
---
|
||||
|
||||
<Layout title={data.title} description={data.summary}>
|
||||
<article>
|
||||
<div class="meta">
|
||||
<div class="meta-left">
|
||||
<span>{data.category_name} · {data.category_code.toUpperCase()}</span>
|
||||
<span>{published_at}</span>
|
||||
<div class="post-wrap">
|
||||
<article>
|
||||
<div class="meta">
|
||||
<div class="meta-left">
|
||||
<span>{data.category_name} · {data.category_code.toUpperCase()}</span>
|
||||
<span>{published_at}</span>
|
||||
</div>
|
||||
|
||||
<div class="meta-view-count">{data.view_count} 次阅读</div>
|
||||
</div>
|
||||
<h1 class="title">{data.title}</h1>
|
||||
|
||||
<div class="meta-view-count">{data.view_count} 次阅读</div>
|
||||
</div>
|
||||
<h1>{data.title}</h1>
|
||||
{
|
||||
data.cover && (
|
||||
<div>
|
||||
<img class="cover" src={data.cover} alt={data.title} />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
{data.cover &&
|
||||
<div>
|
||||
<img class="cover" src={data.cover} alt={data.title}>
|
||||
</div>
|
||||
}
|
||||
<p class="summary">{data.summary}</p>
|
||||
|
||||
<p class="summary">{data.summary}</p>
|
||||
<div class="divider"></div>
|
||||
|
||||
<div class="divider"></div>
|
||||
<div class="markdown-body" data-color-mode="dark" set:html={html} />
|
||||
|
||||
<div class="markdown-body" data-color-mode="dark" set:html={content}/>
|
||||
{
|
||||
data.tags.length > 0 && (
|
||||
<div class="tags">
|
||||
{data.tags.map((tag) => (
|
||||
<span>{tag.name}</span>
|
||||
))}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
</article>
|
||||
|
||||
{
|
||||
data.tags.length > 0 &&
|
||||
<div class="tags">
|
||||
{data.tags.map(tag => <span>{tag.name}</span>)}
|
||||
</div>
|
||||
}
|
||||
</article>
|
||||
{toc.length > 0 && <Toc toc={toc} />}
|
||||
</div>
|
||||
</Layout>
|
||||
|
||||
<style>
|
||||
article {
|
||||
width: 100%;
|
||||
margin: 0 auto;
|
||||
.post-wrap {
|
||||
position: relative;
|
||||
flex: 1;
|
||||
flex-shrink: 0;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.cover {
|
||||
@@ -74,7 +84,7 @@ const published_at = formatDatetime(data.published_at, "YYYY.MM.DD");
|
||||
|
||||
.meta {
|
||||
color: var(--text-meta);
|
||||
font-size: 13px;
|
||||
font-size: 14px;
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
@@ -91,17 +101,18 @@ const published_at = formatDatetime(data.published_at, "YYYY.MM.DD");
|
||||
color: var(--text-primary);
|
||||
}
|
||||
|
||||
h1 {
|
||||
font-size: 28px;
|
||||
.title {
|
||||
font-size: 36px;
|
||||
line-height: 1.4;
|
||||
font-weight: 500;
|
||||
margin-bottom: 20px;
|
||||
color: var(--text-primary);
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.summary {
|
||||
font-size: 14px;
|
||||
margin-bottom: 20px;
|
||||
color: var(--text-meta);
|
||||
color: var(--text-summary);
|
||||
line-height: 1.4;
|
||||
}
|
||||
|
||||
@@ -112,7 +123,7 @@ const published_at = formatDatetime(data.published_at, "YYYY.MM.DD");
|
||||
.markdown-body {
|
||||
margin-top: 24px;
|
||||
line-height: 1.8;
|
||||
font-size: 15px;
|
||||
font-size: 16px;
|
||||
color: var(--article-text-color);
|
||||
}
|
||||
|
||||
@@ -132,8 +143,8 @@ const published_at = formatDatetime(data.published_at, "YYYY.MM.DD");
|
||||
}
|
||||
|
||||
@media (max-width: 640px) {
|
||||
h1{
|
||||
font-size: 24px;
|
||||
.title {
|
||||
font-size: 30px;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
@@ -146,7 +157,7 @@ const published_at = formatDatetime(data.published_at, "YYYY.MM.DD");
|
||||
const article = document.querySelector(".markdown-body")!;
|
||||
const images = Array.from(article.querySelectorAll("img"));
|
||||
|
||||
if(images.length === 0) return
|
||||
if (images.length === 0) return;
|
||||
|
||||
const waitImageLoaded = (img: HTMLImageElement) => {
|
||||
if (img.complete && img.naturalWidth > 0 && img.naturalHeight > 0) {
|
||||
@@ -154,8 +165,8 @@ const published_at = formatDatetime(data.published_at, "YYYY.MM.DD");
|
||||
}
|
||||
|
||||
return new Promise<void>((resolve) => {
|
||||
img.addEventListener("load", () => resolve(), {once: true});
|
||||
img.addEventListener("error", () => resolve(), {once: true});
|
||||
img.addEventListener("load", () => resolve(), { once: true });
|
||||
img.addEventListener("error", () => resolve(), { once: true });
|
||||
});
|
||||
};
|
||||
|
||||
@@ -201,7 +212,6 @@ const published_at = formatDatetime(data.published_at, "YYYY.MM.DD");
|
||||
|
||||
init();
|
||||
|
||||
|
||||
const copyTimers = new WeakMap<HTMLElement, ReturnType<typeof setTimeout>>();
|
||||
|
||||
// 代码块复制功能
|
||||
@@ -209,7 +219,7 @@ const published_at = formatDatetime(data.published_at, "YYYY.MM.DD");
|
||||
text: (el) => {
|
||||
const sibling = el.closest(".code-header")?.nextElementSibling;
|
||||
return sibling?.textContent.trim() || "";
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
clipboard.on("success", (e) => {
|
||||
|
||||
Reference in New Issue
Block a user