@discord/markdown-wasm

中文文档
0.15.0发布于 2026/11/05MITTypeScriptES Module近 30 天下载 34.4万

WASM module for parsing Discord Markdown

原始使用文档
文档目录5 节
  1. Prerequisites
  2. Build
  3. Usage
  4. Initialising
  5. The lossless tree

@discord/markdown-wasm

Prerequisites

Build

pnpm build

Usage

import { parse } from '@discord/markdown-wasm';

const ast = parse('Hello world!');

parse takes optional flags second, picking the syntax to parse as an OR of Syntax bits, which the package also exports. Every syntax parses by default, and with 0:

import { Syntax, parse } from '@discord/markdown-wasm';

const noMentions = parse('@everyone **hi**', Syntax.ALL & ~Syntax.MENTIONS);

Initialising

The package has no top-level await, so importing it never turns the module graph async — it can be require()d, and it bundles for targets that do not support top-level await.

The cost is that the wasm has to be instantiated before the first parse. Node does that itself on first use, reading and compiling the file synchronously. Nowhere else can: the bytes only arrive over a fetch, and browsers refuse to compile buffers over 4KB synchronously on the main thread anyway. So everywhere else, init once at startup:

import { init, parse } from '@discord/markdown-wasm';

await init();

const ast = parse('Hello world!'); // and everything downstream stays sync

If your bundler or host already hands you the wasm, skip the fetch with initSync. Pass a WebAssembly.Module, not bytes — compiling bytes is what the 4KB cap applies to:

import { initSync, parse } from '@discord/markdown-wasm';

initSync(wasmModule);

Parsing before either call throws.

The lossless tree

parseLossless returns a tree for rich text instead: nodes for what the markdown means, and tokens holding every character of the content in order, down to delimiters, escapes, markers and line endings. Joining the tokens' text gives the content back exactly. Every node and token has a span, in UTF-16 code units, so it indexes the string directly:

import { parseLossless } from '@discord/markdown-wasm';

const content = '**hi** 🦶';
const tree = parseLossless(content);
// tree.children[0] is the paragraph; its first child, the bold, spans `**hi**`
const { start, end } = tree.children[0].children[0].span;
content.slice(start, end); // '**hi**'

It takes flags as parse does. The types are LosslessNode and friends in @discord/markdown-types.

版本与发布记录

@discord/markdown-wasm 共发布 27 个版本,首次发布于 2025/05/12;过去 12 个月发布了 17 个版本。

版本发布日期距今
0.15.0latest2026/11/057 天前
0.14.02026/10/2716 天前
0.13.02026/10/2419 天前
0.12.02026/10/1627 天前
0.11.02026/10/1429 天前
0.10.02026/10/071 个月前

关于 @discord/markdown-wasm 的常见问题

@discord/markdown-wasm 是什么?

WASM module for parsing Discord Markdown

如何安装 @discord/markdown-wasm?

在项目目录运行 npm install @discord/markdown-wasm,或使用 pnpm add / yarn add / bun add。

@discord/markdown-wasm 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@discord/markdown-wasm 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@discord/markdown-wasm 可以商用吗?

@discord/markdown-wasm 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@discord/markdown-wasm 的下载量有多少?

根据 npm 官方下载统计,@discord/markdown-wasm 在 2026/10/13 – 2026/11/11 这 30 天内被下载 343,500 次,日均约 11,450 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@discord/markdown-wasm 还在维护吗?

最新版本 0.15.0 发布于 2026/11/05(7 天前);至今共发布 27 个版本,其中过去 12 个月发布了 17 个;首次发布于 2025/05/12。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。