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

HTML parser: content gets placed outside the html tag #320

Closed
akshaykmr080 opened this issue Jun 26, 2019 · 14 comments · Fixed by #563
Closed

HTML parser: content gets placed outside the html tag #320

akshaykmr080 opened this issue Jun 26, 2019 · 14 comments · Fixed by #563
Labels
🌊 blocked/upstream This cannot progress before something external happens first 🐛 type/bug This is a problem

Comments

@akshaykmr080
Copy link

akshaykmr080 commented Jun 26, 2019

The content inside a HTML tag gets placed outside the HTML in the final output

<a href=\"https://en.wikipedia.org/wiki/HTM\">HTML</a>
The above HTML gets rendered as

<a href=\"https://en.wikipedia.org/wiki/HTM\"></a>HTML

MY code -


const allowedMarkdownTypes = [
  'paragraph',
  'text',
  'emphasis',
  'strong',
  'link',
  'blockquote',
  'delete',
  'list',
  'listItem',
  'heading',
  'code',
  'thematicBreak',
  'table',
  'tableHead',
  'tableBody',
  'tableRow',
  'tableCell',
  'html',
]

const markdown = "<a href=\"https://en.wikipedia.org/wiki/HTM\">HTML</a>, and will require the <code>html-parser</code> AST plugin to be loaded, in addition to setting the <code>escapeHtml</code> property to false."


<ReactMarkdown
          source={markdown}
          allowedTypes={allowedMarkdownTypes}
          escapeHtml={false}
          linkTarget='_blank'
/>

image

@dancrevier
Copy link

Any luck on this? I'm hitting the same issue.

@dancrevier
Copy link

With a lot of debugging, I figured out what's going on. If you build the sample, you'll see it's got:
node_modules/unist-visit/node_modules/unist-util-visit-parents and from package-lock.json, you can see it's version 2.0.1.
node_modules/unist-visit-parents and from pacakge-lock.json, you can see this is version 1.1.2.
If you build the demo app, it includes both and the 2.0.1 wins. In my app, for whatever reason, 1.1.2 won. 1.1.2 doesn't support the reverse parameter and so when parseHtml calls visit, it doesn't do it in the reverse order and things get messed up.

I worked around this problem by adding:
"unist-util-visit-parents": "^2.0.0"
to my app's package.json.

I am not enough of an npm expert to know exactly what's going on and what the root cause is. I think react-markdown could work around the problem by adding the same line to the package.json, but there may be a better solution.

@burgessa23
Copy link

I'm seeing the same thing, de-duping to "unist-util-visit-parents": "^2.0.0" doesn't seem to have fixed issues with nested html tags like tables

@praneybehl
Copy link

praneybehl commented Aug 26, 2019

Same Issue, content gets placed outside.
Adding "unist-util-visit-parents": "^2.0.0" to the package.json did fix it for me too.

@ricvillagrana
Copy link

Same issue "unist-util-visit-parents": "^2.0.0" didn't fix the problem for me.

@emptyOne322
Copy link

same issue

@NamespaceValentine
Copy link

Having the same issue, forcing "unist-util-visit-parents": "^2.0.0" did not solve it for me. Neither did forcing "unist-util-visit": "^2.0.0",

@NamespaceValentine
Copy link

For myself, I fixed this issue by using:
const ReactMarkdown = require("react-markdown/with-html");

It seems that the normal flow can no longer parse html effectively, not sure when/how that happened.

@emptyOne322
Copy link

For myself, I fixed this issue by using:
const ReactMarkdown = require("react-markdown/with-html");

It seems that the normal flow can no longer parse html effectively, not sure when/how that happened.

not solving issue for me. i have a ssr and everething is fine on server side. But issue apears in browser. Maybee something wrong with bundling with webpack.

@emptyOne322
Copy link

Same Issue, content gets placed outside.
Adding "unist-util-visit-parents": "^2.0.0" to the package.json did fix it for me too.

Solved issue for me

@h23
Copy link

