-
I’m trying to find a way to parse a markdown # My document
This is my document Index.js should be something like this: const processor = unified()
.use(markdown)
.use(/** magic plugin */)
.use(remark2rehype)
.use(doc, {
title: /** magic property */
})
.use(stringify) Output: <!doctype html>
<html>
<head>
<title>My document</title>
</head>
<body>
<h1>My document</h1>
<p>This is my document</p>
</body>
</html> The |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
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 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) {
fromString(title, toString(h1))
}
}
} |
Beta Was this translation helpful? Give feedback.
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 thetitle
, and when both exist, get the text content of the heading and inject it into the title. For inspiration, see this pseudocode: