diff options
Diffstat (limited to 'core/src/main')
-rw-r--r-- | core/src/main/kotlin/Formats/MarkdownFormatService.kt | 27 | ||||
-rw-r--r-- | core/src/main/kotlin/Kotlin/ContentBuilder.kt | 5 | ||||
-rw-r--r-- | core/src/main/kotlin/Markdown/MarkdownProcessor.kt | 2 |
3 files changed, 24 insertions, 10 deletions
diff --git a/core/src/main/kotlin/Formats/MarkdownFormatService.kt b/core/src/main/kotlin/Formats/MarkdownFormatService.kt index b9c9c04f..f7c17401 100644 --- a/core/src/main/kotlin/Formats/MarkdownFormatService.kt +++ b/core/src/main/kotlin/Formats/MarkdownFormatService.kt @@ -10,6 +10,13 @@ enum class ListKind { Unordered } +private class ListState(val kind: ListKind, var size: Int = 1) { + fun getTagAndIncrement() = when (kind) { + ListKind.Ordered -> "${size++}. " + else -> "* " + } +} + private val TWO_LINE_BREAKS = System.lineSeparator() + System.lineSeparator() open class MarkdownOutputBuilder(to: StringBuilder, @@ -20,7 +27,7 @@ open class MarkdownOutputBuilder(to: StringBuilder, impliedPlatforms: List<String>) : StructuredOutputBuilder(to, location, locationService, languageService, extension, impliedPlatforms) { - private val listKindStack = Stack<ListKind>() + private val listStack = ArrayDeque<ListState>() protected var inTableCell = false protected var inCodeBlock = false private var lastTableCellStart = -1 @@ -34,7 +41,7 @@ open class MarkdownOutputBuilder(to: StringBuilder, } private fun ensureNewline() { - if (inTableCell && listKindStack.isEmpty()) { + if (inTableCell && listStack.isEmpty()) { if (to.length != lastTableCellStart && !to.endsWith("<br>")) { to.append("<br>") } @@ -101,22 +108,22 @@ open class MarkdownOutputBuilder(to: StringBuilder, } override fun appendUnorderedList(body: () -> Unit) { - listKindStack.push(ListKind.Unordered) + listStack.push(ListState(ListKind.Unordered)) body() - listKindStack.pop() + listStack.pop() ensureNewline() } override fun appendOrderedList(body: () -> Unit) { - listKindStack.push(ListKind.Ordered) + listStack.push(ListState(ListKind.Ordered)) body() - listKindStack.pop() + listStack.pop() ensureNewline() } override fun appendListItem(body: () -> Unit) { ensureNewline() - to.append(if (listKindStack.peek() == ListKind.Unordered) "* " else "1. ") + to.append(listStack.peek()?.getTagAndIncrement()) body() ensureNewline() } @@ -151,8 +158,10 @@ open class MarkdownOutputBuilder(to: StringBuilder, if (inTableCell) { ensureNewline() body() - } - else { + } else if (listStack.isNotEmpty()) { + body() + ensureNewline() + } else { ensureParagraph() body() ensureParagraph() diff --git a/core/src/main/kotlin/Kotlin/ContentBuilder.kt b/core/src/main/kotlin/Kotlin/ContentBuilder.kt index c124821e..a244a48e 100644 --- a/core/src/main/kotlin/Kotlin/ContentBuilder.kt +++ b/core/src/main/kotlin/Kotlin/ContentBuilder.kt @@ -96,7 +96,9 @@ fun buildContentTo(tree: MarkdownNode, target: ContentBlock, linkResolver: (Stri } } MarkdownTokenTypes.EOL -> { - if (keepEol(nodeStack.peek()) && node.parent?.children?.last() != node) { + if ((keepEol(nodeStack.peek()) && node.parent?.children?.last() != node) || + // Keep extra blank lines when processing lists (affects Markdown formatting) + (processingList(nodeStack.peek()) && node.previous?.type == MarkdownTokenTypes.EOL)) { parent.append(ContentText(node.text)) } } @@ -156,6 +158,7 @@ fun buildContentTo(tree: MarkdownNode, target: ContentBlock, linkResolver: (Stri private fun MarkdownNode.getLabelText() = children.filter { it.type == MarkdownTokenTypes.TEXT || it.type == MarkdownTokenTypes.EMPH }.joinToString("") { it.text } private fun keepEol(node: ContentNode) = node is ContentParagraph || node is ContentSection || node is ContentBlockCode +private fun processingList(node: ContentNode) = node is ContentOrderedList || node is ContentUnorderedList fun buildInlineContentTo(tree: MarkdownNode, target: ContentBlock, linkResolver: (String) -> ContentBlock) { val inlineContent = tree.children.singleOrNull { it.type == MarkdownElementTypes.PARAGRAPH }?.children ?: listOf(tree) diff --git a/core/src/main/kotlin/Markdown/MarkdownProcessor.kt b/core/src/main/kotlin/Markdown/MarkdownProcessor.kt index d1d40dd4..2c8f7a73 100644 --- a/core/src/main/kotlin/Markdown/MarkdownProcessor.kt +++ b/core/src/main/kotlin/Markdown/MarkdownProcessor.kt @@ -14,6 +14,8 @@ class MarkdownNode(val node: ASTNode, val parent: MarkdownNode?, val markdown: S val text: String get() = node.getTextInNode(markdown).toString() fun child(type: IElementType): MarkdownNode? = children.firstOrNull { it.type == type } + val previous get() = parent?.children?.getOrNull(parent.children.indexOf(this) - 1) + override fun toString(): String = StringBuilder().apply { presentTo(this) }.toString() } |