File tree Expand file tree Collapse file tree 8 files changed +231
-4
lines changed
Expand file tree Collapse file tree 8 files changed +231
-4
lines changed Original file line number Diff line number Diff line change 1+ /target
Original file line number Diff line number Diff line change 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"
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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"
Original file line number Diff line number Diff line change 11import { createResource , Show } from "solid-js" ;
22import http from "@/lib/axios" ;
3+ import MarkdownRenderer from "@/components/solid/MarkdownRenderer" ;
34
45// 定义文章接口
56interface Post {
@@ -51,7 +52,6 @@ async function fetchPost(slug: string): Promise<Post | null> {
5152 }
5253}
5354
54-
5555interface 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 >
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments