Skip to content

Commit a793a99

Browse files
committed
Support Nuxt framework preset
1 parent 5f21d7d commit a793a99

File tree

10 files changed

+103
-9
lines changed

10 files changed

+103
-9
lines changed

.changeset/new-olives-dig.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'next-validate-link': minor
3+
---
4+
5+
Support Nuxt framework preset

docs/content/docs/meta.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"pages": ["index", "---Frameworks---", "..."]
3+
}

docs/content/docs/nuxt.mdx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
title: Nuxt
3+
description: Support Nuxt validation
4+
---
5+
6+
## Usage
7+
8+
By default, it assumes you are using Next.js routing.
9+
You can pass a framework preset to customise how it scans your pages.
10+
11+
```ts
12+
import { scanURLs } from 'next-validate-link';
13+
14+
const scanned = await scanURLs({
15+
preset: 'nuxt',
16+
});
17+
// validate with the scanned urls
18+
```

src/presets/astro.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,12 @@ export async function scanURLs(options: ScanOptions = {}): Promise<ScanResult> {
1414
cwd: path.join(cwd, 'src/pages'),
1515
});
1616

17-
return [
18-
...pagesFiles.map((file) => {
19-
const parsed = path.parse(file);
20-
if (parsed.name === 'index') return parsed.dir;
21-
22-
return path.join(parsed.dir, parsed.name);
23-
}),
24-
];
17+
return pagesFiles.map((file) => {
18+
const parsed = path.parse(file);
19+
if (parsed.name === 'index') return parsed.dir;
20+
21+
return path.join(parsed.dir, parsed.name);
22+
});
2523
}
2624

2725
const result: ScanResult = { urls: new Map(), fallbackUrls: [] };

src/presets/nuxt.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { ScanOptions, ScanResult } from '@/scan';
2+
import * as path from 'node:path';
3+
import fg from 'fast-glob';
4+
import { populateToScanResult } from './shared';
5+
import { isDirExists } from '@/utils/fs';
6+
7+
export async function scanURLs(options: ScanOptions = {}): Promise<ScanResult> {
8+
const ext = options.extensions ?? ['vue', 'md', 'mdx'];
9+
const cwd = options.cwd ?? process.cwd();
10+
11+
async function getFiles() {
12+
const suffix = ext.length > 0 ? `.{${ext.join(',')}}` : '';
13+
14+
const pagesFiles = await fg(`**/*${suffix}`, {
15+
cwd: (await isDirExists(path.join(cwd, 'src/pages')))
16+
? path.join(cwd, 'src/pages')
17+
: path.join(cwd, 'pages'),
18+
});
19+
20+
return pagesFiles.map((file) => {
21+
const parsed = path.parse(file);
22+
if (parsed.name === 'index') return parsed.dir;
23+
24+
return path.join(parsed.dir, parsed.name);
25+
});
26+
}
27+
28+
const result: ScanResult = { urls: new Map(), fallbackUrls: [] };
29+
const files = options.pages ?? (await getFiles());
30+
31+
for (const file of files) {
32+
populateToScanResult(file.split(path.sep), options, result);
33+
}
34+
35+
return result;
36+
}

src/scan.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as Next from './presets/next';
22
import * as Astro from './presets/astro';
3+
import * as Nuxt from './presets/nuxt';
34

45
export type PopulateParams = Record<
56
string,
@@ -45,8 +46,10 @@ export async function scanURLs({
4546
/**
4647
* @default next
4748
*/
48-
preset?: 'next' | 'astro';
49+
preset?: 'next' | 'astro' | 'nuxt';
4950
} = {}): Promise<ScanResult> {
5051
if (preset === 'astro') return Astro.scanURLs(options);
52+
if (preset === 'nuxt') return Nuxt.scanURLs(options);
53+
5154
return Next.scanURLs(options);
5255
}

test/fixture/nuxt-1/pages/blog/[id].vue

Whitespace-only changes.

test/fixture/nuxt-1/pages/docs/[...slug].vue

Whitespace-only changes.

test/fixture/nuxt-1/pages/index.vue

Whitespace-only changes.

test/scan.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,34 @@ test('scan router from file system: Astro', async () => {
6161
}
6262
`);
6363
});
64+
65+
test('scan router from file system: Nuxt', async () => {
66+
const urls = await scanURLs({
67+
preset: 'nuxt',
68+
cwd: path.join(fileURLToPath(import.meta.url), '../fixture/nuxt-1'),
69+
populate: {
70+
'docs/[...slug]': [
71+
{
72+
value: 'test',
73+
},
74+
],
75+
},
76+
});
77+
78+
expect(urls).toMatchInlineSnapshot(`
79+
{
80+
"fallbackUrls": [
81+
{
82+
"meta": {},
83+
"url": /\\^\\\\/blog\\\\/\\(\\.\\+\\)\\$/,
84+
},
85+
],
86+
"urls": Map {
87+
"/" => {},
88+
"/docs/test" => {
89+
"value": "test",
90+
},
91+
},
92+
}
93+
`);
94+
});

0 commit comments

Comments
 (0)