Registry / serialization / mdast-util-heading-style

mdast-util-heading-style

JSON →
library3.0.0jsnpmunverified

mdast-util-heading-style is a focused utility designed to determine the style of a heading within an `mdast` (Markdown Abstract Syntax Tree) node. It identifies if a heading was authored using ATX (hashes, e.g., `## Heading`), ATX-closed (e.g., `## Heading ##`), or Setext (underline, e.g., `Heading\n===`) Markdown syntax. The package is currently at version 3.0.0 and operates within the broader `unified` ecosystem, often leveraged by linting tools like `remark-lint`. Its release cadence is tied to Node.js LTS versions, with major releases typically dropping support for unmaintained Node.js versions. A key differentiator is its precise, narrow scope: providing just the heading style information, rather than modifying or transforming the AST, making it a reliable building block for more complex Markdown processing pipelines. It is an ES module (ESM) only.

npm install mdast-util-heading-style
INSTALL
IMPORT
SIG · MDAST-UTIL-HEADING
M
mdast-util-heading-style
serializationjavascriptv3.0.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

headingStyle
import { headingStyle } from 'mdast-util-heading-style'
const headingStyle = require('mdast-util-heading-style')
This package is ESM-only since v2, requiring Node.js 16+.
Style
import type { Style } from 'mdast-util-heading-style'
Type import for TypeScript users to get the union type 'atx' | 'atx-closed' | 'setext'.

Demonstrates parsing various Markdown heading styles into an `mdast` AST and then using `headingStyle` to determine their type, including how to handle `undefined` results and the `relative` style option.

import { unified } from 'unified'; import { fromMarkdown } from 'mdast-util-from-markdown'; import { headingStyle } from 'mdast-util-heading-style'; import type { Heading } from 'mdast'; // Assuming @types/mdast is installed for type safety const parser = unified().use(fromMarkdown); // Example 1: ATX heading const atxAst = parser.parse('# ATX'); const atxNode = atxAst.children[0] as Heading; console.log(`'# ATX' style: ${headingStyle(atxNode)}`); // Expected output: '# ATX' style: atx // Example 2: ATX closed heading const atxClosedAst = parser.parse('# ATX #\n'); const atxClosedNode = atxClosedAst.children[0] as Heading; console.log(`'# ATX #' style: ${headingStyle(atxClosedNode)}`); // Expected output: '# ATX #' style: atx-closed // Example 3: Setext heading const setextAst = parser.parse('Setext\n==='); const setextNode = setextAst.children[0] as Heading; console.log(`'Setext\n===' style: ${headingStyle(setextNode)}`); // Expected output: 'Setext\n===' style: setext // Example 4: ATX heading with depth 3, potentially ambiguous for setext const depth3Ast = parser.parse('### Another ATX'); const depth3Node = depth3Ast.children[0] as Heading; // By default, it might return undefined if it's not clearly ATX or Setext console.log(`'### Another ATX' style (default): ${headingStyle(depth3Node)}`); // Expected output: '### Another ATX' style (default): undefined // Can be considered 'setext' if relative style is specified console.log(`'### Another ATX' style (relative setext): ${headingStyle(depth3Node, 'setext')}`); // Expected output: '### Another ATX' style (relative setext): setext
Debug
Known issues
breakingThe package transitioned to ESM (ES Modules) only starting from version 2.0.0. CommonJS `require()` is no longer supported.
fix
Refactor your project to use ES Modules (`import` statements). Ensure your `package.json` includes `"type": "module"` or use `.mjs` file extensions for your module files.
affects: >=2.0.0
breakingVersion 3.0.0 and later require Node.js 16 or higher due to ecosystem updates and ESM adoption.
fix
Upgrade your Node.js runtime environment to version 16 or a newer LTS release.
affects: >=3.0.0
breakingThe `headingStyle` function now returns `undefined` instead of `null` when the heading style cannot be inferred (e.g., for an ATX heading of depth 3+ without a `relative` style specified).
fix
Update your code to check for `undefined` instead of `null` when evaluating the return value of `headingStyle`.
affects: >=3.0.0
gotchaVersion 3.0.0 changed to use an `export` map. While generally transparent, this can affect tools or custom setups that relied on internal paths. Always import from the main package name.
fix
Ensure all imports refer to the main package entry point (e.g., `import { headingStyle } from 'mdast-util-heading-style'`) and avoid attempting to import from internal or private file paths.
affects: >=3.0.0
Errors
Common errors & fixes
Error [ERR_REQUIRE_ESM]: require() of ES Module ...mdast-util-heading-style/index.js from ... not supported.
Attempting to use `require()` to import an ES Module-only package.
fix
Migrate your code to use `import` statements (ES Modules). This may involve setting `"type": "module"` in your `package.json` or using `.mjs` file extensions.
TypeError: Cannot read properties of undefined (reading 'headingStyle')
Incorrect CommonJS-style destructuring (`const { headingStyle } = require(...)`) or accessing a non-existent property on a CommonJS `require` result for an ESM-only package.
fix
Use ES Module import syntax: `import { headingStyle } from 'mdast-util-heading-style';`.
Cannot find name 'Style'.
The TypeScript type `Style` was used without being explicitly imported.
fix
Import the type using `import type { Style } from 'mdast-util-heading-style';` in your TypeScript files.
Upgrade
Version history
3.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
4 hits · last 30 days
node
4
Resources
mdast-util-heading-style — npm install mdast-util-heading-style · libregistry