|
| 1 | +import Path from "path"; |
| 2 | +import { Component, RendererComponent } from "../components"; |
| 3 | +import { RendererEvent } from "../events"; |
| 4 | +import { DefaultTheme } from "../themes/default/DefaultTheme"; |
| 5 | +import { Option, writeFile } from "../../utils"; |
| 6 | +import { escapeHtml } from "../../utils/html"; |
| 7 | + |
| 8 | +@Component({ name: "sitemap" }) |
| 9 | +export class SitemapPlugin extends RendererComponent { |
| 10 | + @Option("sitemapBaseUrl") |
| 11 | + accessor sitemapBaseUrl!: string; |
| 12 | + |
| 13 | + override initialize() { |
| 14 | + this.listenTo(this.owner, RendererEvent.BEGIN, this.onRendererBegin); |
| 15 | + } |
| 16 | + |
| 17 | + private onRendererBegin(event: RendererEvent) { |
| 18 | + if (!(this.owner.theme instanceof DefaultTheme)) { |
| 19 | + return; |
| 20 | + } |
| 21 | + if (event.isDefaultPrevented || !this.sitemapBaseUrl) { |
| 22 | + return; |
| 23 | + } |
| 24 | + |
| 25 | + this.owner.preRenderAsyncJobs.push((event) => this.buildSitemap(event)); |
| 26 | + } |
| 27 | + |
| 28 | + private async buildSitemap(event: RendererEvent) { |
| 29 | + // cSpell:words lastmod urlset |
| 30 | + const sitemapXml = Path.join(event.outputDirectory, "sitemap.xml"); |
| 31 | + const lastmod = new Date(this.owner.renderStartTime).toISOString(); |
| 32 | + |
| 33 | + const urls: XmlElementData[] = |
| 34 | + event.urls?.map((url) => { |
| 35 | + return { |
| 36 | + tag: "url", |
| 37 | + children: [ |
| 38 | + { |
| 39 | + tag: "loc", |
| 40 | + children: new URL( |
| 41 | + url.url, |
| 42 | + this.sitemapBaseUrl, |
| 43 | + ).toString(), |
| 44 | + }, |
| 45 | + { |
| 46 | + tag: "lastmod", |
| 47 | + children: lastmod, |
| 48 | + }, |
| 49 | + ], |
| 50 | + }; |
| 51 | + }) ?? []; |
| 52 | + |
| 53 | + const sitemap = |
| 54 | + `<?xml version="1.0" encoding="UTF-8"?>\n` + |
| 55 | + stringifyXml({ |
| 56 | + tag: "urlset", |
| 57 | + attr: { xmlns: "http://www.sitemaps.org/schemas/sitemap/0.9" }, |
| 58 | + children: urls, |
| 59 | + }) + |
| 60 | + "\n"; |
| 61 | + |
| 62 | + await writeFile(sitemapXml, sitemap); |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +interface XmlElementData { |
| 67 | + attr?: Record<string, string>; |
| 68 | + tag: string; |
| 69 | + children: XmlElementData[] | string; |
| 70 | +} |
| 71 | + |
| 72 | +function stringifyXml(xml: XmlElementData, indent = 0) { |
| 73 | + const parts = ["\t".repeat(indent), "<", xml.tag]; |
| 74 | + |
| 75 | + for (const [key, val] of Object.entries(xml.attr || {})) { |
| 76 | + parts.push(" ", key, '="', escapeHtml(val), '"'); |
| 77 | + } |
| 78 | + |
| 79 | + parts.push(">"); |
| 80 | + |
| 81 | + if (typeof xml.children === "string") { |
| 82 | + parts.push(escapeHtml(xml.children)); |
| 83 | + } else { |
| 84 | + for (const child of xml.children) { |
| 85 | + parts.push("\n"); |
| 86 | + parts.push(stringifyXml(child, indent + 1)); |
| 87 | + } |
| 88 | + parts.push("\n", "\t".repeat(indent)); |
| 89 | + } |
| 90 | + |
| 91 | + parts.push("</", xml.tag, ">"); |
| 92 | + |
| 93 | + return parts.join(""); |
| 94 | +} |
0 commit comments