Skip to content

Add support for html tags inside documentation written in markdown #10752

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

Merged
merged 1 commit into from
Dec 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions scala3doc-testcases/src/tests/htmlTests.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package tests
package htmlTests


/**<table>
* <tr>
* <th> ASD </th>
* <th> FGH </th>
* <th> JKL </th>
* </tr>
* <tr>
* <td> 123 </td>
* <td> 456 </td>
* <td> 789 </td>
* </tr>
* </table>
* This is <span style="color:red;">red</span> text.
* <b> I'm bold </b>
* <i> And I'm italic </i>
*
* An example documention with markdown formatting
*
*
* <pre><code>
* def someScalaCode(x: String) = println("Hello " + x)
* </code></pre>
*
*/
class HtmlTest
11 changes: 10 additions & 1 deletion scala3doc/src/dotty/dokka/tasty/comments/MarkdownConverter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,17 @@ class MarkdownConverter(val repr: Repr) extends BaseConverter {

case _: mda.SoftLineBreak => emit(dkkd.Br.INSTANCE)

case inline: mda.HtmlInline =>
emit(dkkd.Html(List(dkk.text(inline.getSegments.mkString)).asJava, kt.emptyMap))

case entity: mda.HtmlEntity =>
emit(dkkd.Html(List(dkk.text(entity.getSegments.mkString)).asJava, kt.emptyMap))

case block: mda.HtmlBlock =>
emit(dkkd.Html(List(dkk.text(block.getContentChars.toString)).asJava, kt.emptyMap))

// TODO (https://github.com/lampepfl/scala3doc/issues/205): for now just silent the warnigs
case _:mda.HtmlInline | _: mda.LinkRef | _: mda.HtmlEntity | _: mda.HtmlBlock | _: com.vladsch.flexmark.ext.emoji.Emoji =>
case _: mda.LinkRef | _: com.vladsch.flexmark.ext.emoji.Emoji =>
emit(dkk.text(MarkdownParser.renderToText(n)))

case _ =>
Expand Down
7 changes: 5 additions & 2 deletions scala3doc/src/dotty/dokka/tasty/comments/MarkdownParser.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dotty.dokka.tasty.comments

import java.util.{ Arrays }
import Regexes._

import com.vladsch.flexmark.util.{ast => mdu}
import com.vladsch.flexmark.formatter.Formatter
Expand Down Expand Up @@ -36,11 +37,13 @@ object MarkdownParser {
"https://github.global.ssl.fastly.net/images/icons/emoji/")
.set(WikiLinkExtension.LINK_ESCAPE_CHARS, "")

val parser = Parser.builder(markdownOptions).build()

val RENDERER = Formatter.builder(markdownOptions).build()

def parseToMarkdown(text: String): mdu.Document =
Parser.builder(markdownOptions)
.build.parse(text).asInstanceOf[mdu.Document]
// We need to remove safe tag markers as they break flexmark parsing
parser.parse(text.replace(safeTagMarker.toString, "")).asInstanceOf[mdu.Document]


def renderToText(node: mdu.Node): String =
Expand Down
3 changes: 2 additions & 1 deletion scala3doc/src/dotty/renderers/ScalaHtmlRenderer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ class ScalaHtmlRenderer(using ctx: DokkaContext) extends HtmlRenderer(ctx) {

override def buildContentNode(f: FlowContent, node: ContentNode, pageContext: ContentPage, sourceSetRestriciton: JSet[DisplaySourceSet]) = {
node match {
case n: HtmlContentNode => withHtml(f, raw(n.body).toString)
case n: HtmlContentNode =>
withHtml(f, raw(n.body).toString)
case n: HierarchyGraphContentNode => buildDiagram(f, n.diagram, pageContext)
case n: DocumentableList =>
val ss = if sourceSetRestriciton == null then Set.empty.asJava else sourceSetRestriciton
Expand Down