Skip to content
Open
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
39 changes: 29 additions & 10 deletions scaladoc/src/dotty/tools/scaladoc/tasty/comments/Cleaner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,44 @@ object Cleaner {
private def cleanHtml(text: String): String = {
val result = StringBuilder()
var index = 0
var insideCode = false
var insideInlineCode = false
var insideFencedCode = false
var insideLink = false
while (index < text.length) {
if (insideCode) {
result.append(text(index))
if (index >= 2 && text(index) == '`' && text(index - 1) == '`' && text(index - 2) == '`') {
insideCode = false
if (insideInlineCode) {
if (text(index) == '`') {
insideInlineCode = false
index += 1
result.append("`")
} else {
result.append(text(index))
index += 1
}
} else if (insideFencedCode) {
if (index <= text.length - 3 && text(index) == '`' && text(index + 1) == '`' && text(index + 2) == '`') {
insideFencedCode = false
index += 3
result.append("```")
} else {
result.append(text(index))
index += 1
}
index += 1
} else if (insideLink) {
result.append(text(index))
if (index >= 1 && text(index) == ']' && text(index - 1) == ']') {
insideLink = false
}
index += 1
} else if (index <= text.length - 3 && text(index) == '`' && text(index + 1) == '`' && text(index + 2) == '`') {
result.append("```")
insideCode = true
index += 3
} else if (index <= text.length - 1 && text(index) == '`') {
result.append("`")
index += 1
if (index <= text.length - 2 && text(index) == '`' && text(index + 1) == '`') {
result.append("``")
index += 2
insideFencedCode = true
} else {
insideInlineCode = true
}
} else if (index <= text.length - 2 && text(index) == '[' && text(index + 1) == '[') {
result.append("[[")
insideLink = true
Expand Down
9 changes: 9 additions & 0 deletions scaladoc/test-resources/defines/fences.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package pkg

/**
* These should not be escaped: `x < y`, `a > b`, `i <not-a-tag> j`
*/
class B

/**
* This is OK: `$a` `${b}`
* And so is this:
Expand All @@ -11,6 +16,10 @@ package pkg
* ${d} `hello` $e
* ```
*
* Fenced blocks should be finished the same way they started:
* ```
* this block does not end here: ` so this should not be escaped: i < j
* ```
*/
class C

Expand Down
3 changes: 2 additions & 1 deletion scaladoc/test/dotty/tools/scaladoc/e2e/EndToEndTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ class EndToEndTests:

@Test
def fences(): Unit = run("defines", "fences.scala") { (out, _) =>
ensureContains(out, "pkg", "C.html", "$a", "${b}", "$c", "${d}", "$e")
ensureContains(out, "pkg", "B.html", "x &lt; y", "a &gt; b", "i &lt;not-a-tag&gt; j")
ensureContains(out, "pkg", "C.html", "$a", "${b}", "$c", "${d} `hello` $e", "i &lt; j")
}

@Test
Expand Down
Loading