Skip to content

Commit

Permalink
Add sitemap script to website and change config
Browse files Browse the repository at this point in the history
  • Loading branch information
fabian-hiller committed Dec 8, 2023
1 parent 6b5afe1 commit 3b39ed6
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 6 deletions.
1 change: 1 addition & 0 deletions website/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
public/sitemap.xml
9 changes: 8 additions & 1 deletion website/adapters/netlify-edge/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ export default extendConfig(baseConfig, () => {
},
outDir: '.netlify/edge-functions/entry.netlify-edge',
},
plugins: [netlifyEdgeAdapter()],
plugins: [
netlifyEdgeAdapter({
ssg: {
include: [],
sitemapOutFile: null,
},
}),
],
};
});
2 changes: 1 addition & 1 deletion website/netlify.toml
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"
1 change: 1 addition & 0 deletions website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"build.server": "vite build -c adapters/netlify-edge/vite.config.ts",
"build.types": "tsc --incremental --noEmit",
"contributors": "tsm ./scripts/contributors.ts",
"sitemap": "tsm ./scripts/sitemap.ts",
"deploy": "netlify deploy --build",
"dev": "vite --mode ssr",
"dev.debug": "node --inspect-brk ./node_modules/vite/bin/vite.js --mode ssr --force",
Expand Down
8 changes: 4 additions & 4 deletions website/scripts/contributors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ const GITHUB_PERSONAL_ACCESS_TOKEN = process.env.GITHUB_PERSONAL_ACCESS_TOKEN;
const EXCLUDED_COMMITS = ['2cc351f6db7798cf60276225abcbacbc1ea491db'];

/**
* Finds all index files in the given directory.
* Finds all index files in the given directories.
*
* @param directories The directories to search in.
*/
async function findIndexFiles(directories: string[]) {
function findIndexFiles(directories: string[]) {
// Create file paths list
const filePaths: string[] = [];

Expand All @@ -40,7 +40,7 @@ async function findIndexFiles(directories: string[]) {
const itemPath = path.join(directory, itemName);
const itemStat = fs.statSync(itemPath);
if (itemStat.isDirectory()) {
filePaths.push(...(await findIndexFiles([itemPath])));
filePaths.push(...findIndexFiles([itemPath]));
}
}
}
Expand All @@ -60,7 +60,7 @@ async function updateContributors() {
}

// Find all MDX files of guides and API reference
const filePaths = await findIndexFiles([
const filePaths = findIndexFiles([
path.join('src', 'routes', 'guides'),
path.join('src', 'routes', 'api'),
]);
Expand Down
71 changes: 71 additions & 0 deletions website/scripts/sitemap.ts
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();

0 comments on commit 3b39ed6

Please sign in to comment.