@beoe/rehype-code-hook

中文文档
0.1.1发布于 2025/04/15MITTypeScriptES Module近 30 天下载 2600

rehype plugin for code fences

原始使用文档
文档目录7 节
  1. Usage
  2. Tips
  3. Meta string
  4. Astro
  5. Starlight
  6. Images instead of inline SVG
  7. TODO

@beoe/rehype-code-hook

Rehype plugin to make it easier to write custom processors for code e.g.:

Block-code:
<pre><code class="language-js">const x = 1;</code></pre>

Inline-code:
<code>x</code>

In markdown it corresponds to:

Block-code:

```js
const x = 1;
```

Inline-code:
`x`

This plugin is usefull if you want to create rehype plugin to:

  • highlight code, like @shikijs/rehype, rehype-prism, rehype-highlight etc.
  • render diagrams, like rehype-mermaid
  • do something else, like rehype-color-chips

Usage

Basic example looks like this:

import { rehypeCodeHook } from "@beoe/rehype-code-hook";
import { generateSvg } from "./generateSvg.js";

export const rehypeExampleDiagram = (options = {}) => {
  return rehypeCodeHook({
    ...options,
    code: ({ code }) => generateSvg(code),
  });
};

If you have code like this (it is more clear in Markdown notation, but plugin actually works at rehype level):

```js {1,10}
const x = 1;
```

code callback would be called with:

{
  code: "const x = 1;\n",
  inline: false,
  language: "js",
  meta: "{1,10}"
}

If you have code like this:

`const x = 1;`

code callback would be called with:

{
  code: "const x = 1;",
  inline: true,
  language: undefined,
  meta: undefined
}

Now it is time to render your thing:

  • you can check code, inline, language, meta and if this is not the block you are looking for you can return undefined - and block would be unchanged
  • if you decided to render something you can return string (for example SVG or HTML), you can return HAST fragment or you can return promise of string or HAST
    • if return value is HAST fragment it will replace whole code block
    • if return value is string it would be converted to HAST and then will replace whole code block
    • if return value is promise it will wait until it resolves and do one of steps above

You can configure your plugin to be called only for specific cases, for example:

  • only for language example: rehypeCodeHook({language: "example",..})
  • only for inline code: rehypeCodeHook({inline: true,..})

To enable caching you need to pass Map-like storage:

rehypeCodeHook({ code, cache: new Map(), hashTostring: true });

I checked it with @beoe/cache, but it suppose to work with any storage that has Map like interface. You may pass additional salt param to reset cache, for example when configuration of your plugin changed.

Tips

Meta string

If you need to parse meta param you can use, for example:

Astro

If you want to use rehype plugin for code elements in Astro you need to disable built-in highlighter and then make sure it will appear after your rehype plugin

import { rehypeShiki, markdownConfigDefaults } from "@astrojs/markdown-remark";

export default defineConfig({
  markdown: {
    syntaxHighlight: false,
    rehypePlugins: [
      // you rehype plugin goes here
      // re-enable default Astro code highlighter
      [rehypeShiki, markdownConfigDefaults.shikiConfig],
    ],
  },
});

Starlight

Important use v0.22+

export default defineConfig({
  markdown: {
    rehypePlugins: [
      // you rehype plugin goes here
      // after you can insert code highlighter
    ],
  },
});

Images instead of inline SVG

My main use-case is to inline SVGs, but if you want to produce images instead you can use data URLs. Something like this:

{
  code: () =>
    `<img src="data:image/png;base64,${dataBuffer.toString("base64")}">`;
}

If you want to convert SVG data URL use mini-svg-data-uri or similar.

I didn't investigate how to render images aside with vfile.

TODO

  • test async function and cache together

版本与发布记录

@beoe/rehype-code-hook 共发布 4 个版本,首次发布于 2024/05/06;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.1.1latest2025/04/151.6 年前
0.1.02024/11/301.9 年前
0.0.22024/09/132 年前
0.0.12024/05/062 年前

关于 @beoe/rehype-code-hook 的常见问题

@beoe/rehype-code-hook 是什么?

rehype plugin for code fences

如何安装 @beoe/rehype-code-hook?

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

@beoe/rehype-code-hook 支持 TypeScript 吗?

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

@beoe/rehype-code-hook 支持 ESM 还是 CommonJS?

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

@beoe/rehype-code-hook 可以商用吗?

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

@beoe/rehype-code-hook 的下载量有多少?

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

@beoe/rehype-code-hook 还在维护吗?

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