-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New rules for checking links are valid
- Loading branch information
Showing
16 changed files
with
181 additions
and
24 deletions.
There are no files selected for viewing
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
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
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
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
44 changes: 44 additions & 0 deletions
44
markdown/src/main/kotlin/com/appmattus/markdown/rules/MissingLinkSchemeRule.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,44 @@ | ||
package com.appmattus.markdown.rules | ||
|
||
import com.appmattus.markdown.dsl.RuleSetup | ||
import com.appmattus.markdown.errors.ErrorReporter | ||
import com.appmattus.markdown.processing.MarkdownDocument | ||
import com.appmattus.markdown.rules.extentions.referenceUrl | ||
import com.vladsch.flexmark.ast.LinkRef | ||
import com.vladsch.flexmark.ast.Reference | ||
import java.net.URI | ||
|
||
/** | ||
* # Missing link scheme | ||
* | ||
* This rule is triggered when a link is missing the scheme: | ||
* | ||
* [missing scheme link](www.example.com) | ||
* | ||
* To fix the violation, provide a scheme for the link: | ||
* | ||
* [a valid link](https://example.com/) | ||
*/ | ||
class MissingLinkSchemeRule( | ||
override val config: RuleSetup.Builder.() -> Unit = {} | ||
) : Rule() { | ||
|
||
override fun visitDocument(document: MarkdownDocument, errorReporter: ErrorReporter) { | ||
document.allLinks.forEach { link -> | ||
|
||
when (link) { | ||
is LinkRef -> link.referenceUrl() | ||
is Reference -> null | ||
else -> link.url | ||
}?.let { url -> | ||
val uri = URI(url.toString()) | ||
|
||
if (uri.scheme.isNullOrBlank() && uri.path.startsWith("www.")) { | ||
val description = "Link is missing http/https scheme, for example 'https://${link.chars}'" | ||
|
||
errorReporter.reportError(url.startOffset, url.endOffset, description) | ||
} | ||
} | ||
} | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
markdown/src/main/kotlin/com/appmattus/markdown/rules/ValidRelativeLinksRule.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,47 @@ | ||
package com.appmattus.markdown.rules | ||
|
||
import com.appmattus.markdown.dsl.RuleSetup | ||
import com.appmattus.markdown.errors.ErrorReporter | ||
import com.appmattus.markdown.processing.MarkdownDocument | ||
import com.appmattus.markdown.rules.extentions.referenceUrl | ||
import com.vladsch.flexmark.ast.LinkRef | ||
import com.vladsch.flexmark.ast.Reference | ||
import java.io.File | ||
import java.net.URI | ||
|
||
/** | ||
* # Relative links exist | ||
* | ||
* This rule is triggered when a relative link cannot be resolved: | ||
* | ||
* [a relative link](this-file-does-not-exist.md) | ||
* | ||
* To fix the violation, ensure the linked file exists. | ||
*/ | ||
class ValidRelativeLinksRule( | ||
override val config: RuleSetup.Builder.() -> Unit = {} | ||
) : Rule() { | ||
|
||
override fun visitDocument(document: MarkdownDocument, errorReporter: ErrorReporter) { | ||
val parentDir = document.file.parent | ||
document.allLinks.forEach { link -> | ||
when (link) { | ||
is LinkRef -> link.referenceUrl() | ||
is Reference -> null | ||
else -> link.url | ||
}?.let { url -> | ||
val uri = URI(url.toString()) | ||
|
||
if (uri.isRelative && uri.path.isNotEmpty() && !File(parentDir, uri.path).exists()) { | ||
val expected = File(document.file.parent, uri.path).normalize().path | ||
val description = | ||
"Relative link does not exist, '${link.chars}', expected at '$expected'" | ||
errorReporter.reportError(url.startOffset, url.endOffset, description) | ||
} | ||
} | ||
} | ||
} | ||
|
||
private val URI.isRelative | ||
get() = !isAbsolute && !path.startsWith("www.") && !path.startsWith("/") | ||
} |
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
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
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
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
12 changes: 12 additions & 0 deletions
12
markdown/src/test/kotlin/com/appmattus/markdown/rules/MissingLinkSchemeRuleTest.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,12 @@ | ||
package com.appmattus.markdown.rules | ||
|
||
import org.spekframework.spek2.Spek | ||
import org.spekframework.spek2.style.gherkin.Feature | ||
|
||
object MissingLinkSchemeRuleTest : Spek({ | ||
Feature("MissingLinkSchemeRule") { | ||
FileRuleScenario(listOf("relative-links.md")) { MissingLinkSchemeRule() } | ||
|
||
FileRuleScenario { MissingLinkSchemeRule() } | ||
} | ||
}) |
12 changes: 12 additions & 0 deletions
12
markdown/src/test/kotlin/com/appmattus/markdown/rules/ValidRelativeLinksRuleTest.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,12 @@ | ||
package com.appmattus.markdown.rules | ||
|
||
import org.spekframework.spek2.Spek | ||
import org.spekframework.spek2.style.gherkin.Feature | ||
|
||
object ValidRelativeLinksRuleTest : Spek({ | ||
Feature("ValidRelativeLinksRule") { | ||
FileRuleScenario(listOf("relative-links.md")) { ValidRelativeLinksRule() } | ||
|
||
FileRuleScenario { ValidRelativeLinksRule() } | ||
} | ||
}) |
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
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
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
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,13 @@ | ||
# Relative links | ||
|
||
[This exists](relative-links.md) | ||
|
||
[This also exists](./relative-links.md) | ||
|
||
[This does not exist](./nothing-to-see-here.md) {ValidRelativeLinksRule} | ||
|
||
[This is absolute](/so-should-not-trigger.md) | ||
|
||
[This is a web url](www.example.com) {MissingLinkSchemeRule} | ||
|
||
[This is a web url](https://www.example.com) |