Skip to content

Commit 76ea80d

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

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

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

Lines changed: 35 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,38 @@ 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(new 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+
if (newLink.contains("oracle")) then // exclude links containing "oracle"
146+
None
147+
else
148+
// We check if the internal link to the static documentation is valid
149+
val docPath = url.getPath.substring(url.getPath.indexOf("/docs/")).replaceFirst("/docs/", "docs/_docs/").replace(".html", ".md")
150+
println(docPath)
151+
val fileExists = Files.exists(Paths.get(docPath))
152+
if !fileExists then
153+
//Si le fichier n'existe pas, on vérifie si le fichier existe avec l'extension .md
154+
val newDocPath = docPath + ".md"
155+
if !Files.exists(Paths.get(newDocPath)) then
156+
report.warning(s"Link to $newLink will return a 404 not found")
157+
case _ => None
158+
}
159+
case false =>
160+
None
161+
}
162+
})
163+
132164
val bodyTags: mutable.Map[TagKey, List[String]] =
165+
if tags.get(SimpleTagKey("see")).isDefined then
166+
processLink
133167
mutable.Map((tagsWithoutDiagram).toSeq: _*)
134168

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

0 commit comments

Comments
 (0)