Skip to content

remark-mdx: empty line before closing tag issue #767

@atanasster

Description

@atanasster

Subject of the issue

The issue causes parsing failure in a compiler plugin that parses the child.value strings for post processing.

Reproduce

The following:

<button>
  <div>hello world</div>
  //<- there is one empty space here at the start of the line
</button>

will parse as

  "children": [
    {
      "type": "jsx",
      "value": "<button>\n  <div>hello world</div>\n\n</button>\n\n",
...
    }
  ],

However, removing the one space on the empty line will parse as 3 separate elements:

<button>
  <div>hello world</div>
//<- there is no empty space here
</button>
 "children": [
    {
      "type": "jsx",
      "value": "<button>\n  <div>hello world</div>",
    },
    {
      "type": "text",
      "value": "\n"
    },
    {
      "type": "jsx",
      "value": "</button>",
    }
  ],

Potential solution

The issue seems to be here:

if (sequence[1].test(line)) {

Since an empty string will always pass the test, the closing tag is missed if there is an empty line before it.

Any reasons not to change the rule to

   ...
      if (line.length && sequence[1].test(line)) {
        index = next
        break
      }
...

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions