
Security News
The Changelog Podcast: Practical Steps to Stay Safe on npm
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.
@diplodoc/page-constructor-extension
Advanced tools
Page constructor plugin for Diplodoc transformer and builder
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:
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')
]);
}
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.
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
The Page Constructor requires runtime scripts to make content interactive on your page. There are two main approaches to adding these scripts:
Best for:
<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>
Best for:
import '@diplodoc/page-constructor-extension/runtime';
import '@diplodoc/page-constructor-extension/runtime/style';
The runtime automatically detects whether to hydrate or render content based on the content's structure:
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.
There are three ways to initialize the Page Constructor runtime:
Best for:
import {PageConstructorRuntime} from '@diplodoc/page-constructor-extension/react';
function App() {
return (
<>
{/* Your content */}
<PageConstructorRuntime />
</>
);
}
Best for:
// 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.Best for:
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 />}
</>
);
}
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';
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:
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;
},
});
See the example directory for a complete example of how to use this extension.
FAQs
Page constructor plugin for Diplodoc transformer and builder
We found that @diplodoc/page-constructor-extension demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 7 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.

Security News
Experts push back on new claims about AI-driven ransomware, warning that hype and sponsored research are distorting how the threat is understood.

Security News
Ruby's creator Matz assumes control of RubyGems and Bundler repositories while former maintainers agree to step back and transfer all rights to end the dispute.