Skip to content

MDX loader: linkify should process the md AST instead of the md string #9048

Closed

Description

@slorber

Have you read the Contributing Guidelines on issues?

Motivation

Our current MDX loader code looks like this:

export default function markdownLoader(
  this: LoaderContext<DocsMarkdownOption>,
  source: string,
): void {
  const fileString = source;
  const callback = this.async();
  const options = this.getOptions();
  return callback(null, linkify(fileString, this.resourcePath, options));
}

The linkify process that turns [link](./xyz.md) to [link](/baseUrl/xyz-pathname) is based on string manipulations and regexes.

As a result, linkify has many edge case bugs related to the unability to properly resolve markdown relative file path links such as [](./relative-path.md)

We should instead do this with a Remark plugin after MDX has parsed the string.

See also:

Self-service

  • I'd be willing to do some initial work on this proposal myself.

Activity

  1. added
    proposal
    status: needs triage
    and removed
    status: needs triage
    on Jun 8, 2023
  2. added
    domain: markdown
    on Jun 8, 2023
  3. Josh-Cena commented on Jun 8, 2023

    @Josh-Cena
    Collaborator

    See previous attempt: #6261

  4. modified the milestones: 4.0, 3.x on Jan 9, 2024
  5. slorber commented on Jan 9, 2024

    @slorber
    CollaboratorAuthor

    Another case it would fix is a bug when using relative md links on markdown images:

    [text](./installation.mdx)
    
    [![alt](/img/slash-introducing.svg)](./installation.mdx)

    Only the first link resolves.

    But reported here: #9720 (comment), that should also be solved by this proposed solution.

  6. slorber commented on Feb 22, 2024

    @slorber
    CollaboratorAuthor

    Another case reported where markdown links resolution fails due to usage of inline code blocks with triple backticks (TIL this exists and is supported by MDX 😅). #9876

    ```something```
    
    [test](test.md)
  7. slorber commented on Apr 25, 2024

    @slorber
    CollaboratorAuthor

    Another use-case reported is when the link is multi-line:

    <!-- This works -->
    A [link to another page](other-page.md)
    
    <!-- This doesn't work -->
    A [link to
    another page](other-page.md)

    #9636
    #10067

  8. slorber commented on Apr 25, 2024

    @slorber
    CollaboratorAuthor

    Another use-case reported is when the link contains certain combinations of symbols:

    [a](./migration/migrate-6.md#hooks) ([b](./migration/migrate-6.md#hooks))
    
    [`Type1`](some_doc.md#type1)\<[`Type2`](some_doc.md#type2)\>

    #9518
    #9553
    #10072

  9. pellyadolfo commented on May 16, 2024

    @pellyadolfo
  10. slorber commented on May 17, 2024

    @slorber
    Author
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    domain: markdownproposal

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions