@ecopages/mdx

中文文档
0.7.1发布于 2026/11/13MITTypeScriptES Module构建来源已验证近 30 天下载 3635

MDX plugin for Ecopages

原始使用文档
文档目录6 节
  1. Choose your MDX path
  2. Installation
  3. Usage
  4. Types
  5. React MDX
  6. Dependency discovery in MDX

@ecopages/mdx

Standalone MDX integration for Ecopages third-party JSX runtimes (for example @kitajs/html). For React or Ecopages JSX MDX, use the owning integration plugin instead.

Choose your MDX path

RuntimePlugin
ReactreactPlugin({ mdx: {} })
Ecopages JSXecopagesJsxPlugin({ mdx: {} })
Third-partymdxPlugin({ compilerOptions: { jsxImportSource: '...' } })

Installation

bun add @ecopages/mdx @mdx-js/mdx @kitajs/html

@mdx-js/mdx is a peer dependency. Install the JSX runtime you set in compilerOptions.jsxImportSource.

Usage

import { defineConfig } from '@ecopages/core/config';
import { mdxPlugin } from '@ecopages/mdx';

export default defineConfig({
    integrations: [
        mdxPlugin({
            compilerOptions: {
                jsxImportSource: '@kitajs/html',
            },
        }),
    ],
});

compilerOptions.jsxImportSource is required. react and @ecopages/jsx are rejected — use reactPlugin or ecopagesJsxPlugin for those runtimes.

MDX uses core's shared route renderer. Its page-module normalization and Page Browser Graph dependencies are supplied through the Integration hook factory; compiler options are consumed by the MDX build plugin.

Types

import type { JsxImportSource, KnownJsxImportSource, StandaloneMdxCompilerOptions } from '@ecopages/mdx';

KnownJsxImportSource is '@kitajs/html' | 'react' | '@ecopages/jsx'. JsxImportSource also accepts custom runtime strings.

Where remarkPlugins, rehypePlugins, and recmaPlugins go depends on the plugin:

  • Standalone mdxPlugin() takes them inside compilerOptions, next to jsxImportSource. It has no top-level plugin lists.
  • reactPlugin({ mdx }) and ecopagesJsxPlugin({ mdx }) take them at the top level of mdx, next to compilerOptions, and do not accept them inside compilerOptions. Both use the shared MdxCompilerOptionsInput shape exported from @ecopages/mdx/core.

React MDX

Do not use standalone mdxPlugin() for React apps:

import { reactPlugin } from '@ecopages/react';

reactPlugin({
    mdx: {},
});

Providing an mdx object enables MDX; omit it to disable MDX. Plugin lists belong at the top level next to compilerOptions:

import remarkGfm from 'remark-gfm';

reactPlugin({
    mdx: {
        compilerOptions: { format: 'mdx' },
        remarkPlugins: [remarkGfm],
    },
});

The React MDX loader derives its file filter from mdx.extensions, which replace compilerOptions.mdxExtensions rather than merging with them. Declaring a custom extension such as ['.react.mdx'] means only those files compile with React — plain .mdx files in the same build stay with whichever integration owns them. MDX entries compiled by React are React components and must be rendered through React-owned routes; see @ecopages/content-processor for the content-entry ownership contract.

Dependency discovery in MDX

The core MDX loader plugin automatically discovers top-level component and stylesheet imports in MDX files across all integrations. projectRoot is required and comes from the app config (rootDir); the loader throws if it is missing.

  • Local components: Top-level imports of local eco.component() declarations are added to config.dependencies.components.
  • Stylesheets: Relative and TSConfig-aliased bare CSS imports (e.g. import './post.css'; or import '@/styles/post.css';) are stripped from the compiled JavaScript. Discovered paths stay on Component identity until collection; they are not copied into config.dependencies.stylesheets. Explicit dependencies.stylesheets entries remain authoritative for attributes such as media.
  • Code blocks and functions: Markdown code blocks containing import statements and dynamic imports inside functions are distinguished from top-level ESM declarations and remain untouched.
  • Config synthesis: Every compiled MDX module is attributed with bindComponentIdentity and attachDiscoveredDependencies. If export const config = { ... } is already present, discovered Components merge with explicit ones. The standalone renderer preserves the page config's existing id and integration, attaches the current page file path, and passes imported MDX layouts to dependency collection with their own identity intact.
  • Source maps: createMdxLoaderPlugin() composes the compiler map with component identity edits and returns the result to the build adapter. Set includeSourceMap: false in its options to omit the map.

版本与发布记录

@ecopages/mdx 共发布 124 个版本,首次发布于 2026/03/09;过去 12 个月发布了 124 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.7.1latest2026/11/13今天
0.7.02026/11/094 天前
0.6.12026/10/3014 天前
0.6.02026/10/2915 天前
0.5.02026/10/2420 天前
0.4.02026/10/2222 天前

其他发布通道:alpha → 0.2.0-alpha.55,beta → 0.2.0-beta.43,rc → 0.2.0-rc.14。

关于 @ecopages/mdx 的常见问题

@ecopages/mdx 是什么?

MDX plugin for Ecopages

如何安装 @ecopages/mdx?

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

@ecopages/mdx 支持 TypeScript 吗?

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

@ecopages/mdx 支持 ESM 还是 CommonJS?

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

@ecopages/mdx 需要什么版本的 Node.js?

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

@ecopages/mdx 可以商用吗?

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

@ecopages/mdx 的下载量有多少?

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

@ecopages/mdx 还在维护吗?

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