syntax-tree / mdast-util-to-hast

utility to transform mdast to hast
https://unifiedjs.com
MIT License
101 stars 42 forks source link

Custom handler for unknown nodes #36

Closed Hocdoc closed 4 years ago

Hocdoc commented 4 years ago

For our project it would be really useful to define a custom handler for unknown nodes.

We mix many custom nodes with remark nodes. We need our custom nodes unchanged in the HAST tree after we run mdast-util-to-hast. Writing a custom handler for every custom type wouldn't work for our use case.

At the moment all unknown nodes get converted to text or a div (as defined in one.js ).

I wish to add to a new property like options.unknownHandler = (h, node, parent): Node (or as alternative a special handler "unknown" in the existing options.handlers object).

A handler for unknown nodes would be also useful in the hast-util-to-html project. There all unknown nodes result in an Error.

It could be also used f.ex. to show wrong node types in the HTML output.

If I can convince you about this feature, I can write PRs for mdast-util-to-hast and hast-util-to-html to support a custom unknown-node-handler.

wooorm commented 4 years ago

I think this would be useful for this project, but how would be it be useful for hast-util-to-html?

Hocdoc commented 4 years ago

Use Cases:

But I see your point, we could also just remove all non-HAST-nodes before running hast-util-to-html.

I will write a PR for mdast-util-to-hast next monday.

wooorm commented 4 years ago

I get the metadata in mdast indeed, but I don’t really see what kind of metadata is still useful in a hast AST (or: maybe useful at first, but then you’d transform them away to make valid hast from your custom hast).

An idea that may be of interest: you could use hast to represent HTML custom elements (<mycompany-icon>) too, which are have more meaning to you? You can also add metadata to nodes at the node.data object.

Custom log messages for unknown nodes

I’d suggest using vfile.messages for that, if possible?


Appreciate the PR, thanks!

wooorm commented 4 years ago

Closed by GH-37.