@diplodoc/page-constructor-extension

中文文档
0.13.17发布于 2026/10/28MITTypeScriptCommonJS构建来源已验证近 30 天下载 3307

Page constructor plugin for Diplodoc transformer and builder

原始使用文档
文档目录10 节
  1. Quickstart
  2. Syntax
  3. MarkdownIt transform plugin
  4. Prepared runtime
  5. Rendering Detection
  6. Initialization Methods
  7. Transform styles and runtime
  8. React hooks
  9. Link Resolvers
  10. Example

english | русский


NPM version Quality Gate Status Coverage Maintainability Rating Reliability Rating Security Rating

Diplodoc page-constructor extension

This is an extension of the Diplodoc platform, which allows using the Page Constructor component from Gravity UI in your documentation. Page Constructor is a library for rendering web pages or their parts based on YAML data. This extension enables creating rich, interactive page layouts with various block types directly in your markdown files.

The extension contains some parts:

Quickstart

Attach the plugin to the transformer:

import pageConstructorExtension from '@diplodoc/page-constructor-extension';
import transform from '@diplodoc/transform';

const {result} = await transform(
  `
::: page-constructor
blocks:
  - type: 'header-block'
    title: 'My Header'
    description: 'This is a description'
:::
`,
  {
    plugins: [pageConstructorExtension.transform({bundle: false})],
  },
);

Don't forget to add the runtime to make page constructor interactive:

// Import the runtime in your application
import '@diplodoc/page-constructor-extension/runtime';
import '@diplodoc/page-constructor-extension/runtime/styles';

// Or include it in your HTML
<script src='_assets/page-constructor.js'></script>
<link rel='stylesheet' href='_assets/page-constructor.css' />

// Or load it conditionally only when needed
import {PAGE_CONSTRUCTOR_RUNTIME} from '@diplodoc/page-constructor-extension/plugin';

// Check if page constructor is used in the content
if (result.meta?.script?.includes(PAGE_CONSTRUCTOR_RUNTIME)) {
  // Load runtime asynchronously
  Promise.all([
    import('@diplodoc/page-constructor-extension/runtime'),
    import('@diplodoc/page-constructor-extension/runtime/styles')
  ]);
}

Syntax

You can use the page constructor in your markdown files using the page-constructor directive:

::: page-constructor
blocks:

- type: 'header-block'
  title: 'My Header'
  description: 'This is a description'
  :::

Note: The blocks: property is required. Direct list format without the blocks: prefix is not supported.

MarkdownIt transform plugin

Plugin for @diplodoc/transform package.

Options:

  • runtime - name of runtime script and style which will be exposed in results script and style sections.
    Can be a string (both script and style will use the same name) or an object with separate script and style properties.
    Default: { script: '_assets/page-constructor.js', style: '_assets/page-constructor.css' }

  • bundle - boolean flag to enable/disable copying of bundled runtime to target directory.
    Where target directory is <transformer output option>/<plugin runtime option>
    Default: true

  • assetLinkResolver - function to resolve asset links (images, videos, etc.) in the page constructor content.
    Signature: (link: string) => string
    Default: undefined

  • contentLinkResolver - function to resolve content links (markdown files, HTML files, etc.) in the page constructor content.
    Signature: (link: string) => string
    Default: undefined

Prepared runtime

The Page Constructor requires runtime scripts to make content interactive on your page. There are two main approaches to adding these scripts:

1. Using Generated Assets

Best for:

  • Static HTML pages
  • Server-rendered applications
  • When you want to separate the runtime from your main application code
<html>
  <head>
    <!-- Assets generated by the MarkdownIt transform plugin -->
    <script src="_assets/page-constructor.js"></script>
    <link rel="stylesheet" href="_assets/page-constructor.css" />
  </head>
  <body>
    ${result.html}
  </body>
</html>

2. Including in Your Bundle

Best for:

  • Single-page applications
  • When using a module bundler (webpack, rollup, etc.)
  • When you want to control versioning of the runtime
import '@diplodoc/page-constructor-extension/runtime';
import '@diplodoc/page-constructor-extension/runtime/styles';

Rendering Detection

The runtime automatically detects whether to hydrate or render content based on the content's structure:

  • Server-rendered content (with pre-rendered HTML) will be hydrated
  • Browser-rendered content (empty placeholder) will be fully rendered

This allows you to use a single runtime that intelligently determines the appropriate rendering method, simplifying integration in mixed environments where both server and browser rendering are used.

Important note about sanitization:

When using server-side rendering (SSR), all HTML passes through the default sanitizer of the transform. However, when rendering on the client side, Page Constructor content is not sanitized automatically. If you need this functionality in client-side rendering scenarios, you need to handle content sanitization yourself to prevent potential security issues.

Initialization Methods

There are three ways to initialize the Page Constructor runtime:

1. React Component

Best for:

  • React applications
  • When you need more control over initialization timing
  • When you want to integrate with React's component lifecycle
import {PageConstructorRuntime} from '@diplodoc/page-constructor-extension/react';

