markdown-wasm:一個非常快的WebAssembly Markdown解析器

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Rasmus Andersson發佈了"},{"type":"link","attrs":{"href":"https:\/\/github.com\/rsms\/markdown-wasm","title":"","type":null},"content":[{"type":"text","text":"markdown-wasm"}]},{"type":"text","text":",一種從C語言移植到WebAssembly的Markdown解析器,速度非常快。在一次基準測試中,markdown-wasm的速度是最好的JavaScript Markdown解析器的兩倍,而且markdown-wasm體積很小(壓縮後只有31KB)。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在一個基準測試中,使用markdown-wasm和其他流行的Markdown解析器(例如"},{"type":"link","attrs":{"href":"https:\/\/github.com\/markdown-it\/markdown-it","title":"","type":null},"content":[{"type":"text","text":"markdown-it"}]},{"type":"text","text":"、"},{"type":"link","attrs":{"href":"https:\/\/github.com\/commonmark\/commonmark.js\/","title":"","type":null},"content":[{"type":"text","text":"commonmark"}]},{"type":"text","text":"、"},{"type":"link","attrs":{"href":"https:\/\/marked.js.org\/","title":"","type":null},"content":[{"type":"text","text":"marked"}]},{"type":"text","text":"和"},{"type":"link","attrs":{"href":"https:\/\/github.com\/showdownjs\/showdown","title":"","type":null},"content":[{"type":"text","text":"showdown"}]},{"type":"text","text":")解析一系列符合CommonMark規範的"},{"type":"link","attrs":{"href":"https:\/\/github.com\/rsms\/markdown-wasm\/tree\/master\/test\/benchmark\/samples","title":"","type":null},"content":[{"type":"text","text":"示例Markdown文件"}]},{"type":"text","text":",markdown-wasm解析示例文件的速度是排在第二位的markdown-it的兩倍。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/resource\/image\/37\/e0\/374e44a40c9346c5c284bbe59b2b81e0.png","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"(每秒聚合操作,來自"},{"type":"link","attrs":{"href":"https:\/\/github.com\/rsms\/markdown-wasm","title":"","type":null},"content":[{"type":"text","text":"markdown-wasm代碼庫"}]},{"type":"text","text":")"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"它之所以能做到這一點,似乎是因爲它始終比基準測試中的其他解析器運行速度快:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/resource\/image\/69\/ed\/699e50637202d5e1bf9c82d4f0549eed.png","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"(最短和最長解析時間,來自"},{"type":"link","attrs":{"href":"https:\/\/github.com\/rsms\/markdown-wasm","title":"","type":null},"content":[{"type":"text","text":"markdown-wasm代碼庫"}]},{"type":"text","text":")"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"markdown-wasm利用了"},{"type":"link","attrs":{"href":"https:\/\/github.com\/mity\/md4c","title":"","type":null},"content":[{"type":"text","text":"MD4C"}]},{"type":"text","text":",一個用C語言開發的符合CommonMark規範的markdown解析器。markdown-wasm使用wasmc將MD4C移植到WebAssembly,它可以處理C和C++ WASM\/JS項目的編譯、鏈接和打包。markdown-wasm沒有依賴項。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"markdown-wasm提供了一個"},{"type":"link","attrs":{"href":"https:\/\/github.com\/rsms\/markdown-wasm#api","title":"","type":null},"content":[{"type":"text","text":"parse API"}]},{"type":"text","text":",它會獲取Markdown內容(JavaScript字符串或UTF8編碼數據)並將其轉換爲HTML(作爲JavaScript字符串或"},{"type":"link","attrs":{"href":"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Uint8Array","title":"","type":null},"content":[{"type":"text","text":"Uint8Array"}]},{"type":"text","text":"返回)。可以在Node.js中使用markdown-wasm,或者用wasmc從源代碼開始構建。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"markdown-wasm通過減少處理工作量(通過更高效的算法和數據結構)、並行執行(利用多核架構)或者更快地執行計算(例如,編譯而非解釋、AOT編譯而非JIT編譯)來提升性能。在對性能要求較高的場景中,開發人員通常會使用那些設計用來跨多個計算核心高效編譯和執行源代碼的語言(Rust、Go、C語言等)。markdown-wasm趕上了開發人員爲了提升性能而使用編譯到原生(compile-to-native)語言開發軟件的大趨勢,最終移植到了WebAssembly,這樣就可以在各種目標環境和運行時(例如,在瀏覽器中)中運行。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"但性能可能並不總是最重要的。例如,markdown-it解析器支持JavaScript插件擴展。這樣的可擴展性促進了社區貢獻和插件"},{"type":"link","attrs":{"href":"https:\/\/www.npmjs.com\/search?q=keywords:markdown-it-plugin","title":"","type":null},"content":[{"type":"text","text":"生態系統"}]},{"type":"text","text":"的發展。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"開發人員可以在專門的"},{"type":"link","attrs":{"href":"https:\/\/rsms.me\/markdown-wasm\/","title":"","type":null},"content":[{"type":"text","text":"Playground"}]},{"type":"text","text":"上體驗“markdown-wasm”。雖然markdown-wasm支持數學符號(例如$x_0$),但Playground卻不支持。markdown-wasm支持CommonMark規範指定和其他"},{"type":"link","attrs":{"href":"https:\/\/github.com\/mity\/md4c#markdown-extensions","title":"","type":null},"content":[{"type":"text","text":"擴展"}]},{"type":"text","text":"提供的所有特性。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在瀏覽器中可以這樣使用markdown-wasm:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"plain"},"content":[{"type":"text","text":"
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章