Skip to content

Commit

Permalink
Merge pull request Automattic#447 from wtfparadox/fix/timeskip-in-url
Browse files Browse the repository at this point in the history
Do not replace timeskip when it is in a URL
  • Loading branch information
geekygecko committed Oct 21, 2022
2 parents 3791a48 + faea472 commit a99ca65
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,10 @@ class ShowNotesHelperTest {
actual = ShowNotesHelper.convertTimesToLinks("<li><a href=\"https://overcast.fm/+BtuyYAAIQ/16:45\">Confirmation of John's prediction about face swipe timing</a></li>")
expected = "<li><a href=\"https://overcast.fm/+BtuyYAAIQ/16:45\">Confirmation of John's prediction about face swipe timing</a></li>"
assertEquals(actual, expected)

// Do not replace timeskip when it is in an URL - https://github.com/Automattic/pocket-casts-android/issues/145
actual = ShowNotesHelper.convertTimesToLinks("<li><a href=\"https://www.theverge.com/2021/12/21/22848957/lg-dualup-32-inch-4k-ultra-fine-monitors-announced-specs\">LG’s new 16:18 monitor looks like a multitasking powerhouse</a></li>")
expected = "<li><a href=\"https://www.theverge.com/2021/12/21/22848957/lg-dualup-32-inch-4k-ultra-fine-monitors-announced-specs\">LG’s new 16:18 monitor looks like a multitasking powerhouse</a></li>"
assertEquals(actual, expected)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@ package au.com.shiftyjelly.pocketcasts.views.helper
object ShowNotesHelper {

fun convertTimesToLinks(html: String): String {
return html.replace("(\\A|\\s|>|[^a-zsA-Z_0-9/])(\\d{0,2}:?\\d{1,2}:\\d{2})(<|\\s|\\z|[^a-zsA-Z_0-9\"])".toRegex(), "$1<a href=\"http:https://localhost/#playerJumpTo=$2\">$2</a>$3")
return html.replace("(\\A|\\s|>|[^a-zsA-Z_0-9/])(\\d{0,2}:?\\d{1,2}:\\d{2})(<|\\s|\\z|[^a-zsA-Z_0-9\"])".toRegex()) { match ->
return@replace if (matchIsInUrl(match, html)) {
match.value
} else {
val (prefix, timeskip, suffix) = match.destructured
"$prefix<a href=\"http:https://localhost/#playerJumpTo=$timeskip\">$timeskip</a>$suffix"
}
}
}

// searches for the first closing tag that comes after a potential timeskip and compares it to url closing tag (</a>)
private fun matchIsInUrl(match: MatchResult, html: String): Boolean {
val closingTagRegex = "</\\w+>".toRegex()
return closingTagRegex.find(html.substring(match.range.first))?.value == "</a>"
}
}

0 comments on commit a99ca65

Please sign in to comment.