remark-gfm icon indicating copy to clipboard operation
remark-gfm copied to clipboard

position info missing when use plain `[url]` syntax

Open JounQin opened this issue 3 months ago • 10 comments

Initial checklist

Affected package

[email protected]

Steps to reproduce

https://github.com/Project-HAMi/HAMi/pull/1271

[https://github.com/Project-HAMi/HAMi/pull/1271]

Actual behavior

The first link and text both have correct position info, while the second link and texts all have no position info unexpectedly.

{
  "type": "root",
  "children": [
    {
      "type": "paragraph",
      "children": [
        {
          "type": "link",
          "title": null,
          "url": "https://github.com/Project-HAMi/HAMi/pull/1271",
          "children": [
            {
              "type": "text",
              "value": "https://github.com/Project-HAMi/HAMi/pull/1271",
              "position": {
                "start": {
                  "line": 1,
                  "column": 1,
                  "offset": 0
                },
                "end": {
                  "line": 1,
                  "column": 47,
                  "offset": 46
                }
              }
            }
          ],
          "position": {
            "start": {
              "line": 1,
              "column": 1,
              "offset": 0
            },
            "end": {
              "line": 1,
              "column": 47,
              "offset": 46
            }
          }
        }
      ],
      "position": {
        "start": {
          "line": 1,
          "column": 1,
          "offset": 0
        },
        "end": {
          "line": 1,
          "column": 47,
          "offset": 46
        }
      }
    },
    {
      "type": "paragraph",
      "children": [
        {
          "type": "text",
          "value": "["
        },
        {
          "type": "link",
          "title": null,
          "url": "https://github.com/Project-HAMi/HAMi/pull/1271",
          "children": [
            {
              "type": "text",
              "value": "https://github.com/Project-HAMi/HAMi/pull/1271"
            }
          ]
        },
        {
          "type": "text",
          "value": "]"
        }
      ],
      "position": {
        "start": {
          "line": 3,
          "column": 1,
          "offset": 48
        },
        "end": {
          "line": 3,
          "column": 49,
          "offset": 96
        }
      }
    }
  ],
  "position": {
    "start": {
      "line": 1,
      "column": 1,
      "offset": 0
    },
    "end": {
      "line": 3,
      "column": 49,
      "offset": 96
    }
  }
}

Expected behavior

position should always be there

Runtime

[email protected]

Package manager

[email protected]

Operating system

macOS Sequoia 15.6.1

Build and bundle tools

No response

JounQin avatar Sep 09 '25 09:09 JounQin

May I ask which package is actually doing the parsing? Too many dependencies... Not sure where to fix.

Is it micromark-extension-gfm-autolink-literal?

fisker avatar Nov 26 '25 14:11 fisker

https://github.com/syntax-tree/mdast-util-gfm-autolink-literal/blob/ba83f42e2d6e7bafbb213b2bf94ec533891a6d82/lib/index.js#L130.

It’s not really fixable, because it by definition works on the AST instead of on the markdown input.

See how GH renders the code by OP:

https://github.com/Project-HAMi/HAMi/pull/1271

[https://github.com/Project-HAMi/HAMi/pull/1271]


References: https://github.com/micromark/micromark-extension-gfm-autolink-literal/tree/main?tab=readme-ov-file#bugs, https://github.com/github/cmark-gfm/issues/278

wooorm avatar Nov 26 '25 17:11 wooorm

It's blocking Prettier, what should we do?

Simply join them as a Node?

fisker avatar Nov 26 '25 17:11 fisker

I am not entirely sure what your use case is; what joining means? what alternatives you have?

Given the GH bug, I’d think the ideal output would be [<https://github.com/Project-HAMi/HAMi/pull/1271>] as that renders correctly on GH as: [https://github.com/Project-HAMi/HAMi/pull/1271]. Generally I think prettier should never output raw URLs but always wrap them in <>, that’s what we do

wooorm avatar Nov 26 '25 17:11 wooorm

what joining means?

Children in link don't have position information, I mean I should join the children as a normal text.

what alternatives you have?

I don't. Since Prettier requires position.

fisker avatar Nov 26 '25 18:11 fisker

It’s not really fixable, because it by definition works on the AST instead of on the markdown input.

I'm not quite sure to understand, this issue is about parsing with position info missing, not about printing? position SHOULD always be there on Node. The linked references bugs are not about position, are they?

JounQin avatar Nov 28 '25 05:11 JounQin

No, position should not always be there: when the tree is changed in a way that new nodes were not present in the original document, position should not be there. The original source text could have &period; and such in there, changing positional info.

Prettier is a formatter, that’s why I suggest formatting in a particular way.

The referenced issues in cmark-gfm are to show that I think how GH works is not good. Because GH works like that, on the AST, these projects work on the AST too

wooorm avatar Nov 28 '25 16:11 wooorm

No, position should not always be there: when the tree is changed in a way that new nodes were not present in the original document, position should not be there.

Hmm... But the content is parsed as text[]) + link nodes same as the source text.

My original issue is not specific for prettier, I found this issue because eslint-mdx is also using position info on Node which is assumed to always be there on parsing phase.

JounQin avatar Nov 28 '25 16:11 JounQin

Should we reconsider allowing disable "autolink" then? https://github.com/remarkjs/remark-gfm/issues/16#issuecomment-899046315

fisker avatar Nov 28 '25 17:11 fisker

I'm trying to make an own mdast-util-gfm by removing gfmAutolinkLiteralFromMarkdown and gfmAutolinkLiteralToMarkdown, somehow

>cat test.md
_http://www.example.com:80/_a*b_

>yarn debug test.md
test.md{
  type: 'paragraph',
  children: [ { type: 'emphasis', children: [], position: [Object] } ],
  position: {
    start: { line: 1, column: 1, offset: 0 },
    end: { line: 1, column: 33, offset: 32 }
  }
}
__

Missing children in emphasis

https://github.com/prettier/prettier/pull/18365

fisker avatar Nov 28 '25 17:11 fisker