diff --git a/modules/ublog/src/main/Env.scala b/modules/ublog/src/main/Env.scala index cb39ae71f2..e38cf300c9 100644 --- a/modules/ublog/src/main/Env.scala +++ b/modules/ublog/src/main/Env.scala @@ -22,6 +22,8 @@ final class Env( ec: scala.concurrent.ExecutionContext ) { + import net.{ assetBaseUrl, baseUrl } + private val colls = new UblogColls(db(CollName("ublog_blog")), db(CollName("ublog_post"))) val topic = wire[UblogTopicApi] diff --git a/modules/ublog/src/main/UblogMarkup.scala b/modules/ublog/src/main/UblogMarkup.scala index 52a338426c..5b3bd77b79 100644 --- a/modules/ublog/src/main/UblogMarkup.scala +++ b/modules/ublog/src/main/UblogMarkup.scala @@ -1,10 +1,12 @@ package lila.ublog import scala.concurrent.duration._ + import lila.common.Chronometer +import lila.common.config import lila.common.config.NetConfig -final class UblogMarkup(net: NetConfig) { +final class UblogMarkup(baseUrl: config.BaseUrl, assetBaseUrl: config.AssetBaseUrl) { private val renderer = new lila.common.Markdown( @@ -21,11 +23,11 @@ final class UblogMarkup(net: NetConfig) { type Html = String def apply(post: UblogPost): String = - cache.get(post.markdown, str => postProcess(renderer(s"ublog:${post.id}")(preProcess(str)))) + cache.get(post.markdown, str => postProcess(renderer(s"ublog:${post.id}")(preProcess(str)).pp)) private def preProcess = replaceGameGifs.apply _ - private def postProcess(html: String) = imageParagraph(html) + private def postProcess(html: String) = unescapeUnderscoreInLinks(imageParagraph(html)) private val cache = lila.memo.CacheApi.scaffeineNoScheduler .maximumSize(2048) @@ -33,11 +35,23 @@ final class UblogMarkup(net: NetConfig) { // replace game GIFs URLs with actual game URLs that can be embedded private object replaceGameGifs { - val regex = (net.assetBaseUrl + """/game/export/gif(/white|/black|)/(\w{8})\.gif""").r - def apply(markdown: Text) = regex.replaceAllIn(markdown, net.baseUrl.value + "/$2$1") + private val regex = (assetBaseUrl.value + """/game/export/gif(/white|/black|)/(\w{8})\.gif""").r + def apply(markdown: Text) = regex.replaceAllIn(markdown, baseUrl.value + "/$2$1") } // put images into a container for styling private def imageParagraph(markup: Html) = markup.replace("""

([^<]+)""".r + private def unescape(txt: String) = txt.replace("""\\_""", "_") + def apply(markup: Html) = contentRegex.replaceAllIn( + hrefRegex.replaceAllIn(markup, m => s"""href="${unescape(m group 1)}""""), + m => s""">${unescape(m group 1)}""" + ) + } } diff --git a/modules/ublog/src/test/UblogMarkupTest.scala b/modules/ublog/src/test/UblogMarkupTest.scala new file mode 100644 index 0000000000..4eab66964b --- /dev/null +++ b/modules/ublog/src/test/UblogMarkupTest.scala @@ -0,0 +1,33 @@ +package lila.ublog + +import org.specs2.execute.Result +import org.specs2.mutable.Specification +import scalatags.Text.all._ + +import lila.common.config + +class UblogMarkupTest extends Specification { + + val m = new UblogMarkup(config.BaseUrl("https://lichess.org"), config.AssetBaseUrl("https://lichess1.org")) + + "backslashUnderscore" should { + "fix href" in { + m.unescapeUnderscoreInLinks( + """

text

""" + ) must_== + """

text

""" + } + "fix link text" in { + m.unescapeUnderscoreInLinks( + """

https://youtu.be/di6W\_i3NiJA

""" + ) must_== + """

https://youtu.be/di6W_i3NiJA

""" + } + "fix href and link text" in { + m.unescapeUnderscoreInLinks( + """

https://youtu.be/di6W\_i3NiJA

""" + ) must_== + """

https://youtu.be/di6W_i3NiJA

""" + } + } +}