nuxt-simple-sitemap
Version:
Powerfully flexible XML Sitemaps that integrate seamlessly, for Nuxt.
129 lines (128 loc) • 4.8 kB
JavaScript
import { joinURL, parseURL, withHttps, withLeadingSlash } from "ufo";
import { splitForLocales } from "../../utils-pure.mjs";
export function normaliseI18nSources(sources, { autoI18n, isI18nMapped }) {
if (autoI18n && isI18nMapped) {
return sources.map((s) => {
const urls = (s.urls || []).map((_url) => {
const url = typeof _url === "string" ? { loc: _url } : _url;
url.loc = url.loc || url.url;
url.loc = withLeadingSlash(url.loc);
return url;
});
s.urls = urls.map((url) => {
if (url._sitemap || url._i18nTransform)
return url;
if (url.loc) {
const match = splitForLocales(url.loc, autoI18n.locales.map((l) => l.code));
const localeCode = match[0] || autoI18n.defaultLocale;
const pathWithoutPrefix = match[1];
const locale = autoI18n.locales.find((e) => e.code === localeCode);
if (locale) {
if (!url.alternatives) {
const alternatives = urls.map((u) => {
if (u._sitemap || u._i18nTransform)
return false;
if (u?.loc) {
const [_localeCode, _pathWithoutPrefix] = splitForLocales(u.loc, autoI18n.locales.map((l) => l.code));
if (pathWithoutPrefix === _pathWithoutPrefix) {
const entries = [];
if (_localeCode === autoI18n.defaultLocale) {
entries.push({
href: u.loc,
hreflang: "x-default"
});
}
entries.push({
href: u.loc,
hreflang: _localeCode || autoI18n.defaultLocale
});
return entries;
}
}
return false;
}).flat().filter(Boolean);
if (alternatives.length)
url.alternatives = alternatives;
}
return {
_sitemap: locale.iso || locale.code,
...url
};
}
}
return url;
});
return s;
});
}
return sources;
}
export function applyI18nEnhancements(_urls, options) {
const { autoI18n } = options;
return _urls.map((e) => {
if (!e._i18nTransform)
return e;
delete e._i18nTransform;
const path = withLeadingSlash(parseURL(e.loc).pathname);
const match = splitForLocales(path, autoI18n.locales.map((l) => l.code));
let pathWithoutLocale = path;
let locale;
if (match[0]) {
pathWithoutLocale = match[1] || "/";
locale = match[0];
}
if (locale && import.meta.dev) {
console.warn("You're providing a locale in the url, but the url is marked as inheritI18n. This will cause issues with the sitemap. Please remove the locale from the url.");
return e;
}
if (autoI18n.differentDomains) {
return {
// will force it to pass filter
_sitemap: options.sitemapName,
...e,
alternatives: [
{
// apply default locale domain
...autoI18n.locales.find((l) => [l.code, l.iso].includes(autoI18n.defaultLocale)),
code: "x-default"
},
...autoI18n.locales.filter((l) => !!l.domain)
].map((locale2) => {
return {
hreflang: locale2.iso || locale2.code,
href: joinURL(withHttps(locale2.domain), pathWithoutLocale)
};
})
};
}
return autoI18n.locales.map((l) => {
let loc = joinURL(`/${l.code}`, pathWithoutLocale);
if (autoI18n.differentDomains || ["prefix_and_default", "prefix_except_default"].includes(autoI18n.strategy) && l.code === autoI18n.defaultLocale)
loc = pathWithoutLocale;
return {
_sitemap: options.isI18nMapped ? l.iso || l.code : void 0,
...e,
loc,
alternatives: [{ code: "x-default" }, ...autoI18n.locales].map((locale2) => {
const code = locale2.code === "x-default" ? autoI18n.defaultLocale : locale2.code;
const isDefault = locale2.code === "x-default" || locale2.code === autoI18n.defaultLocale;
let href = "";
if (autoI18n.strategy === "prefix") {
href = joinURL("/", code, pathWithoutLocale);
} else if (["prefix_and_default", "prefix_except_default"].includes(autoI18n.strategy)) {
if (isDefault) {
href = pathWithoutLocale;
} else {
href = joinURL("/", code, pathWithoutLocale);
}
}
const hreflang = locale2.iso || locale2.code;
return {
hreflang,
href
};
})
};
});
}).flat();
}