validate-mdx-links 1.0.1 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -15,12 +15,14 @@ export async function validateMdxLinks({ cwd = process.cwd(), files: filesGlob,
15
15
  console.log(`Found ${files.length} markdown files to validate.\n`);
16
16
  }
17
17
  const scanned = await scanURLs();
18
- console.log("\n" +
19
- "Scanned routes from the file system:\n" +
20
- [...scanned.urls.keys(), ...scanned.fallbackUrls.map((x) => x.url)]
21
- .map((x) => `"${x}"`)
22
- .join(", ") +
23
- "\n");
18
+ if (verbose) {
19
+ console.log("\n" +
20
+ "Scanned routes from the file system:\n" +
21
+ [...scanned.urls.keys(), ...scanned.fallbackUrls.map((x) => x.url)]
22
+ .map((x) => `"${x}"`)
23
+ .join(", ") +
24
+ "\n");
25
+ }
24
26
  const validations = await validateFiles(files, { scanned });
25
27
  const withoutFalsePositives = await Promise.all(validations.map(async ({ file, detected }) => {
26
28
  const filteredDetected = [];
@@ -30,10 +32,6 @@ export async function validateMdxLinks({ cwd = process.cwd(), files: filesGlob,
30
32
  // If there is a hash #id, we don't parse the file to find the heading,
31
33
  // just assume it exists and check if the file exists.
32
34
  link = link.split("#")[0] || "";
33
- // relative links inside of JSX lose the .mdx extension
34
- if (!link.endsWith(".mdx")) {
35
- link = `${link}.mdx`;
36
- }
37
35
  {
38
36
  const path = resolve(dirname(file), link);
39
37
  if (await fileExists(path)) {
@@ -41,14 +39,27 @@ export async function validateMdxLinks({ cwd = process.cwd(), files: filesGlob,
41
39
  continue;
42
40
  }
43
41
  }
42
+ // relative links can lose their .mdx extension
43
+ if (!link.endsWith(".mdx")) {
44
+ const dest = resolve(dirname(file), `${link}.mdx`);
45
+ if (await fileExists(dest)) {
46
+ // file exists, the error is a false positive
47
+ continue;
48
+ }
49
+ }
50
+ // relative links inside of JSX lose the .mdx extension
44
51
  // if the link is relative and the file containing the link is `page.mdx`,
45
52
  // we can check if {destination}/page.mdx exists
46
53
  if (basename(file) === "page.mdx") {
47
- const possibleDest = resolve(dirname(file), "..", link, "page.mdx");
48
- if (await fileExists(possibleDest)) {
54
+ const dest = resolve(dirname(file), "..", link, "page.mdx");
55
+ if (await fileExists(dest)) {
49
56
  continue;
50
57
  }
51
58
  }
59
+ // There's another case for a false postive:
60
+ // We could have a relative link `./bar` from (a)/(b)/foo/page.mdx to (c)/bar/page.mdx
61
+ // We should either open an issue in `next-validate-link` or handle it with glob patterns.
62
+ // For now, prefer to use absolute links in cases like this.
52
63
  }
53
64
  filteredDetected.push(error);
54
65
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "validate-mdx-links",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "main": "dist/index.js",
5
5
  "type": "module",
6
6
  "bin": {
package/src/index.ts CHANGED
@@ -38,14 +38,16 @@ export async function validateMdxLinks({
38
38
 
39
39
  const scanned = await scanURLs();
40
40
 
41
- console.log(
42
- "\n" +
43
- "Scanned routes from the file system:\n" +
44
- [...scanned.urls.keys(), ...scanned.fallbackUrls.map((x) => x.url)]
45
- .map((x) => `"${x}"`)
46
- .join(", ") +
47
- "\n"
48
- );
41
+ if (verbose) {
42
+ console.log(
43
+ "\n" +
44
+ "Scanned routes from the file system:\n" +
45
+ [...scanned.urls.keys(), ...scanned.fallbackUrls.map((x) => x.url)]
46
+ .map((x) => `"${x}"`)
47
+ .join(", ") +
48
+ "\n"
49
+ );
50
+ }
49
51
 
50
52
  const validations = await validateFiles(files, { scanned });
51
53