UNPKG

nuxt-simple-sitemap

Version:

Powerfully flexible XML Sitemaps that integrate seamlessly, for Nuxt.

51 lines (50 loc) 1.69 kB
import { parseURL } from "ufo"; import { createRouter, toRouteMatcher } from "radix3"; import { getPathRobotConfig } from "#imports"; function createFilter(options = {}) { const include = options.include || []; const exclude = options.exclude || []; if (include.length === 0 && exclude.length === 0) return () => true; return function(path) { for (const v of [{ rules: exclude, result: false }, { rules: include, result: true }]) { const regexRules = v.rules.filter((r) => r instanceof RegExp); if (regexRules.some((r) => r.test(path))) return v.result; const stringRules = v.rules.filter((r) => typeof r === "string"); if (stringRules.length > 0) { const routes = {}; for (const r of stringRules) { if (r === path) return v.result; routes[r] = true; } const routeRulesMatcher = toRouteMatcher(createRouter({ routes, strictTrailingSlash: false })); if (routeRulesMatcher.matchAll(path).length > 0) return Boolean(v.result); } } return include.length === 0; }; } export function filterSitemapUrls(_urls, options) { const urlFilter = createFilter({ include: options.include, exclude: options.exclude }); return _urls.filter((e) => { let path = e.loc; try { path = parseURL(e.loc).pathname; } catch { return false; } if (!urlFilter(path)) return false; if (options.isMultiSitemap && e._sitemap && options.sitemapName) return e._sitemap === options.sitemapName; if (!getPathRobotConfig(e, { path, skipSiteIndexable: true }).indexable) return false; return true; }); }