one
Version:
One is a new React Framework that makes Vite serve both native and web.
53 lines (52 loc) • 1.87 kB
JavaScript
import MicroMatch from "micromatch";
function generateSitemap(routes, options) {
const envUrl = process.env.ONE_SERVER_URL,
baseUrl = options.baseUrl ?? (envUrl && envUrl !== "undefined" ? envUrl : ""),
defaultPriority = options.priority ?? 0.5,
defaultChangefreq = options.changefreq,
excludePatterns = options.exclude || [],
entries = [];
for (const route of routes) {
const {
path,
routeExport
} = route;
if (routeExport?.exclude || excludePatterns.length > 0 && MicroMatch.isMatch(path, excludePatterns)) continue;
const priority = routeExport?.priority ?? defaultPriority,
changefreq = routeExport?.changefreq ?? defaultChangefreq,
lastmod = routeExport?.lastmod;
entries.push({
path,
priority,
changefreq,
lastmod
});
}
return buildSitemapXml(entries, baseUrl);
}
function buildSitemapXml(entries, baseUrl) {
return `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
${entries.map(entry => {
const loc = baseUrl ? `${baseUrl.replace(/\/$/, "")}${entry.path}` : entry.path;
let xml = ` <url>
<loc>${escapeXml(loc)}</loc>`;
if (entry.lastmod) {
const date = entry.lastmod instanceof Date ? entry.lastmod.toISOString().split("T")[0] : entry.lastmod;
xml += `
<lastmod>${escapeXml(date)}</lastmod>`;
}
return entry.changefreq && (xml += `
<changefreq>${entry.changefreq}</changefreq>`), entry.priority !== void 0 && (xml += `
<priority>${entry.priority.toFixed(1)}</priority>`), xml += `
</url>`, xml;
}).join(`
`)}
</urlset>
`;
}
function escapeXml(str) {
return str.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
}
export { generateSitemap };
//# sourceMappingURL=generateSitemap.mjs.map