Skip to content

Commit

Permalink
Add --verbose flag
Browse files Browse the repository at this point in the history
It's difficult to debug link hygiene bugs since we don't know what links
are valid. Eventually, we might implement some levenstein distance.
  • Loading branch information
olafurpg committed Sep 9, 2018
1 parent 39b8ace commit 8030e21
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 6 deletions.
2 changes: 1 addition & 1 deletion mdoc-docs/src/main/scala/mdoc/docs/MdocModifier.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class MdocModifier(context: Context) extends StringModifier {
val cleanInput = Input.VirtualFile(code.filename, code.text)
val markdown = Markdown.toMarkdown(cleanInput, markdownSettings, myReporter, context.settings)
val links = DocumentLinks.fromMarkdown(GitHubIdGenerator, RelativePath("readme.md"), cleanInput)
LinkHygiene.lint(List(links), myReporter)
LinkHygiene.lint(List(links), myReporter, verbose = false)
val stdout = fansi.Str(myStdout.toString()).plainText
if (myReporter.hasErrors || myReporter.hasWarnings) {
if (info != "crash") {
Expand Down
2 changes: 1 addition & 1 deletion mdoc/src/main/scala/mdoc/internal/cli/MainOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ final class MainOps(

def lint(): Unit = {
val docs = DocumentLinks.fromGeneratedSite(settings, reporter)
LinkHygiene.lint(docs, reporter)
LinkHygiene.lint(docs, reporter, settings.verbose)
}

def handleMarkdown(file: InputFile): Exit = synchronized {
Expand Down
2 changes: 2 additions & 0 deletions mdoc/src/main/scala/mdoc/internal/cli/Settings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ case class Settings(
)
@ExtraName("test")
check: Boolean = false,
@Description("Include additional diagnostics for debuggin potential problems.")
verbose: Boolean = false,
@Description(
"Classpath to use when compiling Scala code examples. " +
"Defaults to the current thread's classpath."
Expand Down
7 changes: 5 additions & 2 deletions mdoc/src/main/scala/mdoc/internal/markdown/LinkHygiene.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import java.net.URI
import mdoc.Reporter

object LinkHygiene {
def lint(docs: List[DocumentLinks], reporter: Reporter): Unit = {
def lint(docs: List[DocumentLinks], reporter: Reporter, verbose: Boolean): Unit = {
val isValidHeading = docs.iterator.flatMap(_.absoluteDefinitions).toSet
for {
doc <- docs
Expand All @@ -15,11 +15,14 @@ object LinkHygiene {
if !isValidHeading(uri)
} {
val isAbsolutePath = uri.getPath.startsWith("/")
val debug =
if (verbose) s". isValidHeading=$isValidHeading"
else ""
val hint =
if (isAbsolutePath)
s". To fix this problem, either make the link relative or turn it into complete URL such as http://example.com$uri."
else ""
reporter.warning(reference.pos, s"Unknown link '$uri'$hint")
reporter.warning(reference.pos, s"Unknown link '$uri'$hint$debug")
}
}

Expand Down
3 changes: 3 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,9 @@ Common options:
produce a diff against an existing site. Useful for asserting in CI that a
site is up-to-date.
--verbose
Include additional diagnostics for debuggin potential problems.
--classpath String (default: "")
Classpath to use when compiling Scala code examples. Defaults to the current
thread's classpath.
Expand Down
20 changes: 18 additions & 2 deletions tests/unit/src/test/scala/tests/markdown/LinkHygieneSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import mdoc.internal.markdown.LinkHygiene
class LinkHygieneSuite extends FunSuite with DiffAssertions {
private val myOut = new ByteArrayOutputStream()
private val reporter = new ConsoleReporter(new PrintStream(myOut))
def check(name: String, original: String, expected: String): Unit = {
def check(name: String, original: String, expected: String, verbose: Boolean = false): Unit = {
test(name) {
myOut.reset()
reporter.reset()
Expand All @@ -22,7 +22,7 @@ class LinkHygieneSuite extends FunSuite with DiffAssertions {
.default(root)
.copy(reportRelativePaths = true, in = root, out = root)
val links = DocumentLinks.fromGeneratedSite(settings, reporter)
LinkHygiene.lint(links, reporter)
LinkHygiene.lint(links, reporter, verbose)
val obtained = fansi.Str(myOut.toString()).plainText
assertNoDiffOrPrintExpected(obtained, expected)
}
Expand Down Expand Up @@ -113,4 +113,20 @@ class LinkHygieneSuite extends FunSuite with DiffAssertions {
""".stripMargin
)

check(
"verbose",
"""
|/a.md
|# Header 1
|[2](b.md#header)
|/b.md
|# Header 2
""".stripMargin,
"""|warning: a.md:2:1: warning: Unknown link 'b.md#header'. isValidHeading=Set(b.md, b.md#header-2, a.md, a.md#header-1)
|[2](b.md#header)
|^^^^^^^^^^^^^^^^
|""".stripMargin,
verbose = true
)

}

0 comments on commit 8030e21

Please sign in to comment.