@agentofuser/rehype-section

Wrap headings and their content into (nested) sections

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
@agentofuser/rehype-section
6461.0.55 years ago5 years agoMinified + gzip package size for @agentofuser/rehype-section in KB

Readme

rehype-section
!Downloadsdownloads-badgedownloads !Chatchat-badgechat
Wraps headings and their contents in <section> elements.

Installation

npm install @agentofuser/rehype-section
## Usage

Say we have the following file, `fragment.html`:

<!-- prettier-ignore -->
```html
<h1>h1</h1>
  <h2>h2a</h2>
    <h3>h3ai</h3>
      <p>text3ai</p>
    <h3>h3aj</h3>
      <p>text3aj</p>
  <h2>h2b</h2>
    <h3>h3bi</h3>
      <h4>h4bix</h4>
        <p>text4bix</p>
  <h2>h2c</h2>
    <h3>h3ci</h3>
      <p>text3ci</p>

And our script, example.js, looks as follows:
var fs = require('fs')
var rehype = require('rehype')
var section = require('rehype-section')

rehype()
  .data('settings', { fragment: true })
  .use(section)
  .process(fs.readFileSync('fragment.html'), function(err, file) {
    if (err) throw err
    console.log(String(file))
  })

Now, running node example yields:
<section class="h0Wrapper headingWrapper">
  <section class="h1Wrapper headingWrapper">
    <h1>h1</h1>
    <section class="h2Wrapper headingWrapper">
      <h2>h2a</h2>
      <section class="h3Wrapper headingWrapper">
        <h3>h3ai</h3>
        <p>text3ai</p>
      </section>
      <section class="h3Wrapper headingWrapper">
        <h3>h3aj</h3>
        <p>text3aj</p>
      </section>
    </section>
    <section class="h2Wrapper headingWrapper">
      <h2>h2b</h2>
      <section class="h3Wrapper headingWrapper">
        <h3>h3bi</h3>
        <section class="h4Wrapper headingWrapper">
          <h4>h4bix</h4>
          <p>text4bix</p>
        </section>
      </section>
    </section>
    <section class="h2Wrapper headingWrapper">
      <h2>h2c</h2>
      <section class="h3Wrapper headingWrapper">
        <h3>h3ci</h3>
        <p>text3ci</p>
      </section>
    </section>
  </section>
</section>

API

rehype().use(section)

Wraps headings and their contents in <section> elements.

Contribute

See contributing.md in rehypejs/rehypecontribute for ways to get started.
This organisation has a Code of Conductcoc. By interacting with this repository, organisation, or community you agree to abide by its terms.

License

MITlicense © Agent of Userauthor