如何像插件 gatsby-transformer-remark 那样将 markdown 转换为 html(对于 NetlifyCMS)

How to transform markdown to html like the plugin gatsby-transformer-remark does (for NetlifyCMS)

我想像 plugin gatsby-transformer-remark 一样将降价字符串转换为 html。

到目前为止我使用了showdown. This works ok, but it's missing features like transforming the image editor component

我想使用与插件 gatsby-transformer-remark 相同的算法转换 markdown。

有现成的解决方案吗?

在对 gatsby-transformer-remark 进行逆向工程后,我找到了这个解决方案:

const remark = require(`remark`);
const mdastToHast = require(`mdast-util-to-hast`);
const hastToHtml = require(`hast-util-to-html`);

const remarker = new remark();

const markdownAst = remarker.parse(data.body);
const htmlAst = mdastToHast(markdownAst, { allowDangorousHtml: true });
const htmlRaw = hastToHtml(htmlAst, { allowDangorousHtml: true });

const html = htmlRaw
  .split('<img src="uploads/')
  .join('<img src="/uploads/');