GithubHelp home page GithubHelp logo

jrson83 / rehype-slug-anchor-sectionize Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 111 KB

rehype plugin which wraps headings and their descendants in nested <section> elements and applies slugged anchor links to them

Home Page: https://www.npmjs.com/package/rehype-slug-anchor-sectionize

License: MIT License

TypeScript 100.00%
anchor ast hast heading markdown plugin rehype rehype-plugin section slug

rehype-slug-anchor-sectionize's Introduction

rehype-slug-anchor-sectionize

rehype plugin which wraps headings and their descendants in nested <section> elements and applies slugged anchor links to them.

Contents

What is this?

This package is a unified (rehype) plugin to wrap headings and their descendants in nested <section> elements and apply slugged anchor links to them.

unified is a project that transforms content with abstract syntax trees (ASTs). rehype adds support for HTML to unified. vfile is the virtual file interface used in unified. hast is the HTML AST that rehype uses.

This is a rehype plugin that inspects and transforms hast.

When should I use this?

The plugin tries to minimize the configuration effort and complexity by combining the following already existing similar plugin approaches:

  • sectionize or section
  • section-headings
  • heading-anchor
  • autolink-headings
  • slug

It is useful if you want to use IntersectionObserver or similar to observe changes in the intersection of a target element with an ancestor.

Demo

Inspect the DOM tree and heading elements on any of my Blog posts.

Install

This package is ESM only. In Node.js (version 16+), install with npm:

npm install rehype-slug-anchor-sectionize

In Deno, with esm.sh:

import rehypeSlugAnchorSectionize from 'https://esm.sh/rehype-slug-anchor-sectionize'

In browsers, with esm.sh:

<script type="module">
  import rehypeSlugAnchorSectionize from 'https://esm.sh/rehype-slug-anchor-sectionize?bundle'
</script>

Use

Say our module example.js looks as follows:

import { unified } from 'unified'
import remarkParse from 'remark-parse'
import remarkRehype from 'remark-rehype'
import rehypeSlugAnchorSectionize from 'rehype-slug-anchor-sectionize'
import rehypeStringify from 'rehype-stringify'

const file = await unified()
  .use(remarkParse)
  .use(remarkRehype)
  .use(rehypeSlugAnchorSectionize)
  .use(rehypeStringify).process(`## Heading 2.1

Paragraph 1

## Heading 2.2

### Heading 3.1

Paragraph 3

## Heading 2.3

Paragraph 4`)

console.log(value)

…now running node example.js yields HTML Output::

<section id="heading-21-section">
  <h2 id="heading-21" role="presentation">
    <a href="#heading-21" class="heading-anchor">
      <button type="button" class="heading-anchor__btn">#</button>
    </a>
    <span role="heading" aria-level="2">Heading 2.1</span>
  </h2>
  <p>Paragraph 1</p>
</section>

<section id="heading-22-section">
  <h2 id="heading-22" role="presentation">
    <a href="#heading-22" class="heading-anchor">
      <button type="button" class="heading-anchor__btn">#</button>
    </a>
    <span role="heading" aria-level="2">Heading 2.2</span>
  </h2>
  <p>Paragraph 2</p>
  <section id="heading-31-section">
    <h3 id="heading-31" role="presentation">
      <a href="#heading-31" class="heading-anchor">
        <button type="button" class="heading-anchor__btn">#</button>
      </a>
      <span role="heading" aria-level="3">Heading 3.1</span>
    </h3>
    <p>Paragraph 3</p>
  </section>
</section>

<section id="heading-23-section">
  <h2 id="heading-23" role="presentation">
    <a href="#heading-23" class="heading-anchor">
      <button type="button" class="heading-anchor__btn">#</button>
    </a>
    <span role="heading" aria-level="2">Heading 2.3</span>
  </h2>
  <p>Paragraph 4</p>
</section>

Styling

The following styles should be added as minimal setup.

h2 a[class="heading-anchor"],
h3 a[class="heading-anchor"] {
  margin-left: -2rem;
}

.heading-anchor__btn {
  all: unset; /** replace with buttom reset */
  width: 2rem;
  opacity: 0;
  color: #eee;
}

h2:hover a[class="heading-anchor"] .heading-anchor__btn,
h3:hover a[class="heading-anchor"] .heading-anchor__btn {
  opacity: 1;
  cursor: pointer;
}

You can change the class names by providing a custom property for the element class attributes.

API

This package exports no identifiers. The default export is rehypeSlugAnchorSectionize.

unified().use(rehypeSlugAnchorSectionize, options?)

  • Wraps headings and their descendants in <section> elements.
  • In addition it applies slugged anchor-id's to the <section> and heading elements.
options

Configuration (optional).

options.depth

The heading level depth to sectionize. (number, default: 3).

options.wrapperTagName

The wrapper Element tagName. (string, default: section).

options.wrapperProperties

The wrapper Element properties. (Element['properties'], default: undefined).

options.wrapperSlugAdditive

Adds an additive at end of the wrappers id. (string, default: -section).

options.linkProperties

The link Element properties. (Element['properties'], default: { className: 'heading-anchor' }).

options.buttonProperties

The button Element properties. (Element['properties'], default: { className: 'heading-anchor__btn' }).

Types

This package is fully typed with TypeScript. The additional type RehypeSlugAnchorSectionizeOptions is exported.

Security

As improper use of HTML can open you up to a [cross-site scripting (XSS)][xss] attacks, use of rehype can also be unsafe. Use [rehype-sanitize][rehype-sanitize] to make the tree safe.

Use of rehype plugins could also open you up to other attacks. Carefully assess each plugin and the risks involved in using them.

License

MIT

rehype-slug-anchor-sectionize's People

Contributors

jrson83 avatar renovate[bot] avatar

Watchers

 avatar

rehype-slug-anchor-sectionize's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Awaiting Schedule

These updates are awaiting their schedule. Click on a checkbox to get an update now.

  • build(deps): update dependency @biomejs/biome to v1.8.2
  • build(deps): update dependency @types/node to v20.14.7
  • build(deps): update dependency unified to v11.0.5
  • build(deps): update dependency tsx to v4.15.7
  • build(deps): update dependency typescript to v5.5.2
  • build(deps): update pnpm to v9.4.0
  • build(deps): lock file maintenance

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/node-ci.yml
  • actions/checkout v4.1.7@692973e3d937129bcbf40652eb9f2f61becf3332
  • pnpm/action-setup v3.0.0@a3252b78c470c02df07e9d59298aecedc3ccdd6d
  • actions/setup-node v4.0.2@60edb5dd545a775178f52524783378180af0d1f8
npm
package.json
  • github-slugger ^2.0.0
  • hast-util-to-string ^3.0.0
  • unist-util-find-after ^5.0.0
  • unist-util-visit ^5.0.0
  • @biomejs/biome ^1.6.2
  • @types/hast ^3.0.4
  • @types/node ^20.11.30
  • husky ^9.0.11
  • nano-staged ^0.8.0
  • rehype-stringify ^10.0.0
  • remark-parse ^11.0.0
  • remark-rehype ^11.1.0
  • tsx ^4.7.1
  • typescript ^5.4.3
  • unified ^11.0.4
  • unist-util-is ^6.0.0
  • node >=16.0.0
  • pnpm 9.1.4

  • Check this box to trigger a request for Renovate to run again on this repository

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.