From 83653c83cf74d2d6bf4ff26c209c3311a8754765 Mon Sep 17 00:00:00 2001 From: KishiTheMechanic Date: Mon, 28 Oct 2024 01:12:46 +0100 Subject: [PATCH] fixing --- deno.json | 2 +- mod.ts | 83 ++++++++++++++++++++++++++++++++++--------------------- 2 files changed, 52 insertions(+), 33 deletions(-) diff --git a/deno.json b/deno.json index 22650c5..2314684 100644 --- a/deno.json +++ b/deno.json @@ -1,6 +1,6 @@ { "name": "@elsoul/fresh-sitemap", - "version": "0.7.2", + "version": "0.7.3", "description": "Lightweight global state management library for Fresh framework using Preact signals.", "runtimes": ["deno", "browser"], "exports": "./mod.ts", diff --git a/mod.ts b/mod.ts index c23661c..00c1881 100644 --- a/mod.ts +++ b/mod.ts @@ -91,7 +91,8 @@ export async function saveSitemapAndRobots( } /** - * Generates sitemap entries for static routes, excluding dynamic and grouping directories. + * Generates sitemap entries for static routes, excluding unnecessary paths such as + * dynamic routes, private routes (e.g., _ files), and index files. * @param basename - The base URL * @param distDirectory - Directory containing routes * @param options - Options for sitemap generation @@ -102,55 +103,73 @@ async function generateSitemap( distDirectory: string, options: SiteMapOptions, ): Promise { + const sitemapSet = new Set() // To store unique paths const sitemap: Sitemap = [] - const include = options.include && globToRegExp(options.include) - const exclude = options.exclude && globToRegExp(options.exclude) + const include = options.include ? globToRegExp(options.include) : null + const exclude = options.exclude ? globToRegExp(options.exclude) : null + // Helper function to process and clean each path + function processPath(relPath: string): string | null { + const segments = relPath + .split(SEPARATOR) + .filter((segment) => + !segment.startsWith('_') && // Exclude private routes + !segment.startsWith('(') && // Exclude grouping directories + segment !== 'index' // Exclude 'index' files + ) + if (segments.length === 0) return null + + let pathname = normalize(`/${segments.join('/')}`) + pathname = pathname.replace(/\.tsx$/, '') // Remove .tsx extension + + // Check include/exclude patterns only if they are RegExp instances + if ( + (exclude instanceof RegExp && exclude.test(pathname)) || + (include instanceof RegExp && !include.test(pathname)) + ) { + return null + } + return pathname + } + + // Collect and process paths async function addDirectory(directory: string) { for await (const path of stableRecurseFiles(directory)) { const relPath = distDirectory === '.' ? path : path.substring(distDirectory.length) - const segments = normalize(`/${relPath}`).split(SEPARATOR) - - // Filter out unwanted segments at this level - const filteredSegments = segments.filter((segment) => { - return !( - segment.startsWith('_') || // Exclude segments starting with '_' - segment === 'index' || // Exclude 'index' segments - segment.match(/\(.*?\)/) // Exclude segments like '(default)' - ) - }) - - if (filteredSegments.length === 0) continue // Skip if no valid segments remain - - // Construct valid pathname - const pathname = `/${filteredSegments.join('/')}` - - // Apply include/exclude filters if specified - const isExcluded = exclude && exclude.test(pathname.substring(1)) - const isIncluded = !include || include.test(pathname.substring(1)) - if (isExcluded || !isIncluded) continue + const pathname = processPath(relPath) + if (!pathname) continue // Skip if pathname is null or filtered const { mtime } = await Deno.stat(path) - sitemap.push({ - loc: `${basename.replace(/\/+$/, '')}${pathname}`, - lastmod: (mtime ?? new Date()).toISOString(), - }) + sitemapSet.add( + JSON.stringify({ + loc: basename + pathname, + lastmod: (mtime ?? new Date()).toISOString(), + }), + ) - // Add paths for each specified language, if applicable + // Add localized paths if languages are specified options.languages?.forEach((lang) => { if (lang !== options.defaultLanguage) { - sitemap.push({ - loc: `${basename}/${lang}${pathname}`, - lastmod: (mtime ?? new Date()).toISOString(), - }) + sitemapSet.add( + JSON.stringify({ + loc: `${basename}/${lang}${pathname}`, + lastmod: (mtime ?? new Date()).toISOString(), + }), + ) } }) } } await addDirectory(distDirectory) + + // Convert set entries to the sitemap array + for (const entry of sitemapSet) { + sitemap.push(JSON.parse(entry)) + } + return sitemap }