Closed kt3k closed 3 years ago
I’m not sure about your argument for readability. How is your example more readable than the following?
const { parse } = require('remark')
parse(markdown).children.forEach(node => {
if (node.type === 'heading') {
// do something for headings
} else if (node.type === 'paragraph') {
// do something for paragraphs
}
})
I have been thinking about using the mdast
module similar to hastscript
, but for markdown. As a wrapper for unist-builder
. Maybe that could expose types as well?
The word readability was a little misleading. Sorry for that. But it is sometimes considered as an antipattern to put string literals directly in source code, especially when the string literal works like a symbol or enum. For example, see this.
So what I do in my source code for now is like the below.
const { parse } = require('remark')
const { HEADING, PARAGRAPH } = require('./my-constant-module')
parse(markdown).children.forEach(node => {
if (node.type === HEADING) {
// do something for headings
} else if (node.type === PARAGRAPH) {
// do something for paragraphs
}
})
If remark provides the type strings officially, I don't need define types by myself and the code would look more legitimate.
I have been thinking about using the mdast module similar to hastscript, but for markdown. As a wrapper for unist-builder. Maybe that could expose types as well?
That would be great!
@kt3k Would you like to work on an mdast
/ mdastscript
package?
@wooorm Yes! I'm glad to contribute what I've suggested.
Alright! I added you to the syntax-tree
org, would you like to start work in a repo called mdastscript
there?
Let me know if I can help, and we can discuss about the name before publishing, potentially switching to mdast
?!
Thanks! I joined @syntax-tree org.
I started working on mdastscript
for now. https://github.com/syntax-tree/mdastscript
Please review it when I finished scaffolding the test settings.
Super! Looks like a good start! Let me know if you get a bit further and if there’s more to review! 👍
I added bundling (microbundle), testing (tape & babel), lint (xo), coverage(nyc & codecov) and ci (travis). I put the type constants in index.js and confirmed they exist in test cases.
I think I've done what I can do for now! 😄
I used microbundle because it seemed easier to configure than other tools, but I found that it only supports node >= 6.x. If that's a problem for mdastscript, then I'll switch bundling tool to another one.
I guessed the description of mdastscript as DSL for creating virtual MDAST trees
, based on hastscript
's description. But I'm not sure if this is correct. Please update it with more correct one.
Thanks for starting the discussion @kt3k ! We're in the process unifying ideas in with discussions unifiedjs/collective#44 If you'd like to continue this thread, or start a new one https://github.com/remarkjs/remark/discussions/categories/ideas will be the home for ideas going forward.
How about exposing node types (strings) as constants of remark module?
It would increase the readability of dependent modules.