generated from JetBrains/intellij-platform-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(markdown): add MarkdownPsiCapture for URL extraction #59
Add MarkdownPsiCapture class to extract URLs from markdown text using AST traversal.
- Loading branch information
Showing
3 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
languages/shire-markdown/src/main/kotlin/com/phodal/shirelang/markdown/MarkdownPsiCapture.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() } | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
languages/shire-markdown/src/main/resources/com.phodal.shirelang.markdown.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
40 changes: 40 additions & 0 deletions
40
...es/shire-markdown/src/test/kotlin/com/phodal/shirelang/markdown/MarkdownPsiCaptureTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | ||
} | ||
} |