function App() {
  return (
    <>
      {/* Your content */}
      <PageConstructorRuntime />
    </>
  );
}

2. Direct Runtime Import

Best for:

  • Static HTML pages
  • Non-React applications
  • Simple integration scenarios
// Import the runtime - it will automatically detect the rendering type
import '@diplodoc/page-constructor-extension/runtime';

Additional parameters:

  • theme (optional) - Sets the theme for Page Constructor components. Available values: 'light' | 'dark'
  • preMountHook (optional) - Callback function that is called before mounting the component. Receives the container element as parameter.

3. Conditional Runtime Loading

Best for:

  • Performance optimization
  • When you want to load the runtime only if it's needed
  • Large applications where page constructor might not be used on every page
import {useState, useEffect} from 'react';
import {
  transform as pageConstructorPlugin,
  PAGE_CONSTRUCTOR_RUNTIME,
} from '@diplodoc/page-constructor-extension/plugin';
import {PageConstructorRuntime} from '@diplodoc/page-constructor-extension/react';

function App() {
  const {result} = transform(content, {
    plugins: [pageConstructorPlugin()],
  });
  const [runtimeLoaded, setRuntimeLoaded] = useState(false);

  // Asynchronously load runtime only if needed
  useEffect(() => {
    if (result.meta?.script?.includes(PAGE_CONSTRUCTOR_RUNTIME)) {
      // Load runtime asynchronously
      Promise.all([
        import('@diplodoc/page-constructor-extension/runtime'),
        import('@diplodoc/page-constructor-extension/runtime/styles'),
      ]).then(() => {
        setRuntimeLoaded(true);
      });
    }
  }, [result.meta?.script]);

  return (
    <>
      <div dangerouslySetInnerHTML={{__html: result.html}} />
      {runtimeLoaded && <PageConstructorRuntime />}
    </>
  );
}

Transform styles and runtime

The @diplodoc/page-constructor-extension package does not bundle the @diplodoc/transform CSS and JS at runtime, so you need to import them manually:

@import '~@diplodoc/transform/dist/css/yfm.css';
import '@diplodoc/transform/dist/js/yfm';

React hooks

In addition to the PageConstructorRuntime component described in the Initialization Methods section, the extension provides React hooks for more control over the rendering process:

import {
  usePageConstructorController,
  usePageConstructor,
} from '@diplodoc/page-constructor-extension/react';

function MyComponent() {
  // Get the controller
  const controller = usePageConstructorController();

  // Get the render function
  const renderPageConstructors = usePageConstructor();

  // Render page constructors when needed
  useEffect(() => {
    if (controller) {
      renderPageConstructors();
    }
  }, [controller, renderPageConstructors]);

  return <div>My Component</div>;
}

These hooks are useful when you need to:

  • Control when page constructors are rendered
  • Integrate with other React components or libraries
  • Implement custom rendering logic
  • Respond to specific events or state changes

The assetLinkResolver and contentLinkResolver options allow you to customize how links are resolved in the page constructor content. These functions are called for each link in the content and should return the resolved link.

  • assetLinkResolver: Called for links to assets (images, videos, etc.)
  • contentLinkResolver: Called for links to content (markdown files, HTML files, etc.)

Example:

pageConstructorPlugin({
  // Other options...
  assetLinkResolver: (link) => {
    // Add a prefix to asset links
    return link.startsWith('http') ? link : `/assets/${link}`;
  },
  contentLinkResolver: (link) => {
    // Convert .md links to .html
    return link.endsWith('.md') ? link.replace('.md', '.html') : link;
  },
});

Example

See the example directory for a complete example of how to use this extension.

版本与发布记录

@diplodoc/page-constructor-extension 共发布 35 个版本,首次发布于 2025/05/20;过去 12 个月发布了 19 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.13.17latest2026/10/2814 天前
0.13.162026/09/072 个月前
0.13.152026/08/282 个月前
0.13.142026/08/272 个月前
0.13.132026/08/262 个月前
0.13.122026/08/073 个月前

关于 @diplodoc/page-constructor-extension 的常见问题

@diplodoc/page-constructor-extension 是什么?

Page constructor plugin for Diplodoc transformer and builder

如何安装 @diplodoc/page-constructor-extension?

在项目目录运行 npm install @diplodoc/page-constructor-extension,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(^16.8.0 || ^17.0.0 || ^18.0.0 || ^19.0.0)、react-dom(^16.8.0 || ^17.0.0 || ^18.0.0 || ^19.0.0)、markdown-it(^13.0.0)、@diplodoc/transform(^4.78.12)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@diplodoc/page-constructor-extension 支持 TypeScript 吗?

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

@diplodoc/page-constructor-extension 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@diplodoc/page-constructor-extension 需要什么版本的 Node.js?

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

@diplodoc/page-constructor-extension 可以商用吗?

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

@diplodoc/page-constructor-extension 的下载量有多少?

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

@diplodoc/page-constructor-extension 还在维护吗?

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