JetBrains / markdown

Markdown parser written in kotlin
Apache License 2.0
682 stars 75 forks source link

Generated HTML is wrapped in <p> tag #72

Open girishkamat opened 3 years ago

girishkamat commented 3 years ago

The below code generates HTML wrapped in <p> tag by default. Is there a way to exclude the wrapper tag? Its causing styling issues.

  val markDownText = "Some text"
  val flavour = new GFMFlavourDescriptor
  val parsedTree = new MarkdownParser(flavour).buildMarkdownTreeFromString(markDownText)
  val html = new HtmlGenerator(markDownText, parsedTree, flavour, false).generateHtml
ajalt commented 3 years ago

The HTML isn't wrapped in a <p> tag: each paragraph is. That's the correct behavior according to the spec.

I'd suggest the library stay as-is for this case, and instead you could strip the <p> tags yourself or fix the styling problems you're seeing.

valich commented 3 years ago

@girishkamat Well, wrapping into a paragraph or now depends on your input, so it's not certain that there will be a paragraph to strip at all. If you have a specific spec on your input, you might try to strip paragraph manually indeed. You might also try to disable paragraphs rendering at all in html generators, but that will be cumbersome, I believe.

What's your specific use case?

mateuszkwiecinski commented 2 years ago

I will share my use case which led to manually stripping <body> and <p> tags. In short, my use case is inject markdown into existing html. Longer version: I'm working on an Android app where I fetch user comments from external API which are formatted as plain html. I have to substitute parts of the html when user taps on specific areas ( spoilers). (Unfortunately) spoilers are stored as Markdown, so I parse related markdown and I want to inject parsed output into existing html - so as per my understanding I shouldn't need additional <body> or <p> tags as they should be a result of parent's html layout. Sample mentioned in Readme file produces <body><p>Some <em>Markdown</em></p></body> while in my case I just need Some <em>Markdown</em> part. Ideally I wouldn't mix formats, but that's outside my control, and it is highly unlikely it will be changed on the backend side so... 🤷

Footnote I really appreciate you made this library support kotlin multiplatform, thank you!
develar commented 1 year ago

Same for all usages in the IntelliJ IDEA codebase where old markdown library was used :) It is up to the client to wrap into <body></body> if needed.

develar commented 1 year ago

Solution: use parse(IElementType("ROOT"), markdownText) instead of buildMarkdownTreeFromString(markdownText).