syntax-tree / mdast-util-to-hast

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

Support info string in code blocks #25

Closed stefanprobst closed 5 years ago

stefanprobst commented 5 years ago

Now that https://github.com/remarkjs/remark/pull/345 has landed, add support for meta prop to mdast-util-to-hast as well. Ok to put this on data-meta? Also, is the node.lang regex still needed?

wooorm commented 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!

stefanprobst commented 5 years ago

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?

wooorm commented 5 years ago

@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!

stefanprobst commented 5 years ago

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()?

wooorm commented 5 years ago

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? 🤔

stefanprobst commented 5 years ago

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).

wooorm commented 5 years ago

@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?

stefanprobst commented 5 years ago

It does, yes. Sorry should have checked closed issues myself. Thanks for your help!

wooorm commented 5 years ago

@stefanprobst Sure no problem! 👍