Skip to content

How would I parse markdown h1 into rehype-document title property #525

Answered by wooorm
phortuin asked this question in Q&A
Discussion options

You must be logged in to vote

this is more of a rehype thing indeed, as we’re not worried about markdown in this case.

Typically, in unified, everything can be done with plugins: ast transforms. To solve this, one could write a custom plugin to find the h1, and the title, and when both exist, get the text content of the heading and inject it into the title. For inspiration, see this pseudocode:

var select = require('hast-util-select').select
var fromString = require('hast-util-from-string')
var toString = require('hast-util-to-string')

.use(myPlugin)

function myPlugin() {
  return myTransformer
  function myTransformer(tree) {
    var h1 = select('h1', tree)
    var title = select('title', tree)
    if (h1 && title) {

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@phortuin
Comment options

Answer selected by wooorm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants