Skip to content
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# intellij-markdown Changelog

## [Unreleased]
- Generate HTML with `code-line line-added`/`line-deleted` classes for added/deleted lines in `diff` / `patch` blocks
- Support tables inside list items
- Android compatibility issues

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,18 +197,37 @@ internal class CodeFenceGeneratingProvider : GeneratingProvider {
}

var lastChildWasContent = false
var isDiff = false

val attributes = ArrayList<String>()
for (child in childrenToConsider) {
if (state == 1 && child.type in listOf(MarkdownTokenTypes.CODE_FENCE_CONTENT,
MarkdownTokenTypes.EOL)) {
visitor.consumeHtml(HtmlGenerator.trimIndents(HtmlGenerator.leafText(text, child, false), indentBefore))
val content = HtmlGenerator.trimIndents(HtmlGenerator.leafText(text, child, false), indentBefore)
if (isDiff && child.type == MarkdownTokenTypes.CODE_FENCE_CONTENT) {
val lineClass = when {
content.startsWith("+") -> "line-added"
Comment thread
donnerpeter marked this conversation as resolved.
content.startsWith("-") -> "line-deleted"
Comment thread
donnerpeter marked this conversation as resolved.
else -> null
}
if (lineClass != null) {
visitor.consumeTagOpen(child, "span", "class=\"code-line $lineClass\"")
visitor.consumeHtml(content)
visitor.consumeTagClose("span")
} else {
visitor.consumeHtml(content)
}
} else {
visitor.consumeHtml(content)
}
lastChildWasContent = child.type == MarkdownTokenTypes.CODE_FENCE_CONTENT
}
if (state == 0 && child.type == MarkdownTokenTypes.FENCE_LANG) {
attributes.add("class=\"language-${
HtmlGenerator.leafText(text, child).toString().trim().split(' ')[0]
}\"")
val info = HtmlGenerator.leafText(text, child).toString().trim()
val whitespaceIndex = info.indexOfFirst { it.isWhitespace() }
val language = if (whitespaceIndex == -1) info else info.substring(0, whitespaceIndex)
isDiff = language == "diff" || language == "patch"
attributes.add("class=\"language-$language\"")
}
if (state == 0 && child.type == MarkdownTokenTypes.EOL) {
visitor.consumeTagOpen(node, "code", *attributes.toTypedArray())
Expand Down Expand Up @@ -323,4 +342,3 @@ open class ImageGeneratingProvider(linkMap: LinkMap, baseURI: URI?) : LinkGenera
val REGEX = Regex("[^a-zA-Z0-9 ]")
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ class HtmlGeneratorCommonTest : HtmlGeneratorTestBase() {
defaultTest()
}

@Test
fun testDiffCodeFences() {
defaultTest()
}

@Test
fun testEscaping() {
defaultTest()
Expand Down
32 changes: 32 additions & 0 deletions src/fileBasedTest/resources/data/html/diffCodeFences.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
```diff
+added line 1
+added line 2
+added line 3
```

```diff metadata
+tab-separated info string
```

```diff
-removed line 1
-removed line 2
-removed line 3
```

```diff
context line
+added line 1
+added line 2
-removed line 1
-removed line 2
context line
```

```diff
+ <tag>&
-
+
plain line

```
44 changes: 44 additions & 0 deletions src/fileBasedTest/resources/data/html/diffCodeFences.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<body>
<pre><code class="language-diff">
<span class="code-line line-added">+added line 1</span>

<span class="code-line line-added">+added line 2</span>

<span class="code-line line-added">+added line 3</span>

</code></pre>
<pre><code class="language-diff">
<span class="code-line line-added">+tab-separated info string</span>

</code></pre>
<pre><code class="language-diff">
<span class="code-line line-deleted">-removed line 1</span>

<span class="code-line line-deleted">-removed line 2</span>

<span class="code-line line-deleted">-removed line 3</span>

</code></pre>
<pre><code class="language-diff">
context line
<span class="code-line line-added">+added line 1</span>

<span class="code-line line-added">+added line 2</span>

<span class="code-line line-deleted">-removed line 1</span>

<span class="code-line line-deleted">-removed line 2</span>

context line
</code></pre>
<pre><code class="language-diff">
<span class="code-line line-added">+ &lt;tag&gt;&amp;</span>

<span class="code-line line-deleted">-</span>

<span class="code-line line-added">+</span>

plain line

</code></pre>
</body>
Loading