Giter Club home page Giter Club logo

Comments (9)

jake-low avatar jake-low commented on June 23, 2024 2

That functionality isn't built into this plugin, but it is possible. Here's an example.

// This rehype plugin traverses <section> elements in the document, and if that section's first
// child is a heading (h1, h2, etc), it moves that child's `id` attribute to the section element.

const visit = require('unist-util-visit-parents')

const MAX_HEADING_DEPTH = 6
const HEADINGS = ["h1", "h2", "h3", "h4", "h5", "h6"]

module.exports = plugin

function plugin () {
  return transform
}

function transform (tree) {
  visit(
    tree,
    node => node.tagName === "section",
    update
  )
}

function update (section) {
  if (section.children && section.children[0] && HEADINGS.includes(section.children[0].tagName)) {
    const heading = section.children[0]
    const { id, ...rest } = heading.properties
    section.properties.id = id
    heading.properties = rest
  }
}

The code above is a rehype plugin: it transforms HTML ASTs, not Markdown ASTs. Add it to your project and name it something sensible, like rehype-heading-ids-to-section-ids.js. Then to use it, you'd do something like this.

var unified = require("unified") // unified is the text processing interface that powers remark and rehype

var processor = unified()
  .use(require("remark-parse")) // parse markdown text to an AST
  .use(require("remark-sectionize")) // wraps stuff in sections (transforming the AST)
  .use(require("remark-rehype")) // convert the markdown AST to an HTML AST
  .use(require("rehype-slug")) // adds `id` attributes to headings
  .use(require('./rehype-heading-ids-to-section-ids.js')) // the filename for the above plugin code
  .use(require("rehype-stringify")) // convert the modified HTML AST to a string

var outputHTML = processor.process(inputMarkdown)

There's no reason that you couldn't modify the plugin shown above to operate directly on the Markdown AST, but I didn't have sample code handy for that.

from remark-sectionize.

TheComputerM avatar TheComputerM commented on June 23, 2024 1

For example give the section a unique id based on the heading text content

from remark-sectionize.

steveruizok avatar steveruizok commented on June 23, 2024 1

Nevermind, looks like that's already in ;)

from remark-sectionize.

steveruizok avatar steveruizok commented on June 23, 2024

I'd love to have a clue as to the depth, like <section data-depth="0">!

from remark-sectionize.

jrson83 avatar jrson83 commented on June 23, 2024

Is it possible to add an id to every section, depending on current heading?

from remark-sectionize.

jrson83 avatar jrson83 commented on June 23, 2024

Oh man thank you, this is great!

from remark-sectionize.

jake-low avatar jake-low commented on June 23, 2024

You're welcome! 🙂

from remark-sectionize.

aquaductape avatar aquaductape commented on June 23, 2024

This should be included as an option for this plugin

from remark-sectionize.

jrson83 avatar jrson83 commented on June 23, 2024

@aquaductape ended up creating my own plugin:

https://github.com/jrson83/jrson.me/blob/main/plugins/unified/rehype/rehypeSectionize.ts

from remark-sectionize.

Related Issues (5)

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.