UNPKG

@intlayer/core

Version:

Includes core Intlayer functions like translation, dictionary, and utility functions shared across multiple packages.

112 lines (110 loc) 4.15 kB
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); const require_localization_getPrefix = require('./getPrefix.cjs'); const require_localization_getMultilingualUrls = require('./getMultilingualUrls.cjs'); //#region src/localization/generateSitemap.ts /** * Returns whether xhtml:link alternate tags should be generated for the given routing mode. * * Alternates are meaningful only when locale URLs are distinct: * - 'no-prefix' produces identical URLs for all locales → no alternates * - all other modes produce distinct URLs → alternates are generated */ const shouldIncludeAlternates = (mode, xhtmlLinks) => xhtmlLinks && mode !== "no-prefix"; /** * Generates a single `<url>` sitemap entry for the given path. * * Example: * * ```ts * generateSitemapUrl('/dashboard', { * siteUrl: 'https://example.com', * changefreq: 'weekly', * priority: 0.8, * xhtmlLinks: true, * locales: ['en', 'fr'], * defaultLocale: 'en', * mode: 'prefix-no-default', * }); * // Returns: * // <url> * // <loc>https://example.com/dashboard</loc> * // <changefreq>weekly</changefreq> * // <priority>0.8</priority> * // <xhtml:link rel="alternate" hrefLang="en" href="https://example.com/dashboard"/> * // <xhtml:link rel="alternate" hrefLang="fr" href="https://example.com/fr/dashboard"/> * // <xhtml:link rel="alternate" hrefLang="x-default" href="https://example.com/dashboard"/> * // </url> * ``` * * @param path - The canonical path to generate the entry for. * @param options - Configuration options. * @returns A `<url>` XML string. */ const generateSitemapUrl = (path, options) => { const { siteUrl, changefreq, priority, lastmod, xhtmlLinks = true, ...routingOptions } = options; const resolved = require_localization_getPrefix.resolveRoutingConfig(routingOptions); const fullUrl = `${siteUrl}${path}`; const lines = [ " <url>", ` <loc>${fullUrl}</loc>`, lastmod ? ` <lastmod>${lastmod}</lastmod>` : null, changefreq ? ` <changefreq>${changefreq}</changefreq>` : null, priority !== void 0 ? ` <priority>${priority}</priority>` : null ]; const hasFileExtension = /\.[a-z0-9]+$/i.test(path); if (shouldIncludeAlternates(resolved.mode, xhtmlLinks) && !hasFileExtension) { const alternates = require_localization_getMultilingualUrls.getMultilingualUrls(path, routingOptions); for (const [lang, localePath] of Object.entries(alternates)) lines.push(` <xhtml:link rel="alternate" hreflang="${lang}" href="${siteUrl}${localePath}"/>`); lines.push(` <xhtml:link rel="alternate" hreflang="x-default" href="${fullUrl}"/>`); } lines.push(" </url>"); return lines.filter(Boolean).join("\n"); }; /** * Generates a full XML sitemap string from an array of URL entries. * * Automatically adds `xmlns:xhtml` to the `<urlset>` declaration when * xhtml:link alternate tags are included. * * Example: * * ```ts * generateSitemap( * [ * { path: '/', changefreq: 'daily', priority: 1.0 }, * { path: '/about', changefreq: 'monthly', priority: 0.5 }, * ], * { * siteUrl: 'https://example.com', * xhtmlLinks: true, * locales: ['en', 'fr'], * defaultLocale: 'en', * mode: 'prefix-no-default', * } * ); * ``` * * @param entries - Array of URL entries to include in the sitemap. * @param options - Sitemap generation options. * @returns A full XML sitemap string. */ const generateSitemap = (entries, options) => { const { siteUrl, xhtmlLinks = true, ...routingOptions } = options; const includeAlternates = shouldIncludeAlternates(require_localization_getPrefix.resolveRoutingConfig(routingOptions).mode, xhtmlLinks); const xmlEntries = entries.map((entry) => generateSitemapUrl(entry.path, { ...entry, siteUrl, xhtmlLinks, ...routingOptions })); return `<?xml version="1.0" encoding="UTF-8"?> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"${includeAlternates ? "\n xmlns:xhtml=\"http://www.w3.org/1999/xhtml\"" : ""} > ${xmlEntries.join("\n")} </urlset>`; }; //#endregion exports.generateSitemap = generateSitemap; exports.generateSitemapUrl = generateSitemapUrl; //# sourceMappingURL=generateSitemap.cjs.map