h23 commented Sep 8, 2020

Same Issue, content gets placed outside.
Adding "unist-util-visit-parents": "^2.0.0" to the package.json did fix it for me too.

Solved issue for me

Solved issue for me too

@ChristianMurphy
Copy link
Member

this appears to be coming from

"mdast-add-list-metadata": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/mdast-add-list-metadata/-/mdast-add-list-metadata-1.0.1.tgz",
"integrity": "sha512-fB/VP4MJ0LaRsog7hGPxgOrSL3gE/2uEdZyDuSEnKCv/8IkYHiDkIQSbChiJoHyxZZXZ9bzckyRk+vNxFzh8rA==",
"requires": {
"unist-util-visit-parents": "1.1.2"
}
},

which comes from

"mdast-add-list-metadata": "1.0.1",

this would need to be updated upstream at

https://gitlab.com/staltz/mdast-add-list-metadata#README

@ChristianMurphy ChristianMurphy added 🌊 blocked/upstream This cannot progress before something external happens first 🐛 type/bug This is a problem labels Oct 8, 2020
@wooorm wooorm changed the title The html content gets placed outside the html tag HTML parser: content gets placed outside the html tag Oct 19, 2020
@wooorm
Copy link
Member

wooorm commented Oct 19, 2020

I believe this is fixed on 5.0.0, with this change. Could someone confirm whether that works? Otherwise it’s just the HTML parser being funky and the plan is for that to be replaced next major.

wooorm added a commit that referenced this issue Apr 12, 2021
* Replace `renderers` w/ `components`
* Replace `allowNode` w/ `allowElement`, which is now given a hast element (as
  the first parameter)
* Replace `allowedTypes` w/ `allowedElements`
* Replace `disallowedTypes` w/ `disallowedElements`
* Change signature of `linkTarget` and `transformLinkUri`, which are now given
  hast children (as the second parameter)
* Change signature of `transformImageUri`, which is now given the `alt` string
  as the second parameter (instead of the fourth)
* Replace `plugins` w/ `remarkPlugins` (backwards compatible change)
* Add `rehypePlugins`
* Change `includeNodeIndex` to `includeElementIndex`: it still sets an `index`,
  but that value now represents the number of preceding elements, it also sets a
  `siblingCount` (instead of `parentChildCount`) with the number of sibling
  elements in the parent
* The `columnAlignment` prop is no longer given to table elements: it’s
  available as `style` on `th` and `td` elements instead
* The `spread` prop is no longer given to list elements: it’s already handled

Remove buggy HTML parsers from core

* If you want HTML, add [`rehype-raw`](https://github.com/rehypejs/rehype-raw)
  to `rehypePlugins` and it’ll work without bugs!
* Remove `allowDangerousHtml` (previously called `escapeHtml`) option: pass
  `rehype-raw` in `rehypePlugins` to allow HTML instead
* Remove `with-html.js`, `plugins/html-parser.js` entries from library
* Remove naïve HTML parser too: either use `rehype-raw` to properly support
  HTML, or don’t allow it at all

Closes GH-549.
Closes GH-563.

The following issues are solved as rehype is now available:

Closes GH-522.
Closes GH-465.
Closes GH-427.
Closes GH-384.
Closes GH-356.

The following issues are solved as a proper HTML parser (`rehype-raw`) is now
available:

Closes GH-562.
Closes GH-460.
Closes GH-454.
Closes GH-452.
Closes GH-433.
Closes GH-386.
Closes GH-385.
Closes GH-345.
Closes GH-320.
Closes GH-302.
Closes GH-267.
Closes GH-259.

The following issues are solved as docs are improved:

Closes GH-251.
@wooorm
Copy link
Member

wooorm commented Apr 12, 2021

This should be solved by landing GH-563 today, which will soon be released in v6.0.0!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🌊 blocked/upstream This cannot progress before something external happens first 🐛 type/bug This is a problem
Development

Successfully merging a pull request may close this issue.

10 participants