Skip to content

Commit 70cf4e0

Browse files
committed
Add invalid link check to markdown checker
1 parent af6daf5 commit 70cf4e0

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

src/scripts/markdownChecker.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,19 @@ const BROKEN_LINK_REGEX = new RegExp(
2323
"\\[[^\\]]+\\]\\([^\\)\\s]+\\s[^\\)]+\\)",
2424
"g"
2525
)
26+
// This RegEx checks for invalid links in markdown content.
27+
// The criteria for invalid links are:
28+
// 1. Exclude images: The link shouldn't be preceded by an exclamation mark
29+
// 2. Exclude internal links: The URL part of the link shouldn't start with a forward slash
30+
// 3. Exclude fragment identifiers: The URL part of the link shouldn't start with a hash
31+
// 4. Exclude typical external links: The URL part of the link shouldn't start with http or https
32+
// 5. Exclude email links: The URL part of the link shouldn't start with mailto:
33+
// 6. Exclude PDF links: The URL part of the link shouldn't end with .pdf
34+
// 7. Exclude links wrapped in angled brackets: The URL part of the link shouldn't start with a <
35+
const INVALID_LINK_REGEX = new RegExp(
36+
"(?<!\\!)\\[[^\\]]+\\]\\((?!<|/|#|http|mailto:)[^\\)]*(?<!\\.pdf)\\)",
37+
"g"
38+
)
2639
const INCORRECT_PATH_IN_TRANSLATED_MARKDOWN = new RegExp(
2740
"image: ../../(assets/|../assets/)",
2841
"g"
@@ -204,6 +217,14 @@ function processMarkdown(path: string) {
204217
// if (!BROKEN_LINK_REGEX.global) break
205218
}
206219

220+
let invalidLinkMatch: RegExpExecArray | null
221+
222+
// Check for invalid links
223+
while ((invalidLinkMatch = INVALID_LINK_REGEX.exec(markdownFile))) {
224+
const lineNumber = getLineNumber(markdownFile, invalidLinkMatch.index)
225+
console.warn(`Invalid link found: ${path}:${lineNumber}`)
226+
}
227+
207228
let incorrectImagePathMatch: RegExpExecArray | null
208229

209230
// Todo: refactor to simply check if the image exists relative to the path

0 commit comments

Comments
 (0)