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