deno.land / x / lume@v2.1.4 / plugins / sitemap.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import { merge } from "../core/utils/object.ts";import { Page } from "../core/file.ts";import { stringify } from "../deps/xml.ts";
import type Site from "../core/site.ts";import type { Data } from "../core/file.ts";
type ChangeFreq = | "always" | "hourly" | "daily" | "weekly" | "monthly" | "yearly" | "never";
export interface Options { /** The sitemap file name */ filename?: string;
/** The query to search pages included in the sitemap */ query?: string;
/** The values to sort the sitemap */ sort?: string;
/** The key to use for the lastmod field or a custom function */ lastmod?: string | ((data: Data) => Date);
/** The key to use for the changefreq field or a custom function */ changefreq?: string | ((data: Data) => ChangeFreq);
/** The key to use for the priority field or a custom function */ priority?: string | ((data: Data) => number);}
// Default optionsexport const defaults: Options = { filename: "/sitemap.xml", query: "", sort: "url=asc", lastmod: "date",};
/** A plugin to generate a sitemap.xml from page files after build */export default function (userOptions?: Options) { const options = merge(defaults, userOptions);
return (site: Site) => { site.addEventListener("beforeSave", async () => { // Create the sitemap.xml page const sitemap = Page.create({ url: options.filename, content: generateSitemap( site.search.pages(options.query, options.sort), ), });
// Add the sitemap page to pages site.pages.push(sitemap);
// Add or update `robots.txt` with the sitemap url const robots = await site.getOrCreatePage("/robots.txt"); const content = robots.content as string || `User-agent: *\nAllow: /\n`; robots.content = `${content}\nSitemap: ${ site.url(options.filename, true) }`; });
function generateSitemap(pages: Data[]): string { const sitemap = { xml: { "@version": "1.0", "@encoding": "UTF-8", }, urlset: { "@xmlns": "http://www.sitemaps.org/schemas/sitemap/0.9", "@xmlns:xhtml": "http://www.w3.org/1999/xhtml", url: pages.map((data) => { const node: UrlItem = { loc: site.url(data.url, true), };
const lastmod = getValue<Date>(data, options.lastmod) ?.toISOString(); if (lastmod) { node.lastmod = lastmod; }
const changefreq = getValue<ChangeFreq>(data, options.changefreq); if (changefreq) { node.changefreq = changefreq; }
const priority = getValue<number>(data, options.priority); if (priority) { node.priority = priority; }
if (data.alternates?.length) { node["xhtml:link"] = data.alternates.map((alternate: Data) => ({ "@rel": "alternate", "@hreflang": alternate.lang!, "@href": site.url(alternate.url, true), })); } if (data.unmatchedLangUrl) { node["xhtml:link"]?.push({ "@rel": "alternate", "@hreflang": "x-default", "@href": site.url(data.unmatchedLangUrl, true), }); }
return node; }), }, };
return stringify(sitemap); } };}
interface UrlItem { loc: string; lastmod?: string; changefreq?: ChangeFreq; priority?: number; "xhtml:link"?: { "@rel": "alternate"; "@hreflang": string; "@href": string; }[];}
function getValue<T>( data: Data, key?: string | ((data: Data) => T),): T | undefined { if (!key) { return undefined; }
if (typeof key === "function") { return key(data); }
return data[key];}
lume

Version Info

Tagged at
5 months ago