Closed stefanprobst closed 5 years ago
@stefanprobst Thanks for the issue!
This should not go in data
, as no other markdown -> html compiler (including commonmark) does that. And we like to stick to the standards.
The tests are very useful though. And something that checks the regex as well!
TBH I'm not really familiar with the commonmark spec. Is there any standards-compliant way to not lose the non-language-part of the infostring in the MDAST->HAST transformation?
@stefanprobst similar to data-foo
in HTML, on a dataset
object, it’s also possible to set stuff on data
. See the Unist spec for more info!
Thanks very much for the link! I'm afraid I'm still not totally clear on dataset
properties (i.e. node.properties.dataMeta?). Wouldn't they end up as data-*
attributes when stringified?
As for unist
, I did not know that nodes can have data
, somehow assumed that was only for vfile
. Can data
be passed directly to h()
?
Thanks very much for the link! I'm afraid I'm still not totally clear on dataset properties (i.e. node.properties.dataMeta?). Wouldn't they end up as data-* attributes when stringified?
Yup, they would!
As for unist, I did not know that nodes can have data, somehow assumed that was only for vfile. Can data be passed directly to h()?
Data is for any node, it works for anything! What do you mean with the latter sentence though? 🤔
Thanks for your patience. So to summarize, the two options would be
var lang = node.lang && node.lang.match(/^[^ \t]+(?=[ \t]|$)/)
+ var meta = node.meta
var props = {}
if (lang) {
props.className = ['language-' + lang]
+ if (meta) {
+ props.dataMeta = meta
+ }
}
Or
- return h(node.position, 'pre', [h(node, 'code', props, [u('text', value)])])
+ var hastNode = h(node.position, 'pre', [
+ h(node, 'code', props, [u('text', value)])
+ ])
+
+ if (lang) {
+ hastNode.data = {lang: String(lang)}
+ if (meta) {
+ hastNode.data.meta = meta
+ }
+ }
+
+ return hastNode
Correct?
The question above was if it is possible to directly attach data
to the newly created HAST node with the h()
function that is passed to the handler, e.g. h(node, 'code', props, data, children)
.
@stefanprobst Ah sorry. I just had a déjà vu moment where I remembered I already talked about this recently here: https://github.com/syntax-tree/mdast-util-to-hast/issues/24.
Does that make sense?
It does, yes. Sorry should have checked closed issues myself. Thanks for your help!
@stefanprobst Sure no problem! 👍
Now that https://github.com/remarkjs/remark/pull/345 has landed, add support for
meta
prop tomdast-util-to-hast
as well. Ok to put this ondata-meta
? Also, is the node.lang regex still needed?