Skip to content

Commit

Permalink
fix : URI_DETECTOR regex
Browse files Browse the repository at this point in the history
  • Loading branch information
David committed Oct 18, 2023
1 parent 56e4d0c commit 2629949
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package com.github.zeldigas.text2confl.convert.confluence

import com.github.zeldigas.text2confl.convert.PageHeader
import io.github.oshai.kotlinlogging.KotlinLogging
import java.net.URL
import java.nio.file.InvalidPathException
import java.nio.file.Path
import java.util.regex.Matcher
import java.util.regex.Pattern
import kotlin.io.path.relativeTo

interface ReferenceProvider {
Expand Down Expand Up @@ -41,16 +44,23 @@ class ReferenceProviderImpl(private val basePath: Path, documents: Map<Path, Pag
ReferenceProvider {

companion object {
private val URI_DETECTOR = "^(https?|ftp)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]".toRegex()
private const val URI_DETECTOR = "^(https?|ftp)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]"
private const val MAILTO_DETECTOR = "mailto:"
private val logger = KotlinLogging.logger {}
}
fun isValid(url: String): Boolean {
val pattern = Pattern.compile(URI_DETECTOR, Pattern.CASE_INSENSITIVE);
val matcher = pattern.matcher(url.trim());
return matcher.matches();
}

private val normalizedDocs =
documents.map { (path, header) -> path.relativeTo(basePath).normalize() to header }.toMap()

override fun resolveReference(source: Path, refTo: String): Reference? {

if (URI_DETECTOR.matches(refTo)) return null
if (refTo.startsWith(MAILTO_DETECTOR)) return null
if (isValid(refTo)) return null
if (refTo.startsWith("#")) return Anchor(refTo.substring(1))

val parts = refTo.split("#", limit = 2)
Expand All @@ -62,7 +72,7 @@ class ReferenceProviderImpl(private val basePath: Path, documents: Map<Path, Pag
val targetPath = source.resolveSibling(ref).relativeTo(basePath).normalize()
val document = normalizedDocs[targetPath]?.title ?: return null
return Xref(document, anchor)
} catch (ex: InvalidPathException){
} catch (ex: InvalidPathException) {
logger.error { "Failed to resolve : $refTo from $source" }
throw ex
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.github.zeldigas.text2confl.convert.confluence
import assertk.assertThat
import assertk.assertions.isEqualTo
import assertk.assertions.isNotNull
import assertk.assertions.isNull
import com.github.zeldigas.text2confl.convert.PageHeader
import org.junit.jupiter.api.Test
import org.junit.jupiter.params.ParameterizedTest
Expand Down Expand Up @@ -68,4 +69,26 @@ internal class ReferenceProviderImplTest {

assertThat(result).isNotNull().isEqualTo(Xref("Sub Title One", "test"))
}


@Test
internal fun `Http resolution`() {
val result = providerImpl.resolveReference(Path("docs/one.md"), "http://github.com")

assertThat(result).isNull()
}

@Test
internal fun `Https resolution`() {
val result = providerImpl.resolveReference(Path("docs/one.md"), "https://github.com")

assertThat(result).isNull()
}

@Test
internal fun `Mailto resolution`() {
val result = providerImpl.resolveReference(Path("docs/one.md"), "mailto:john.doe@github.com")

assertThat(result).isNull()
}
}

0 comments on commit 2629949

Please sign in to comment.