-
-
Notifications
You must be signed in to change notification settings - Fork 217
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add sitemap script to website and change config
- Loading branch information
1 parent
6b5afe1
commit 3b39ed6
Showing
6 changed files
with
86 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
public/sitemap.xml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
[build] | ||
publish = "dist" | ||
command = "npm run build" | ||
command = "npm run sitemap && npm run build" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import fs from 'node:fs'; | ||
import path from 'node:path'; | ||
|
||
const ORIGIN = 'https://valibot.dev/'; | ||
|
||
/** | ||
* Finds all index files in the given directory. | ||
* | ||
* @param directory The directory to search in. | ||
*/ | ||
function findIndexFiles(directory: string) { | ||
// Create file paths list | ||
const filePaths: string[] = []; | ||
|
||
// Get items of directory | ||
const items = fs.readdirSync(directory); | ||
|
||
for (const itemName of items) { | ||
// If item is a index file, add it to list | ||
if (itemName === 'index.tsx' || itemName === 'index.mdx') { | ||
filePaths.push(path.join(directory, itemName)); | ||
|
||
// Otherwise, search for nested index files | ||
} else { | ||
const itemPath = path.join(directory, itemName); | ||
const itemStat = fs.statSync(itemPath); | ||
if (itemStat.isDirectory()) { | ||
filePaths.push(...findIndexFiles(itemPath)); | ||
} | ||
} | ||
} | ||
|
||
// Return file paths list | ||
return filePaths; | ||
} | ||
|
||
/** | ||
* Generates the sitemap of the website. | ||
*/ | ||
async function generateSitemap() { | ||
// Find all route index files | ||
const filePaths = findIndexFiles(path.join('src', 'routes')); | ||
|
||
// Create URL paths and sort them | ||
const urlSet = filePaths | ||
// Transform file paths to URL paths | ||
.map((filePath) => | ||
filePath | ||
.replace(/\\/g, '/') | ||
.replace(/src\/routes\//, '') | ||
.replace(/\(.+\)\//, '') | ||
.replace(/index\.(tsx|mdx)/, '') | ||
) | ||
// Sort URL paths alphabetically | ||
.sort() | ||
// Reduce URL paths to URL set | ||
.reduce( | ||
(urlPaths, urlPath) => | ||
`${urlPaths}<url><loc>${ORIGIN}${urlPath}</loc></url>`, | ||
'' | ||
); | ||
|
||
// Write sitemap.xml to public directory | ||
fs.writeFileSync( | ||
path.join('public', 'sitemap.xml'), | ||
`<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">${urlSet}</urlset>` | ||
); | ||
} | ||
|
||
// Start generation of sitemap | ||
generateSitemap(); |