remark-sectionize icon indicating copy to clipboard operation
remark-sectionize copied to clipboard

Add ability to change section attributes based on heading

Open TheComputerM opened this issue 5 years ago • 9 comments

TheComputerM avatar Aug 09 '20 08:08 TheComputerM

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

TheComputerM avatar Aug 09 '20 17:08 TheComputerM

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

steveruizok avatar Aug 29 '20 11:08 steveruizok

Nevermind, looks like that's already in ;)

steveruizok avatar Aug 29 '20 11:08 steveruizok

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

jrson83 avatar Mar 03 '22 22:03 jrson83

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.

jake-low avatar Mar 04 '22 01:03 jake-low

Oh man thank you, this is great!

jrson83 avatar Mar 04 '22 01:03 jrson83

You're welcome! 🙂

jake-low avatar Mar 04 '22 02:03 jake-low

This should be included as an option for this plugin

aquaductape avatar Apr 20 '23 21:04 aquaductape

@aquaductape ended up creating my own plugin:

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

jrson83 avatar Apr 20 '23 23:04 jrson83