syntax-tree / mdast-util-directive

mdast extension to parse and serialize generic directives (`:cite[smith04]`)
https://unifiedjs.com
MIT License
14 stars 6 forks source link

Directives should Store Class Names as an Array #1

Closed benrbray closed 3 years ago

benrbray commented 3 years ago

Initial checklist

Subject

Markdown:

:::foo[bar]{#baz, .one, .two, .three, key=val}
lorem ipsum dolor sit amet
:::

Mdast:

{
  "type": "root",
  "children": [
    {
      "type": "containerDirective",
      "name": "foo",
      "attributes": {
        "id": "baz,",
        "class": "one, two, three,",
        "key": "val"
      },
      "children": [
        {
          "type": "paragraph",
          "data": {
            "directiveLabel": true
          },
          "children": [
            {
              "type": "text",
              "value": "bar"
            }
          ]
        },
        {
          "type": "paragraph",
          "children": [
            {
              "type": "text",
              "value": "lorem ipsum dolor sit amet"
            }
          ]
        }
      ]
    }
  ]
}

Problem

Currently directive class names like {.one, .two, .three} are returned as a string:

"attributes": {
    "class": "one, two, three,"
},

This is a bit weird, since the class name have been transformed, just not into a useful format. There's also a pesky trailing comma to remember when parsing the string manually. I would expect any of the following instead:

I also noticed that the id field keeps the trailing comma if something appears after it, which is a bug.

If this is something you're willing to change, I would be willing to work on the PR. Thanks!

Solution

Proposed changes:

Alternatives

see above

wooorm commented 3 years ago

Why are you adding commas in there, they aren’t supposed to be there: the stuff between curly braces are HTML attributes, with the addition that shortcuts for ID and class exist: https://github.com/micromark/micromark-extension-directive#syntax

wooorm commented 3 years ago

I find no reference of commas, and do find many references w/o them, in the proposal: https://talk.commonmark.org/t/generic-directives-plugins-syntax/444

benrbray commented 3 years ago

Sorry, you're absolutely right about the commas, I was confused. The suggestion about returning the list of class names as an array still makes sense, though, I think. It would be a breaking change, so maybe it's not worth it when a .split(" ") will do.

wooorm commented 3 years ago

The downside of that is that I think it would be confusing to explain:

There are also a couple of other things that could be “smarter”, such as “boolean” attributes (download), so without a value, or style as an object. However, I think that would make it all a bit hard to explain. I’d say sticking as close to HTML is the most reasonable solution

wooorm commented 3 years ago

I don’t think it’s a wise idea to treat class differently, as stated in my previous comment, so I’ll close this.