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":"
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章