remark/rehype and other unified things are used here. Most of the code related to it is in the build/
directory.
@Kikobeats asked if I could review the code, so here goes, in an unordered list of things I came across.
Tips
Cheerio not needed
Everything cheerio is used for, rehype can also do. Cheerio is often easier, because it’s like the DOM, and thus more familiar to developers. It’s also used more, so probably less buggy. However, it’s heavy to load, and so is rehype, and functionalities overlap, so it’s probably better to pick one.
Move stuff to plugins and utilities when possible
...and publish them! So the rest of the unified community can benefit from them as well!
Is markdown linted?
|
import remarkPreset from 'remark-preset-lint-recommended' |
A linting preset is used, is that needed? What’s done with the lint warnings?
Don’t use remark-html
https://github.com/IBM/nicedoc.io/blob/master/core/build/html.js
remark-html
is essentially remark-rehype
and rehype-stringify
combined. But the downside is that you can’t use other rehype plugins in combination with it. So you need to parse the document again. Which is what’s happening in html.js
.
You can instead use one processor like so:
export default async data => {
const { contents: html } = await unified()
.use(remarkParse)
.use(remarkRehype)
.use(rehypeSlug)
.use(rehypePrism, { ignoreMissing: true, preLangClass: false })
.use(rehypeStringify)
.process(data)
return html
}
Use unified instead of remark
or rehype
directly
remark
is unified
+ remark-parse
+ remark-stringify
, rehype
is unified
+ rehype-parse
+ rehype-stringify
. If you’re using a different -stringify
, such as rehype-stringify
, you are loading an unneeded stringier (remark-stringify
).
If you use unified
directly, it’s more explicit which parser and stringifier is used! And you won’t duplicate them.
unified supports promises!
No need to use util.promisify
, .process(data)
without a callback already returns a promise!