Skip to content

Commit b69594f

Browse files
committed
Verify links in docs
- Add a check of valid url and internal link - Add condition flag for the warning
1 parent d4e80c4 commit b69594f

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

scaladoc/src/dotty/tools/scaladoc/tasty/comments/Preparser.scala

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@ package tasty.comments
44
import scala.collection.mutable
55
import scala.collection.immutable.SortedMap
66
import scala.util.matching.Regex
7+
import java.net.URL
8+
import java.nio.file.{Paths, Files}
9+
import scala.util.Try
710

811
object Preparser {
912
import Regexes._
1013

1114
/** Parses a raw comment string into a `Comment` object. */
1215
def preparse(
1316
comment: List[String],
14-
): PreparsedComment = {
17+
)(using DocContext): PreparsedComment = {
1518

1619
/** Parses a comment (in the form of a list of lines) to a `Comment`
1720
* instance, recursively on lines. To do so, it splits the whole comment
@@ -129,7 +132,30 @@ object Preparser {
129132
val stripTags = List(inheritDiagramTag, contentDiagramTag, SimpleTagKey("template"), SimpleTagKey("documentable"))
130133
val tagsWithoutDiagram = tags.filterNot(pair => stripTags.contains(pair._1))
131134

135+
def processLink: Unit =
136+
if (!summon[DocContext].args.noLinkWarnings) then tags.get(SimpleTagKey("see")).get.foreach(link => {
137+
val newLink: String = link.replaceAll("\\[\\[|\\]\\]", "")
138+
val isValid = Try(URL(newLink)).isSuccess
139+
isValid match {
140+
case true =>
141+
val url = new URL(newLink)
142+
url match {
143+
// We check if it's an internal link
144+
case s if s.getPath.contains("/docs/") =>
145+
// We check if the internal link to the static documentation is valid
146+
val fileExists = Files.exists(Paths.get(url.getPath().replaceFirst("/docs/", "docs/_docs/").replace(".html", ".md")))
147+
if !fileExists then
148+
report.warning(s"Link to $url will return a 404 not found")
149+
case _ => None
150+
}
151+
case false =>
152+
report.warning(s"Link to $newLink is not valid")
153+
}
154+
})
155+
132156
val bodyTags: mutable.Map[TagKey, List[String]] =
157+
if tags.get(SimpleTagKey("see")).isDefined then
158+
processLink
133159
mutable.Map((tagsWithoutDiagram).toSeq: _*)
134160

135161
def allTags(key: SimpleTagKey): List[String] =

0 commit comments

Comments
 (0)