question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

mdast-util-find-and-replace with HTML inside the Markdown

See original GitHub issue

Parsing MDX containing HTML

Hi,

Not sure this is a bug or a feature;

I’m trying to parse MDX which can contain some HTML. At the end i’m looking to use mdast-util-find-and-replace to replace some text terms in the markdown.

While trying to parse some markdown with mdast-util-from-markdown, i find a difference when the markdown starts with HTML tags

When parsing Some <b>HTML</b>content the tree looks correct and the <b> is a mdxJsxTextElement. mdast-util-find-and-replace can then operate on the text element.

Capture d’écran 2021-02-03 à 11 34 26

When parsing <p>Some HTML content</p>, i only get a single html element containing the <p> tags, and in that case mdast-util-find-and-replace is unable to replace the text

Capture d’écran 2021-02-03 à 11 34 53

There is a full repro with failing test case here for illustration : https://codesandbox.io/s/addglossary-in-mdx-zxyzw?file=/src/index.js

Expected behavior

Even starting with html tags, the MDX should be fully parsed

Actual behavior

The markdown is parsed as a single html node

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:18 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
revolunetcommented, Feb 3, 2021

Thanks for the explanation, it helps.

Our current use case is adding “tooltip” capabilities to various content, based on a glossary, so we just need to “wrap” these texts with some specific container. This will be used client-side to trigger the pretty tooltip.

1reaction
wooormcommented, Feb 3, 2021

HTML is a black box to markdown, with MDX, less so. But I thought I’d split the adding JSX vs removing HTML parts, it’s a bit confusing but at this level I thought it wise to split it!

Sounds interesting, what you’re doing with MDX at such a low level btw!

Read more comments on GitHub >

github_iconTop Results From Across the Web

mdast - Topics - unified
utility to get the plain text content of an mdast node ... mdast utility to parse markdown ... plugin that turns markdown into...
Read more >
Mdast-util - npm.io
mdast utility to get the plain text content of a node. unistmdastmdast-utilutilutilitymarkdownnodestringserialize. 3.1.0 • Published 2 years ago ...
Read more >
mdast - Npms.io
mdast utility to get the plain text content of a node. local_offerunist, mdast, mdast-util, util, utility, markdown, node, string, serialize.
Read more >
How to get only text values from a markdown string in Javascript
One way to do it is by parsing the HTML string with DOMParser API to turn your string into a Document object and...
Read more >
Transforming Markdown with Remark & Rehype - ryanfiller.com
html file. To paraphrase heavily from the mdsvex docs, the source file is first parsed into a Markdown AST (MDAST), where remark plugins...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found