@devrelkit/remark-include-code

中文文档
0.1.2发布于 2026/05/22MITTypeScriptESM + CJS近 30 天下载 9079

Remark plugin to include code from external files into markdown/MDX, with region slicing and line-highlight passthrough.

原始使用文档
文档目录12 节
  1. Install
  2. Usage
  3. Region markers
  4. Line highlighting
  5. Whole-file include
  6. Other meta fields pass through
  7. Options
  8. rootDir
  9. highlightStyle
  10. Errors
  11. Why a remark plugin?
  12. License

@devrelkit/remark-include-code

A remark plugin that includes code from external files into markdown / MDX, with named-region slicing and line-highlight passthrough.

The classic problem: code samples in docs drift out of sync with reality because they live as plain text inside the docs. This plugin lets you keep examples as real source files that get tested in CI, then transclude them into your docs at build time.

Install

pnpm add -D @devrelkit/remark-include-code
# or
npm install --save-dev @devrelkit/remark-include-code

Usage

import { remark } from "remark";
import remarkIncludeCode from "@devrelkit/remark-include-code";

const file = await remark()
  .use(remarkIncludeCode, {
    /* options */
  })
  .process(markdown);

In your markdown / MDX, replace inline code with a file= directive on the fence:

```ts file=./examples/create-account.ts#region=create
```

The plugin reads ./examples/create-account.ts (relative to the markdown file), extracts the create region, and replaces the codeblock body. The fence keeps its language tag, so downstream highlighters work normally.

Region markers

Regions in source files use a comment style that's neutral across most languages:

// #region create
const account = createAccount({ payer, lamports });
// #endregion create
# #region create
account = Account(payer=payer, lamports=1_000_000)
# #endregion create

The plugin picks the comment token (// or #) automatically based on the codeblock's language tag.

Supported languages out of the box:

  • // — ts, tsx, typescript, js, jsx, javascript, rust, rs, c, cpp, go, java, swift, kotlin, scala, dart
  • # — py, python, sh, bash, zsh, ruby, yaml, toml, r, perl

Multiple regions with the same name in one file are concatenated (separated by a blank line) — useful for stitching non-contiguous pieces into one example.

The common leading indent of the region body is stripped, so a region defined inside an indented block still renders flush-left.

Line highlighting

Add highlight= to mark lines for visual emphasis:

```ts file=./examples/create-account.ts#region=create highlight=2-5,10
```

By default the ranges are appended to the codeblock meta as {2-5,10}, which is the convention used by Shiki, rehype-pretty-code, VitePress, and Astro Starlight. Configure highlightStyle for other renderers (see Options).

Whole-file include

Drop the #region=... to include the whole file:

```ts file=./examples/full-script.ts
```

Other meta fields pass through

Anything other than file= and highlight= is preserved on the meta string:

```ts title="Kit" file=./examples/foo.ts#region=create
```

→ output keeps title="Kit" so renderers like CodeHike's <CodeTabs> see what they expect.

Options

interface PluginOptions {
  rootDir?: string;
  highlightStyle?:
    | "fence-meta"
    | "codehike"
    | ((ctx: HighlightContext) => HighlightResult);
}

rootDir

Resolution root for file= paths. Defaults to the directory of the markdown file being processed (recommended). Set this if you want to centralise examples in one location and reference them from anywhere with the same relative path.

highlightStyle

How highlight= ranges land in the output:

  • "fence-meta" (default): appends {2-5,10} to the codeblock meta string. Compatible with Shiki, rehype-pretty-code, VitePress, Astro Starlight, and most modern markdown highlighters.
  • "codehike": injects // !mark(N:M) (or # !mark(N:M) for hash-comment languages) above each highlighted range, so CodeHike's mark annotation handler picks them up.
  • function: receives the slice + ranges + lang + meta, returns the final code + meta. Use this for any other renderer or custom annotation format.
import remarkIncludeCode, { type HighlightContext } from "@devrelkit/remark-include-code";

remark().use(remarkIncludeCode, {
  highlightStyle: (ctx: HighlightContext) => ({
    code: ctx.code,
    meta: `${ctx.meta} data-lines=${JSON.stringify(ctx.ranges)}`,
  }),
});

Errors

The plugin throws (rather than silently producing wrong output) when:

  • file= path doesn't resolve to a readable file
  • #region=... doesn't exist in the source
  • highlight= range is malformed (abc, 5-2, etc.)

All wrap as IncludeError with the markdown file path included, so build failures point you at the broken doc.

Why a remark plugin?

remark is the markdown half of the unified ecosystem. It parses markdown into an AST (mdast), runs plugins to transform that tree, then serialises back. MDX, Fumadocs, Docusaurus, Next.js, Astro, and most modern markdown stacks compose on top of it — so a remark plugin slots into all of them via a one-line .use(...) registration.

License

MIT

版本与发布记录

@devrelkit/remark-include-code 共发布 2 个版本,首次发布于 2026/05/01;过去 12 个月发布了 2 个版本。

版本发布日期距今
0.1.2latest2026/05/225 个月前
0.1.12026/05/016 个月前

关于 @devrelkit/remark-include-code 的常见问题

@devrelkit/remark-include-code 是什么?

Remark plugin to include code from external files into markdown/MDX, with region slicing and line-highlight passthrough.

如何安装 @devrelkit/remark-include-code?

在项目目录运行 npm install @devrelkit/remark-include-code,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:unified(^11.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@devrelkit/remark-include-code 支持 TypeScript 吗?

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

@devrelkit/remark-include-code 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@devrelkit/remark-include-code 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=18。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@devrelkit/remark-include-code 可以商用吗?

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

@devrelkit/remark-include-code 的下载量有多少?

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

@devrelkit/remark-include-code 还在维护吗?

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