@nullpinter/satteri-sectionize
v0.3.0
Published
Satteri plugin for wrapping heading sections in section elements
Maintainers
Readme
@nullpinter/satteri-sectionize
Satteri plugin for wrapping heading sections in <section> elements.
Usage
import { sectionize } from "@nullpinter/satteri-sectionize";
import { markdownToHtml } from "satteri";
const result = await markdownToHtml(markdown, {
mdastPlugins: [sectionize()],
});The plugin mirrors remark-sectionize: headings are wrapped together with the
following sibling nodes until the next heading at the same or a higher level.
It requires Satteri ^0.11.5. The implementation uses the parent and sibling index
APIs, custom nodes, and ctx.setField to update section children.
Options
const result = await markdownToHtml(markdown, {
mdastPlugins: [sectionize({ maxDepth: 4 })],
});maxDepthcontrols the deepest heading level that starts a section and defaults to6.
MDX ESM declarations such as export remain outside section elements and end
the preceding section, matching remark-sectionize.
Generated nodes
Generated sections use Satteri custom nodes with this shape:
{
type: "section",
data: {
hName: "section",
depth: 2,
},
children: [],
}data.hName renders the node as <section>, while data.depth records the
heading depth that opened it. The implementation does not use directive or
blockquote nodes, so plugins that visit either kind will only receive actual
source nodes.
Plugin order
Place plugins that add, remove, or change headings before sectionize().
Plugins that consume the generated section containers should run after it:
mdastPlugins: [rewriteHeadings(), sectionize(), consumeSections()]Later plugins can identify generated sections by checking node.type ===
"section", or use the exported type guard:
import { isSectionNode } from "@nullpinter/satteri-sectionize";
if (isSectionNode(node)) {
console.log(node.data.depth);
}