Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does not gracefully handle nested links #157

Open
inf3rnus opened this issue Dec 16, 2021 · 1 comment
Open

Does not gracefully handle nested links #157

inf3rnus opened this issue Dec 16, 2021 · 1 comment

Comments

@inf3rnus
Copy link

Grab the .md from here: https://raw.githubusercontent.com/daohu527/awesome-self-driving-car/master/README.md

The component will throw an error to console: Warning: validateDOMNesting(...): <a> cannot appear as a descendant of <a>.

Not sure what's required to fix it, but you have the info :)

image

image

@inf3rnus
Copy link
Author

inf3rnus commented Dec 16, 2021

Note, if anyone else gets this issue, it can be fixed externally via:

const hasNestedAnchors = (element, depth = 0) => {
  const { tagName, children } = element;

  if (children === undefined || (tagName !== "a" && depth === 0)) {
    return false;
  }

  if (depth > 0 && tagName === "a") {
    return true;
  } else {
    for (const child of children) {
      if (hasNestedAnchors(child, depth + 1)) {
        return true;
      }
    }
    return false;
  }
};

and then pass that into MarkdownPreview like so:

<MarkdownPreview allowElement={element => !hasNestedAnchors(element)} />

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant