Skip to content

Commit

Permalink
[IDEA-285910]links pointing to SETEXT and AXT in markdown file fails …
Browse files Browse the repository at this point in the history
…, after export to html.
  • Loading branch information
XenoAmess committed Dec 29, 2021
1 parent 76fb99f commit a7fff17
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,16 @@ open class CommonMarkFlavourDescriptor(protected val useSafeLinks: Boolean = tru
MarkdownElementTypes.LIST_ITEM to ListItemGeneratingProvider(),

MarkdownTokenTypes.SETEXT_CONTENT to TrimmingInlineHolderProvider(),
MarkdownElementTypes.SETEXT_1 to SimpleTagProvider("h1"),
MarkdownElementTypes.SETEXT_2 to SimpleTagProvider("h2"),
MarkdownElementTypes.SETEXT_1 to SimpleTagWithLinkProvider("h1"),
MarkdownElementTypes.SETEXT_2 to SimpleTagWithLinkProvider("h2"),

MarkdownTokenTypes.ATX_CONTENT to TrimmingInlineHolderProvider(),
MarkdownElementTypes.ATX_1 to SimpleTagProvider("h1"),
MarkdownElementTypes.ATX_2 to SimpleTagProvider("h2"),
MarkdownElementTypes.ATX_3 to SimpleTagProvider("h3"),
MarkdownElementTypes.ATX_4 to SimpleTagProvider("h4"),
MarkdownElementTypes.ATX_5 to SimpleTagProvider("h5"),
MarkdownElementTypes.ATX_6 to SimpleTagProvider("h6"),
MarkdownElementTypes.ATX_1 to SimpleTagWithLinkProvider("h1"),
MarkdownElementTypes.ATX_2 to SimpleTagWithLinkProvider("h2"),
MarkdownElementTypes.ATX_3 to SimpleTagWithLinkProvider("h3"),
MarkdownElementTypes.ATX_4 to SimpleTagWithLinkProvider("h4"),
MarkdownElementTypes.ATX_5 to SimpleTagWithLinkProvider("h5"),
MarkdownElementTypes.ATX_6 to SimpleTagWithLinkProvider("h6"),

MarkdownElementTypes.AUTOLINK to object : GeneratingProvider {
override fun processNode(visitor: HtmlGenerator.HtmlGeneratingVisitor, text: String, node: ASTNode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package org.intellij.markdown.html

import org.intellij.markdown.MarkdownElementTypes
import org.intellij.markdown.MarkdownTokenTypes
import org.intellij.markdown.MarkdownTokenTypes.Companion.ATX_CONTENT
import org.intellij.markdown.MarkdownTokenTypes.Companion.SETEXT_CONTENT
import org.intellij.markdown.ast.*
import org.intellij.markdown.ast.impl.ListCompositeNode
import org.intellij.markdown.ast.impl.ListItemCompositeNode
import org.intellij.markdown.html.entities.EntityConverter
import org.intellij.markdown.lexer.Compat.assert
import org.intellij.markdown.parser.LinkMap
import kotlin.text.Regex

abstract class OpenCloseGeneratingProvider : GeneratingProvider {
abstract fun openTag(visitor: HtmlGenerator.HtmlGeneratingVisitor, text: String, node: ASTNode)
Expand Down Expand Up @@ -52,6 +53,54 @@ open class SimpleTagProvider(val tagName: String) : OpenCloseGeneratingProvider(

}

open class SimpleTagWithLinkProvider(val tagName: String) : OpenCloseGeneratingProvider() {

override fun openTag(visitor: HtmlGenerator.HtmlGeneratingVisitor, text: String, node: ASTNode) {
visitor.consumeTagOpen(
node,
tagName,
getPureIdString(node,text)
)
}

override fun closeTag(visitor: HtmlGenerator.HtmlGeneratingVisitor, text: String, node: ASTNode) {
visitor.consumeTagClose(tagName)
}

private fun getPureIdString(
node: ASTNode,
text: String
) : String {
val stringBuilder = StringBuilder()
getPureIdStringDfs(
node,
text,
stringBuilder
)
val idValue : CharSequence = LinkMap.normalizeDestination(stringBuilder.toString().trim(), true)
return "id = \"$idValue\""
}

private fun getPureIdStringDfs(
node: ASTNode,
text: String,
stringBuilder: StringBuilder
) {
if (node.type in listOf(SETEXT_CONTENT, ATX_CONTENT)) {
stringBuilder.append(text.substring(node.startOffset, node.endOffset))
} else {
for(children in node.children){
getPureIdStringDfs(
children,
text,
stringBuilder
)
}
}
}

}

open class SimpleInlineTagProvider(val tagName: String, val renderFrom: Int = 0, val renderTo: Int = 0)
: InlineHolderGeneratingProvider() {
override fun openTag(visitor: HtmlGenerator.HtmlGeneratingVisitor, text: String, node: ASTNode) {
Expand Down
7 changes: 7 additions & 0 deletions src/fileBasedTest/resources/data/html/headers.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,10 @@ This is *header* lvl2
#### ATX4 *em* ATX4 ####
##### ATX5 *em* ATX5 #####
###### ATX6 *em* ATX6 ######

###### ATX6_0<>&=
###### ATX6_1

[link ATX6_0<>&=](#ATX6_0<>&=)

[link ATX6_1](#ATX6_1)

0 comments on commit a7fff17

Please sign in to comment.