-
| Hi there, I have a directive called  
 import type { ContainerDirective } from 'mdast-util-directive'
import { toHast } from 'mdast-util-to-hast'
function processCard(node: ContainerDirective) {
  node.data = node.data ?? {}
  node.data.hName = 'section'
  node.data.hProperties = {
    className: 'sc-card'
  }
  node.data.hChildren = [
    {
      type: 'element',
      tagName: 'div',
      properties: {
        className: node.name + '-content'
      },
      children: node.children.map(toHast)
    }
  ]
}
export default processCardAnd having the above markdown yields the following (I have removed the  {
  "type": "containerDirective",
  "name": "card",
  "attributes": {},
  "children": [
    {
      "type": "heading",
      "depth": 1,
      "children": [
        {
          "type": "text",
          "value": "This is common markdown"
        }
      ]
    },
    {
      "type": "image",
      "url": "https://it3-content.usbank.com/300x200/000000/fff",
      "title": null
    },
    {
      "type": "containerDirective",
      "name": "notice",
      "attributes": {
        "note": ""
      },
      "children": [
        {
          "type": "paragraph",
          "children": [
            {
              "type": "text",
              "value": "This is a note notice"
            }
          ]
        }
      ],
      "data": {
        "hName": "section",
        "hProperties": {
          "className": "sc-notice note"
        },
        "hChildren": [
          {
            "type": "element",
            "tagName": "div",
            "properties": {
              "className": "notice-title"
            },
            "children": [
              {
                "type": "element",
                "tagName": "svg",
                "properties": {
                  "className": "notice-icon",
                  "variant": "note"
                },
                "children": []
              },
              {
                "type": "element",
                "tagName": "p",
                "properties": {
                  "className": "notice-icon"
                },
                "children": [
                  {
                    "type": "text",
                    "value": "note"
                  }
                ]
              }
            ]
          },
          {
            "type": "element",
            "tagName": "div",
            "properties": {
              "className": "notice-content"
            },
            "children": [
              {
                "type": "element",
                "tagName": "p",
                "properties": {},
                "children": [
                  {
                    "type": "text",
                    "value": "This is a note notice"
                  }
                ]
              }
            ]
          }
        ]
      }
    }
  ],
  "data": {
    "hName": "section",
    "hProperties": {
      "className": "sc-card"
    },
    "hChildren": [
      {
        "type": "element",
        "tagName": "div",
        "properties": {
          "className": "card-content"
        },
        "children": [
          {
            "type": "element",
            "tagName": "h1",
            "properties": {
              "id": "this-is-common-markdown"
            },
            "children": [
              {
                "type": "text",
                "value": "This is common markdown"
              }
            ]
          },
          {
            "type": "element",
            "tagName": "div",
            "properties": {},
            "children": []
          },
          {
            "type": "element",
            "tagName": "div",
            "properties": {},
            "children": [
              {
                "type": "element",
                "tagName": "p",
                "properties": {},
                "children": [
                  {
                    "type": "text",
                    "value": "This is a note notice"
                  }
                ]
              }
            ]
          }
        ]
      }
    ]
  }
}I would expect the same HTML to be created for the  As usual here is a runnable example: https://codesandbox.io/s/remark-directive-forked-zmwdn8 | 
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 9 replies
-
| Hey @reypm! 👋 
 | 
Beta Was this translation helpful? Give feedback.
reversedoesn’t change to a depth-first tree-traversel, so it doesn’t do what you want.Your problem is that you transform the tree.
While transforming the tree, by setting
hChildren, you are saying: ignore everything, use myhChildreninstead.Hopefully, you don’t care about the
div, and can apply the class to the section instead.Then it just works:
node.data.hProperties = { - className: ['sc-card'] + className: ['sc-card', node.name + '-content'] } - node.data.hChildren = [ - { - type: 'element', - tagName: 'div', - properties: { - className: node.name + '-content' - }, - children: node.children.map(toHast) - } - ]