@beoe/fenceparser

中文文档
2.3.1发布于 2025/04/15MITTypeScriptES Module零依赖近 30 天下载 2555

A well-tested parser for parsing metadata out of fenced code blocks in Markdown

原始使用文档
文档目录3 节
  1. Overview ・
  2. Syntax
  3. Acknowledgements

Fork of https://github.com/frencojobs/fenceparser


A tiny, well-tested parser for parsing metadata out of fenced code blocks in Markdown.


Overview ・ npm bundle size Codecov

Assuming you have this code fence in your Markdown,

 ```ts twoslash {1-3, 5} title="Hello, World"

Using remark will yield two information about that code block, lang and meta like this.

{
  "lang": "ts",
  "meta": "twoslash {1-3, 5} title=\"Hello, World\""
}

Use fenceparser to parse the meta string out to a useful object.

import parse from 'fenceparser'

console.log(parse(meta))

// {
//   twoslash: true,
//   highlight: { '1-3': true, '5': true },
//   title: 'Hello, World'
// }

The parser won't intentionally handle parsing the language part since it is usually handled by the Markdown parsers.

But if you want to allow loose syntax grammars such as ts{1-3, 5} as well as ts {1-3, 5} which is used by gatsby-remark-vscode as an example, remark won't parse the language correctly.

{
  "lang": "ts{1-3,", // because remark uses space to split
  "meta": "5}"
}

In these cases, you can use the the library's lex function to get a properly tokenized array. You may then take out the first element as lang. For example,

import {lex, parse} from 'fenceparser'
// Notice this ^ parse is not the same the default export function

const full = [node.lang, node.meta].join(' ') // Join them back

const tokens = lex(full)
const lang = tokens.shift() // ts
const meta = parse(tokens) // { highlight: {'1-3': true, '5': true} }

Syntax

The syntax grammar is loosely based on techniques used by various syntax-highlighters. Rules are such that

  • Valid HTML attributes can be used, attribute, data-attribute, etc.
  • Just like in HTML, top-level attribute names are case insensitive
  • Attributes without values are assigned as true
  • Attribute values can be single or double quoted strings, int/float numbers, booleans, objects or arrays
  • Non-quoted strings are valid as long as they are not separated by a whitespace or a line-break, attr=--theme-color
  • Objects can accept valid attributes as children, or valid attributes with value assigned by : keyword, {1-3, 5, ids: {7}}
  • Arrays are just like JavaScript's arrays
  • Objects without attribute keys {1-3} {7} are merged and assigned to the highlight object
  • No trailing commas

Acknowledgements

  1. This project is made initially to use with Twoslash.
  2. The initial implementations of lexer and parser are based on the examples from the book Crafting Interpreters.

版本与发布记录

@beoe/fenceparser 共发布 2 个版本,首次发布于 2024/11/30;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.3.1latest2025/04/151.6 年前
2.3.02024/11/301.9 年前

关于 @beoe/fenceparser 的常见问题

@beoe/fenceparser 是什么?

A well-tested parser for parsing metadata out of fenced code blocks in Markdown

如何安装 @beoe/fenceparser?

在项目目录运行 npm install @beoe/fenceparser,或使用 pnpm add / yarn add / bun add。

@beoe/fenceparser 支持 TypeScript 吗?

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

@beoe/fenceparser 支持 ESM 还是 CommonJS?

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

@beoe/fenceparser 可以商用吗?

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

@beoe/fenceparser 的下载量有多少?

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

@beoe/fenceparser 还在维护吗?

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