Files
outline/server/utils/sitemap.ts
Tom Moor aa8e077649 feat: Add sitemap to publicly shared documents with indexing enabled (#9334)
* quick: Add sitemap to publicly shared documents with indexing enabled

* escape
2025-05-30 17:54:14 -04:00

36 lines
995 B
TypeScript

import escape from "lodash/escape";
import { NavigationNode } from "@shared/types";
/**
* Converts a navigation tree to a sitemap XML string, by traversing the nodes.
*
* @param tree The navigation tree to convert.
* @param baseUrl The base URL to prepend to each node's URL.
* @returns The sitemap XML string.
*/
export function navigationNodeToSitemap(
tree: NavigationNode | undefined | null,
baseUrl: string
): string {
const urls: string[] = [];
function collectUrls(node: NavigationNode, urls: string[]) {
urls.push(`${baseUrl}${node.url}`);
if (node.children) {
node.children.forEach((child) => collectUrls(child, urls));
}
}
if (tree) {
collectUrls(tree, urls);
}
// Build XML
return `<?xml version="1.0" encoding="UTF-8"?>\n<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n${urls
.map(
(url) =>
` <url><loc>${escape(url)}</loc><changefreq>weekly</changefreq></url>`
)
.join("\n")}\n</urlset>`;
}