stefanprobst / rehype-extract-toc

MIT License
18 stars 0 forks source link

Doesn't work with variables (mdx-bundler) #5

Open CanRau opened 2 years ago

CanRau commented 2 years ago

I'm using mdx-bundler#frontmatter-and-const and define my h1 as

# {frontmatter.title}

which in the ast looks like

{
  "type": "element",
  "tagName": "h1",
  "properties": {},
  "children": [
    {
      "type": "mdxTextExpression",
      "value": "frontmatter.title",
      "position": {
        "start": {
          "line": 56,
          "column": 3,
          "offset": 2509
        },
        "end": {
          "line": 56,
          "column": 22,
          "offset": 2528
        }
      },
      "data": {
        "estree": {
          "type": "Program",
          "start": 2510,
          "end": 2527,
          "body": [
            {
              "type": "ExpressionStatement",
              "expression": {
                "type": "MemberExpression",
                "start": 2510,
                "end": 2527,
                "object": {
                  "type": "Identifier",
                  "start": 2510,
                  "end": 2521,
                  "name": "frontmatter",
                  "loc": {
                    "start": {
                      "line": 56,
                      "column": 3
                    },
                    "end": {
                      "line": 56,
                      "column": 14
                    }
                  },
                  "range": [
                    2510,
                    2521
                  ]
                },
                "property": {
                  "type": "Identifier",
                  "start": 2522,
                  "end": 2527,
                  "name": "title",
                  "loc": {
                    "start": {
                      "line": 56,
                      "column": 15
                    },
                    "end": {
                      "line": 56,
                      "column": 20
                    }
                  },
                  "range": [
                    2522,
                    2527
                  ]
                },
                "computed": false,
                "optional": false,
                "loc": {
                  "start": {
                    "line": 56,
                    "column": 3
                  },
                  "end": {
                    "line": 56,
                    "column": 20
                  }
                },
                "range": [
                  2510,
                  2527
                ]
              },
              "start": 2510,
              "end": 2527,
              "loc": {
                "start": {
                  "line": 56,
                  "column": 3
                },
                "end": {
                  "line": 56,
                  "column": 20
                }
              },
              "range": [
                2510,
                2527
              ]
            }
          ],
          "sourceType": "module",
          "comments": [],
          "loc": {
            "start": {
              "line": 56,
              "column": 3
            },
            "end": {
              "line": 56,
              "column": 20
            }
          },
          "range": [
            2510,
            2527
          ]
        }
      }
    }
  ],
  "position": {
    "start": {
      "line": 56,
      "column": 1,
      "offset": 2507
    },
    "end": {
      "line": 56,
      "column": 22,
      "offset": 2528
    }
  }
}

which is not compatible with toString(node) (I guess), but changing value: toString(node) to value: node doesn't seem to be compatible with valueToEstree

Any idea how it could be made compatible? If you have the time and interest of course 😇

stefanprobst commented 2 years ago

hmm, i think we would need a way to evaluate the estree at compile time. or create the tree ourselves instead of using valueToEstree (which sounds more doable).