-
-
Notifications
You must be signed in to change notification settings - Fork 69
feat: treat markdown content in overview text better #1193
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rozPierog
wants to merge
3
commits into
damontecres:main
Choose a base branch
from
rozPierog:leon/respect-html-in-overview
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
52 changes: 52 additions & 0 deletions
52
app/src/main/java/com/github/damontecres/wholphin/util/TextUtils.kt
This file contains hidden or 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,52 @@ | ||
| package com.github.damontecres.wholphin.util | ||
|
|
||
| private val MARKDOWN_CHARS = charArrayOf( | ||
| '#', '*', '_', '~', '`', '[', '!', '>', '<', '-', '+', '.', '(', ')', | ||
| '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' | ||
| ) | ||
|
|
||
| /** | ||
| * Strips common Markdown and HTML tags from a string for plain-text display. | ||
| */ | ||
| fun String.stripMarkdown(): String { | ||
| // Early return if no markdown-like characters are present and no trimming is needed. | ||
| if (this.none { it in MARKDOWN_CHARS } && this.trim() == this) return this | ||
|
|
||
| return this | ||
| // Code blocks (``` ... ```) - Remove first to avoid matching contents | ||
| .replace(Regex("(?s)```.*?```"), "") | ||
| // Horizontal rules (--- or ***) - MUST be before bold/italic | ||
| .replace(Regex("(?m)^([-*_]){3,}\\s*$"), "") | ||
| // Headers (# Heading) | ||
| .replace(Regex("(?m)^#{1,6}\\s+"), "") | ||
| // Bold + italic (***text*** or ___text___) | ||
| .replace(Regex("\\*{3}(.+?)\\*{3}"), "$1") | ||
| .replace(Regex("_{3}(.+?)_{3}"), "$1") | ||
| // Bold (**text** or __text__) | ||
| .replace(Regex("\\*{2}(.+?)\\*{2}"), "$1") | ||
| .replace(Regex("_{2}(.+?)_{2}"), "$1") | ||
| // Italic (*text* or _text_) | ||
| .replace(Regex("\\*(.+?)\\*"), "$1") | ||
| .replace(Regex("_(.+?)_"), "$1") | ||
| // Strikethrough (~~text~~) | ||
| .replace(Regex("~~(.+?)~~"), "$1") | ||
| // Inline code (`code`) | ||
| .replace(Regex("`(.+?)`"), "$1") | ||
| // Images () | ||
| .replace(Regex("!\\[.*?]\\(.*?\\)"), "") | ||
| // Links ([text](url)) → keep text | ||
| .replace(Regex("\\[(.+?)]\\(.*?\\)"), "$1") | ||
| // Blockquotes (> text) | ||
| .replace(Regex("(?m)^>\\s+"), "") | ||
| // Unordered lists (- item, * item, + item) | ||
| .replace(Regex("(?m)^[\\-*+]\\s+"), "") | ||
| // Ordered lists (1. item) | ||
| .replace(Regex("(?m)^\\d+\\.\\s+"), "") | ||
| // HTML tags | ||
| .replace(Regex("<[^>]+>"), "") | ||
| // Clean up extra blank lines (more than 2) | ||
| .replace(Regex("(\\r?\\n){3,}"), "\n\n") | ||
| // Collapse multiple spaces to a single space | ||
| .replace(Regex(" {2,}"), " ") | ||
| .trim() | ||
| } |
163 changes: 163 additions & 0 deletions
163
app/src/test/java/com/github/damontecres/wholphin/util/TextUtilsKtTest.kt
This file contains hidden or 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,163 @@ | ||
| package com.github.damontecres.wholphin.util | ||
|
|
||
| import junit.framework.TestCase.assertEquals | ||
| import junit.framework.TestCase.assertFalse | ||
| import junit.framework.TestCase.assertSame | ||
| import junit.framework.TestCase.assertTrue | ||
| import org.junit.Test | ||
|
|
||
| class TextUtilsKtTest { | ||
| @Test | ||
| fun `Empty string input`() { | ||
| assertEquals("", "".stripMarkdown()) | ||
| } | ||
|
|
||
| @Test | ||
| fun `String without markdown characters`() { | ||
| val plain = "Hello, world! This is plain text." | ||
| val result = plain.stripMarkdown() | ||
| assertSame(plain, result) // fast path must return the exact same reference | ||
| } | ||
|
|
||
| @Test | ||
| fun `Headers level 1 to 6 removal`() { | ||
| assertEquals("Heading", "# Heading".stripMarkdown()) | ||
| assertEquals("Heading", "## Heading".stripMarkdown()) | ||
| assertEquals("Heading", "### Heading".stripMarkdown()) | ||
| assertEquals("Heading", "#### Heading".stripMarkdown()) | ||
| assertEquals("Heading", "##### Heading".stripMarkdown()) | ||
| assertEquals("Heading", "###### Heading".stripMarkdown()) | ||
| } | ||
|
|
||
| @Test | ||
| fun `Triple emphasis Bold and Italic stripping`() { | ||
| assertEquals("text", "***text***".stripMarkdown()) | ||
| assertEquals("text", "___text___".stripMarkdown()) | ||
| } | ||
|
|
||
| @Test | ||
| fun `Double emphasis Bold stripping`() { | ||
| assertEquals("text", "**text**".stripMarkdown()) | ||
| assertEquals("text", "__text__".stripMarkdown()) | ||
| } | ||
|
|
||
| @Test | ||
| fun `Single emphasis Italic stripping`() { | ||
| assertEquals("text", "*text*".stripMarkdown()) | ||
| assertEquals("text", "_text_".stripMarkdown()) | ||
| } | ||
|
|
||
| @Test | ||
| fun `Strikethrough stripping`() { | ||
| assertEquals("text", "~~text~~".stripMarkdown()) | ||
| } | ||
|
|
||
| @Test | ||
| fun `Inline code stripping`() { | ||
| assertEquals("code", "`code`".stripMarkdown()) | ||
| assertEquals("Use code here", "Use `code` here".stripMarkdown()) | ||
| } | ||
|
|
||
| @Test | ||
| fun `Multi line code block removal`() { | ||
| val input = | ||
| """ | ||
| Before | ||
| ``` | ||
| fun foo() = 42 | ||
| ``` | ||
| After | ||
| """.trimIndent() | ||
| val result = input.stripMarkdown() | ||
| assertFalse(result.contains("fun foo")) | ||
| assertTrue(result.contains("Before")) | ||
| assertTrue(result.contains("After")) | ||
| } | ||
|
|
||
| @Test | ||
| fun `Markdown link conversion`() { | ||
| assertEquals("Click here", "[Click here](https://example.com)".stripMarkdown()) | ||
| assertFalse("[text](https://example.com)".stripMarkdown().contains("https")) | ||
| } | ||
|
|
||
| @Test | ||
| fun `Blockquote prefix removal`() { | ||
| assertEquals("A quote", "> A quote".stripMarkdown()) | ||
| assertEquals("Line one\nLine two", "> Line one\n> Line two".stripMarkdown()) | ||
| } | ||
|
|
||
| @Test | ||
| fun `Unordered list marker removal`() { | ||
| assertEquals("Item", "- Item".stripMarkdown()) | ||
| assertEquals("Item", "* Item".stripMarkdown()) | ||
| assertEquals("Item", "+ Item".stripMarkdown()) | ||
| } | ||
|
|
||
| @Test | ||
| fun `Ordered list marker removal`() { | ||
| assertEquals("First", "1. First".stripMarkdown()) | ||
| assertEquals("Second", "2. Second".stripMarkdown()) | ||
| assertEquals("Tenth", "10. Tenth".stripMarkdown()) | ||
| } | ||
|
|
||
| @Test | ||
| fun `Horizontal rule removal`() { | ||
| assertEquals("", "---".stripMarkdown()) | ||
| assertEquals("", "***".stripMarkdown()) | ||
| assertEquals("", "___".stripMarkdown()) | ||
| assertEquals("", "------".stripMarkdown()) | ||
| } | ||
|
|
||
| @Test | ||
| fun `HTML tag stripping`() { | ||
| assertEquals("text", "<b>text</b>".stripMarkdown()) | ||
| assertEquals("Hello world", "Hello <br/> world".stripMarkdown()) | ||
| } | ||
|
|
||
| @Test | ||
| fun `Nested emphasis handling`() { | ||
| // ***text*** should collapse to just "text" | ||
| assertEquals("text", "***text***".stripMarkdown()) | ||
| // **bold *italic*** — inner markers stripped, text preserved | ||
| val result = "**bold *italic***".stripMarkdown() | ||
| assertTrue(result.contains("bold")) | ||
| assertTrue(result.contains("italic")) | ||
| } | ||
|
|
||
| @Test | ||
| fun `Escaped markdown character behavior`() { | ||
| // Current impl does NOT handle escapes — document the actual behaviour | ||
| val result = """\*not italic\*""".stripMarkdown() | ||
| // Backslashes are left intact; no italic stripping occurs on escaped markers | ||
| assertTrue(result.contains("not italic")) | ||
| } | ||
|
|
||
| @Test | ||
| fun `Non matching markdown characters`() { | ||
| // '#' mid-word must not be stripped (regex is anchored to line start) | ||
| assertEquals("colour #FF0000", "colour #FF0000".stripMarkdown()) | ||
| // '>' inside a sentence must not be stripped | ||
| assertEquals("a > b", "a > b".stripMarkdown()) | ||
| } | ||
|
|
||
| @Test | ||
| fun `Incomplete markdown syntax`() { | ||
| // Unmatched markers — no crash, text is preserved as-is | ||
| val result = "**bold without close".stripMarkdown() | ||
| assertTrue(result.contains("bold without close")) | ||
|
|
||
| val result2 = "[link(url)".stripMarkdown() | ||
| assertTrue(result2.isNotEmpty()) | ||
| } | ||
|
|
||
| @Test | ||
| fun `Large text performance`() { | ||
| val chunk = "# Title\n\n**bold** and *italic* with [link](url)\n\n" | ||
| val large = chunk.repeat(10_000) | ||
| val start = System.currentTimeMillis() | ||
| val result = large.stripMarkdown() | ||
| val elapsed = System.currentTimeMillis() - start | ||
| assertTrue(result.isNotEmpty()) | ||
| assertTrue("Took ${elapsed}ms, expected < 2000ms", elapsed < 2000) | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think the overview needs any processing here. I would just leave it as is.
Doing the processing here will run on the main thread. And pre-computing it will a bit more memory overhead which I'm trying to limit.