Skip to content

Commit

Permalink
feat(markdown): add MarkdownPsiCapture for URL extraction #59
Browse files Browse the repository at this point in the history
Add MarkdownPsiCapture class to extract URLs from markdown text using AST traversal.
  • Loading branch information
phodal committed Aug 24, 2024
1 parent b0a9468 commit ab9739b
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.phodal.shirelang.markdown

import org.intellij.markdown.IElementType
import org.intellij.markdown.ast.ASTNode
import org.intellij.markdown.ast.accept
import org.intellij.markdown.ast.visitors.RecursiveVisitor
import org.intellij.markdown.flavours.gfm.GFMFlavourDescriptor
import org.intellij.markdown.flavours.gfm.GFMTokenTypes
import org.intellij.markdown.parser.MarkdownParser

class MarkdownPsiCapture {
private val embeddedHtmlType = IElementType("ROOT")

/**
* Capture markdown text with ast node Node
*/
fun captureUrl(markdownText: String, type: String): List<String> {
val flavour = GFMFlavourDescriptor()
val parsedTree: ASTNode = MarkdownParser(flavour).parse(embeddedHtmlType, markdownText)

val types: List<String> = when (type) {
/**
* [GFMTokenTypes.GFM_AUTOLINK] , [MarkdownElementTypes.INLINE_LINK]
*/
"link" -> listOf("GFM_AUTOLINK")
else -> listOf()
}

// Traverse the AST to find and process nodes of the specified type
val result = mutableListOf<String>()
parsedTree.accept(object : RecursiveVisitor() {
override fun visitNode(node: ASTNode) {
when {
types.contains(node.type.name) -> {
result.add(markdownText.substring(node.startOffset, node.endOffset))
}

node.type.name.lowercase() == type -> {
result.add(markdownText.substring(node.startOffset, node.endOffset))
}
}

super.visitNode(node)
}
})

return result.map { it.trim() }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<idea-plugin package="com.phodal.shirelang.markdown">
<!--suppress PluginXmlValidity -->
<dependencies>
<plugin id="org.intellij.plugins.markdown"/>
</dependencies>
</idea-plugin>
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import com.phodal.shirelang.markdown.MarkdownPsiCapture
import junit.framework.TestCase.assertEquals
import org.junit.Test

class MarkdownPsiCaptureTest {

@Test
fun should_return_url_when_gfm_auto_link() {
// given
val markdownText = """
normal text link: https://shire.phodal.com
""".trimIndent()
val type = "link"
val markdownPsiCapture = MarkdownPsiCapture()

// when
val result = markdownPsiCapture.captureUrl(markdownText, type)

// then
assertEquals("https://shire.phodal.com", result.first())
}

@Test
fun should_return_url_when_markdown_label_link() {
// given
val markdownText = """
normal text link: [Shire](https://shire.phodal.com)
link 2: https://aise.phodal.com
""".trimIndent()
val type = "link"
val markdownPsiCapture = MarkdownPsiCapture()

// when
val result = markdownPsiCapture.captureUrl(markdownText, type)

// then
assertEquals("https://shire.phodal.com", result.first())
assertEquals("https://aise.phodal.com", result[1])
}
}

0 comments on commit ab9739b

Please sign in to comment.