Skip to content

Commit f7853b2

Browse files
committed
feat(blog-web): 添加 WASM Markdown 渲染器并集成到博客组件中
添加了基于 Rust 和 WASM 的 Markdown 渲染器,并将其集成到博客组件中,以提高渲染性能
1 parent 9bc4702 commit f7853b2

File tree

8 files changed

+231
-4
lines changed

8 files changed

+231
-4
lines changed

.DS_Store

0 Bytes
Binary file not shown.

blog-web/md-wasm/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target

blog-web/md-wasm/Cargo.lock

Lines changed: 186 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

blog-web/md-wasm/Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "md-wasm"
3+
version = "0.1.0"
4+
edition = "2024"
5+
6+
[lib]
7+
crate-type = ["cdylib"]
8+
9+
[dependencies]
10+
wasm-bindgen = "0.2"
11+
pulldown-cmark = "0.13.0"

blog-web/md-wasm/src/lib.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
use pulldown_cmark::{Parser, html};
2+
use wasm_bindgen::prelude::*;
3+
4+
#[wasm_bindgen]
5+
pub fn render_markdown(input: &str) -> String {
6+
let parser = Parser::new(input);
7+
let mut html_output = String::new();
8+
html::push_html(&mut html_output, parser);
9+
html_output
10+
}

blog-web/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"scripts": {
66
"dev": "astro dev",
77
"build": "astro build",
8+
"build:wasm": "wasm-pack build md-wasm --target web",
89
"preview": "astro preview",
910
"astro": "astro",
1011
"start": "HOST=0.0.0.0 PORT=4321 node ./dist/server/entry.mjs"

blog-web/src/components/solid/BlogPost.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { createResource, Show } from "solid-js";
22
import http from "@/lib/axios";
3+
import MarkdownRenderer from "@/components/solid/MarkdownRenderer";
34

45
// 定义文章接口
56
interface Post {
@@ -51,7 +52,6 @@ async function fetchPost(slug: string): Promise<Post | null> {
5152
}
5253
}
5354

54-
5555
interface BlogPostProps {
5656
slug: string;
5757
}
@@ -97,7 +97,7 @@ export default function BlogPost(props: BlogPostProps) {
9797
<article class="prose prose-lg max-w-none dark:prose-invert">
9898
{post()!.featured_image && (
9999
<img
100-
src={post()!.featured_image ?? ''}
100+
src={post()!.featured_image ?? ""}
101101
alt={post()!.title}
102102
class="w-full h-64 md:h-96 object-cover rounded-lg shadow-md mb-8"
103103
/>
@@ -115,8 +115,7 @@ export default function BlogPost(props: BlogPostProps) {
115115
</div>
116116
)}
117117
</div>
118-
119-
<div class="mt-8" innerHTML={post()!.content}></div>
118+
<MarkdownRenderer content={post()!.content} />
120119
</article>
121120
)}
122121
</Show>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { createSignal, onMount } from "solid-js";
2+
3+
export default function MarkdownRenderer(props: { content: string }) {
4+
const [html, setHtml] = createSignal("");
5+
6+
onMount(async () => {
7+
const wasm = await import("../../../md-wasm/pkg");
8+
await wasm.default(); // 初始化 WASM
9+
const rendered = wasm.render_markdown(props.content);
10+
setHtml(rendered);
11+
});
12+
13+
return (
14+
<div
15+
class="prose prose-indigo mx-auto p-4 bg-white rounded shadow-md"
16+
innerHTML={html()}
17+
/>
18+
);
19+
}

0 commit comments

Comments
 (0)