MCPcopy Create free account
hub / github.com/AnswerOverflow/AnswerOverflow / toXml

Method toXml

packages/utils/src/sitemap.ts:32–67  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

30 }
31
32 toXml() {
33 if (this.entries.length > 50000)
34 throw new Error('Sitemap cannot have more than 50,000 entries');
35
36 const urls = this.entries.map((entry) => {
37 let data = '';
38 data += this.type === 'sitemap' ? '<sitemap>' : '<url>';
39 data += `<loc>${
40 entry.loc.startsWith('http') ? entry.loc : this.baseUrl + entry.loc
41 }</loc>`;
42 if (entry.lastmod)
43 data += `<lastmod>${entry.lastmod.toISOString()}</lastmod>`;
44 if (entry.changefreq && this.type === 'url')
45 data += `<changefreq>${entry.changefreq}</changefreq>`;
46 data += this.type === 'sitemap' ? '</sitemap>' : '</url>';
47 return data;
48 });
49
50 if (this.type === 'url') {
51 return (
52 `<?xml version="1.0" encoding="UTF-8"?>
53 <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">` +
54 urls.join('') +
55 `
56 </urlset>`
57 );
58 } else {
59 return (
60 `<?xml version="1.0" encoding="UTF-8"?>
61 <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">` +
62 urls.join('') +
63 `
64 </sitemapindex>`
65 );
66 }
67 }
68
69 applyToRes(res: ServerResponse<IncomingMessage>) {
70 res.setHeader('Content-Type', 'text/xml');

Callers 2

applyToResMethod · 0.95
generateSitemapFunction · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected