Skip to content

Commit ba5da8f

Browse files
committed
added option to pass handlers to hast-util-to-mdast for better control over html to markdown conversion
1 parent 66af6b4 commit ba5da8f

File tree

3 files changed

+11
-13
lines changed

3 files changed

+11
-13
lines changed

index.js

+5-9
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ var parse = require("rehype-parse");
33
var rehype2remark = require("rehype-remark");
44
var stringify = require("remark-stringify");
55
var mdx = require("@mdx-js/mdx");
6-
var toHtml = require("hast-util-to-html");
76

87
const babel = require(`@babel/core`);
98
const BabelPluginPluckImports = require(`babel-plugin-pluck-imports`);
@@ -12,16 +11,13 @@ const objRestSpread = require(`@babel/plugin-proposal-object-rest-spread`);
1211
const htmlAttrToJSXAttr = require(`./babel-plugin-html-attr-to-jsx-attr`);
1312
const removeExportKeywords = require(`babel-plugin-remove-export-keywords`);
1413

15-
function iframe(h, node) {
16-
return h(node, "html", toHtml(node, { space: "html" }));
17-
}
1814

19-
var processor = unified()
20-
.use(parse)
21-
.use(rehype2remark, { handlers: { iframe } })
22-
.use(stringify);
15+
module.exports = async (htmlInput, handlers) => {
16+
const processor = unified()
17+
.use(parse)
18+
.use(rehype2remark, { handlers: { ...handlers } })
19+
.use(stringify);
2320

24-
module.exports = async (htmlInput) => {
2521
const markdown = processor.processSync(htmlInput);
2622
const compiledMarkdown = await mdx(markdown);
2723

package.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@draftbox-co/html-to-compiled-mdx",
33
"description": "Utility to convert HTML to compiled MDX, for consumption in MDX renderer.",
4-
"version": "1.0.1",
4+
"version": "1.0.2",
55
"main": "index.js",
66
"license": "MIT",
77
"dependencies": {
@@ -16,12 +16,14 @@
1616
"camelcase-css": "2.0.1",
1717
"change-case": "4.1.1",
1818
"hast-util-to-html": "^7.1.1",
19+
"hast-util-to-mdast": "^7.1.1",
1920
"rehype": "11.0.0",
2021
"rehype-parse": "7.0.0",
2122
"rehype-remark": "8.0.0",
2223
"remark": "12.0.0",
2324
"style-to-object": "0.3.0",
2425
"unified": "9.0.0",
25-
"unified-stream": "1.0.6"
26+
"unified-stream": "1.0.6",
27+
"unist-util-visit": "^2.0.2"
2628
}
2729
}

yarn.lock

+2-2
Original file line numberDiff line numberDiff line change
@@ -1311,7 +1311,7 @@ hast-util-to-html@^7.1.1:
13111311
unist-util-is "^4.0.0"
13121312
xtend "^4.0.0"
13131313

1314-
hast-util-to-mdast@^7.0.0:
1314+
hast-util-to-mdast@^7.0.0, hast-util-to-mdast@^7.1.1:
13151315
version "7.1.1"
13161316
resolved "https://registry.yarnpkg.com/hast-util-to-mdast/-/hast-util-to-mdast-7.1.1.tgz#1fc4440fc670273a1a7041584be08e5bb1d037ac"
13171317
integrity sha512-MXD6n7sxvmg1ntpugcGAE0AD+SrZWUqlJgLjYZr93697s3TWgsrs/PGXcgk/E7mvcj4q85tZy5grsMDKzmqBxA==
@@ -2123,7 +2123,7 @@ unist-util-visit-parents@^3.0.0:
21232123
"@types/unist" "^2.0.0"
21242124
unist-util-is "^4.0.0"
21252125

2126-
[email protected], unist-util-visit@^2.0.0:
2126+
[email protected], unist-util-visit@^2.0.0, unist-util-visit@^2.0.2:
21272127
version "2.0.2"
21282128
resolved "https://registry.yarnpkg.com/unist-util-visit/-/unist-util-visit-2.0.2.tgz#3843782a517de3d2357b4c193b24af2d9366afb7"
21292129
integrity sha512-HoHNhGnKj6y+Sq+7ASo2zpVdfdRifhTgX2KTU3B/sO/TTlZchp7E3S4vjRzDJ7L60KmrCPsQkVK3lEF3cz36XQ==

0 commit comments

Comments
 (0)