Skip to content

Commit 34d403b

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 34d403b

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

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

Lines changed: 23 additions & 2 deletions
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
@@ -135,6 +138,24 @@ object Preparser {
135138
def allTags(key: SimpleTagKey): List[String] =
136139
(bodyTags remove key).getOrElse(Nil).reverse
137140

141+
def processLink(key: SimpleTagKey): SimpleTagKey =
142+
if (!summon[DocContext].args.noLinkWarnings) then
143+
bodyTags(key).foreach(link => {
144+
val newLink: String = link.replaceAll("\\[\\[|\\]\\]", "")
145+
val isValid = Try(URL(newLink)).isSuccess
146+
if isValid then
147+
val url = new URL(newLink)
148+
val path = url.getPath().replaceFirst("/docs/", "docs/_docs/").replace(".html", ".md")
149+
// We check if the internal link to the static documentation is valid
150+
val fileExists = Files.exists(Paths.get(path))
151+
if !fileExists then
152+
report.warning(s"Link to $url will return a 404 not found")
153+
else
154+
report.warning(s"Link $link is not a valid URL")
155+
}
156+
)
157+
key
158+
138159
def allSymsOneTag(key: TagKey, filterEmpty: Boolean = true): SortedMap[String, String] = {
139160
val keys: Seq[SymbolTagKey] =
140161
bodyTags.keys.toSeq flatMap {
@@ -157,7 +178,7 @@ object Preparser {
157178
val cmt = PreparsedComment(
158179
body = docBody.toString,
159180
authors = allTags(SimpleTagKey("author")),
160-
see = allTags(SimpleTagKey("see")),
181+
see = allTags(processLink(SimpleTagKey("see"))),
161182
result = allTags(SimpleTagKey("return")),
162183
throws = allSymsOneTag(SimpleTagKey("throws")),
163184
valueParams = allSymsOneTag(SimpleTagKey("param")),

0 commit comments

Comments
 (0)