mdast-util-heading-style

mdast utility to get the style of a heading

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
mdast-util-heading-style
402.0.17 days ago8 years agoMinified + gzip package size for mdast-util-heading-style in KB

Readme

mdast-util-heading-style
!Buildbuild-badgebuild !Coveragecoverage-badgecoverage !Downloadsdownloads-badgedownloads !Sizesize-badgesize !Sponsorssponsors-badgecollective !Backersbackers-badgecollective !Chatchat-badgechat
mdast utility to get the style of a heading.

Contents

*   [`headingStyle(node[, relative])`](#headingstylenode-relative)
*   [`Style`](#style)

What is this?

This package is a tiny utility to figure out if a heading was written as ATX or as setext.
## ATX uses hashes

Setext uses an underline
------------------------

When should I use this?

Probably not a lot! It’s used in remark-lintremark-lint.

Install

This package is ESM onlyesm. In Node.js (version 14.14+ and 16.0+), install with npm:
npm install mdast-util-heading-style

In Deno with esm.shesmsh:
import {headingStyle} from 'https://esm.sh/mdast-util-heading-style@2'

In browsers with esm.shesmsh:
<script type="module">
  import {headingStyle} from 'https://esm.sh/mdast-util-heading-style@2?bundle'
</script>

Use

import {unified} from 'unified'
import {fromMarkdown} from 'mdast-util-from-markdown'
import {headingStyle} from 'mdast-util-heading-style'

headingStyle(fromMarkdown('# ATX').children[0]) // => 'atx'
headingStyle(fromMarkdown('# ATX #\n').children[0]) // => 'atx-closed'
headingStyle(fromMarkdown('ATX\n===').children[0]) // => 'setext'

headingStyle(fromMarkdown('### ATX').children[0]) // => null
headingStyle(fromMarkdown('### ATX').children[0], 'setext') // => 'setext'

API

This package exports the identifier headingStyleapi-headingstyle. There is no default export.

headingStyle(node[, relative])

Get the heading style of a heading, optionally relative to a preferred style.
This is because ATX headings with a depth of three or more could be considered setext.
Parameters
— heading node to check
— relative style
Returns
Style (Styleapi-style) if it can be inferred, null otherwise.

Style

Style of heading (TypeScript type).
Type
type Style = 'atx' | 'atx-closed' | 'setext'

Types

This package is fully typed with TypeScript. It exports the additional type Styleapi-style.

Compatibility

Projects maintained by the unified collective are compatible with all maintained versions of Node.js. As of now, that is Node.js 14.14+ and 16.0+. Our projects sometimes work with older versions, but this is not guaranteed.

Security

Use of mdast-util-heading-style does not involve hast so there are no openings for cross-site scripting (XSS)xss attacks.

Related

— make sure there is no more than a single top-level heading
— use headings as ranges

Contribute

See contributing.mdcontributing in syntax-tree/.githubhealth for ways to get started. See support.mdsupport for ways to get help.
This project has a code of conductcoc. By interacting with this repository, organization, or community you agree to abide by its terms.

License

MITlicense © Titus Wormerauthor