svelte-sitemap 4.0.0 → 4.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dto/global.interface.d.ts +52 -0
- package/helpers/file.js +1 -1
- package/helpers/global.helper.js +4 -3
- package/helpers/global.helper.js.map +1 -1
- package/package.js +1 -1
- package/package.json +1 -1
|
@@ -2,25 +2,77 @@ import { CHANGE_FREQ, IntegrationMethod } from "../const.js";
|
|
|
2
2
|
|
|
3
3
|
//#region src/dto/global.interface.d.ts
|
|
4
4
|
interface Arguments {
|
|
5
|
+
/**
|
|
6
|
+
* Your website domain URL (e.g., 'https://example.com').
|
|
7
|
+
*/
|
|
5
8
|
domain: string;
|
|
9
|
+
/**
|
|
10
|
+
* Additional sitemap generation options.
|
|
11
|
+
*/
|
|
6
12
|
options?: Options;
|
|
7
13
|
}
|
|
8
14
|
interface Options {
|
|
15
|
+
/**
|
|
16
|
+
* Enable debug mode to display detailed log outputs during execution.
|
|
17
|
+
* @default false
|
|
18
|
+
*/
|
|
9
19
|
debug?: boolean;
|
|
20
|
+
/**
|
|
21
|
+
* How frequently the page content is likely to change.
|
|
22
|
+
* This value provides general information to search engines and may not correlate exactly to how often they crawl the page.
|
|
23
|
+
* @see {@link ChangeFreq}
|
|
24
|
+
*/
|
|
10
25
|
changeFreq?: ChangeFreq;
|
|
26
|
+
/**
|
|
27
|
+
* If set to true, resets the last modified time (`lastmod`) of all pages to the current date/time.
|
|
28
|
+
* @default false
|
|
29
|
+
*/
|
|
11
30
|
resetTime?: boolean;
|
|
31
|
+
/**
|
|
32
|
+
* Custom build/output folder containing the static HTML files.
|
|
33
|
+
* @default 'build'
|
|
34
|
+
*/
|
|
12
35
|
outDir?: string;
|
|
36
|
+
/**
|
|
37
|
+
* Whether to include an attribution comment ("Generated by svelte-sitemap") in the output sitemap.xml.
|
|
38
|
+
* @default true
|
|
39
|
+
*/
|
|
13
40
|
attribution?: boolean;
|
|
41
|
+
/**
|
|
42
|
+
* Glob patterns or specific file/directory paths to exclude from the sitemap.
|
|
43
|
+
* @default []
|
|
44
|
+
* @example `ignore: ['**\/admin/**', 'my-secret-page']`
|
|
45
|
+
*/
|
|
14
46
|
ignore?: string | string[];
|
|
47
|
+
/**
|
|
48
|
+
* Whether to append a trailing slash to all page URLs in the sitemap.
|
|
49
|
+
* @default false
|
|
50
|
+
*/
|
|
15
51
|
trailingSlashes?: boolean;
|
|
52
|
+
/**
|
|
53
|
+
* Array of additional page paths to manually add to the sitemap (e.g. paths external to SvelteKit or static assets).
|
|
54
|
+
* @example `additional: ['my-page', 'my-second-page']`
|
|
55
|
+
*/
|
|
16
56
|
additional?: string[];
|
|
17
57
|
}
|
|
18
58
|
interface OptionsSvelteSitemap extends Options {
|
|
59
|
+
/**
|
|
60
|
+
* Your website domain URL (e.g., 'https://example.com').
|
|
61
|
+
*/
|
|
19
62
|
domain: string;
|
|
20
63
|
}
|
|
21
64
|
interface PagesJson {
|
|
65
|
+
/**
|
|
66
|
+
* The path or URL of the page.
|
|
67
|
+
*/
|
|
22
68
|
page: string;
|
|
69
|
+
/**
|
|
70
|
+
* How frequently the page content is likely to change.
|
|
71
|
+
*/
|
|
23
72
|
changeFreq?: ChangeFreq;
|
|
73
|
+
/**
|
|
74
|
+
* The last modified timestamp of the page.
|
|
75
|
+
*/
|
|
24
76
|
lastMod?: string;
|
|
25
77
|
}
|
|
26
78
|
/**
|
package/helpers/file.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { existsSync } from "fs";
|
|
2
|
-
import { createJiti } from "jiti";
|
|
3
2
|
import { resolve } from "path";
|
|
3
|
+
import { createJiti } from "jiti";
|
|
4
4
|
import { fileURLToPath } from "url";
|
|
5
5
|
//#region src/helpers/file.ts
|
|
6
6
|
const jiti = createJiti(fileURLToPath(import.meta.url));
|
package/helpers/global.helper.js
CHANGED
|
@@ -3,6 +3,7 @@ import { version as version$1 } from "../package.js";
|
|
|
3
3
|
import { cliColors, errorMsgFolder, errorMsgHtmlFiles, errorMsgWrite, successMsg } from "./vars.helper.js";
|
|
4
4
|
import fg from "fast-glob";
|
|
5
5
|
import fs from "fs";
|
|
6
|
+
import path from "path";
|
|
6
7
|
import { create } from "xmlbuilder2";
|
|
7
8
|
//#region src/helpers/global.helper.ts
|
|
8
9
|
const version = version$1;
|
|
@@ -53,10 +54,10 @@ const detectErrors = ({ folder, htmlFiles }, outDir = OUT_DIR) => {
|
|
|
53
54
|
else if (htmlFiles) console.error(cliColors.red, errorMsgHtmlFiles(outDir));
|
|
54
55
|
};
|
|
55
56
|
const checkPrerenderRoutes = async (pages, outDir, options) => {
|
|
56
|
-
if (fs.existsSync(
|
|
57
|
+
if (fs.existsSync(path.join(outDir, "_app")) && pages.length > 0) {
|
|
57
58
|
const hasOnlyRootOrFallback = pages.every((page) => {
|
|
58
|
-
const
|
|
59
|
-
return
|
|
59
|
+
const relative = path.relative(outDir, page);
|
|
60
|
+
return relative === "index.html" || relative === "fallback.html";
|
|
60
61
|
});
|
|
61
62
|
const hasNoAdditional = !options?.additional || options.additional.length === 0;
|
|
62
63
|
if (hasOnlyRootOrFallback && hasNoAdditional) console.warn(cliColors.yellow, ` ⚠️ Warning: Only the homepage or fallback page was found in '${outDir}/'.\n If your SvelteKit site has multiple routes, make sure you enabled prerendering for them.\n For SPA (Single Page Apps), you can add routes manually using the 'additional' option.`);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"global.helper.js","names":["pkg.version"],"sources":["../../src/helpers/global.helper.ts"],"sourcesContent":["import fg from 'fast-glob';\nimport fs from 'fs';\nimport { create } from 'xmlbuilder2';\nimport type { XMLBuilder } from 'xmlbuilder2/lib/interfaces.js';\nimport pkg from '../../package.json' with { type: 'json' };\nimport { CHANGE_FREQ, CHUNK, OUT_DIR } from '../const.js';\nimport type { ChangeFreq, Options, OptionsSvelteSitemap, PagesJson } from './../dto/index.js';\nimport {\n cliColors,\n errorMsgFolder,\n errorMsgHtmlFiles,\n errorMsgWrite,\n successMsg\n} from './vars.helper.js';\n\nconst version = pkg.version;\n\nconst getUrl = (url: string, domain: string, options: Options) => {\n let slash: '' | '/' = getSlash(domain);\n\n let trimmed = url\n .split((options?.outDir ?? OUT_DIR) + '/')\n .pop()\n .replace('index.html', '');\n\n trimmed = removeHtml(trimmed);\n\n // Add all traling slashes\n if (options?.trailingSlashes) {\n trimmed = trimmed.length && !trimmed.endsWith('/') ? trimmed + '/' : trimmed;\n } else {\n trimmed = trimmed.endsWith('/') ? trimmed.slice(0, -1) : trimmed;\n slash = trimmed ? slash : '';\n }\n\n // URI-encode each path segment to handle special characters (e.g. spaces → %20).\n // Decode first to avoid double-encoding already percent-encoded segments.\n trimmed = trimmed\n .split('/')\n .map((segment) => {\n try {\n return encodeURIComponent(decodeURIComponent(segment));\n } catch {\n return encodeURIComponent(segment);\n }\n })\n .join('/');\n\n return `${domain}${slash}${trimmed}`;\n};\n\nexport const removeHtml = (fileName: string) => {\n if (fileName?.endsWith('.html')) {\n return fileName.slice(0, -5);\n }\n return fileName;\n};\n\nexport async function prepareData(domain: string, options?: Options): Promise<PagesJson[]> {\n const FOLDER = options?.outDir ?? OUT_DIR;\n\n const ignore = prepareIgnored(options?.ignore, options?.outDir);\n const changeFreq = prepareChangeFreq(options);\n const pages: string[] = await fg(`${FOLDER}/**/*.html`, { ignore });\n\n if (options.additional) pages.push(...options.additional);\n\n const results = pages.map((page) => {\n return {\n page: getUrl(page, domain, options),\n changeFreq: changeFreq,\n lastMod: options?.resetTime ? new Date().toISOString().split('T')[0] : ''\n };\n });\n\n detectErrors(\n {\n folder: !fs.existsSync(FOLDER),\n htmlFiles: !pages.length\n },\n FOLDER\n );\n\n await checkPrerenderRoutes(pages, FOLDER, options);\n\n return results;\n}\n\nexport const detectErrors = (\n { folder, htmlFiles }: { folder: boolean; htmlFiles: boolean },\n outDir: string = OUT_DIR\n) => {\n if (folder && htmlFiles) {\n console.error(cliColors.red, errorMsgFolder(outDir));\n } else if (htmlFiles) {\n // If no page exists, then the static adapter is probably not used\n console.error(cliColors.red, errorMsgHtmlFiles(outDir));\n }\n};\n\nexport const checkPrerenderRoutes = async (pages: string[], outDir: string, options?: Options) => {\n // Check if it's a SvelteKit build by checking for the '_app' directory in output folder\n const appDirExists = fs.existsSync(`${outDir}/_app`);\n\n if (appDirExists) {\n const hasOnlyRootOrFallback = pages.every((page) => {\n const basename = page.split('/').pop();\n return basename === 'index.html' || basename === 'fallback.html';\n });\n\n const hasNoAdditional = !options?.additional || options.additional.length === 0;\n\n if (hasOnlyRootOrFallback && hasNoAdditional) {\n console.warn(\n cliColors.yellow,\n ` ⚠️ Warning: Only the homepage or fallback page was found in '${outDir}/'.\\n` +\n ` If your SvelteKit site has multiple routes, make sure you enabled prerendering for them.\\n` +\n ` For SPA (Single Page Apps), you can add routes manually using the 'additional' option.`\n );\n }\n }\n};\n\nexport const writeSitemap = (items: PagesJson[], options: Options, domain: string): void => {\n const outDir = options?.outDir ?? OUT_DIR;\n\n if (items?.length <= CHUNK.maxSize) {\n createFile(items, options, outDir);\n } else {\n // If the number of pages is greater than the chunk size, then we split the sitemap into multiple files\n // and create an index file that links to all of them\n // https://support.google.com/webmasters/answer/183668?hl=en\n const numberOfChunks = Math.ceil(items.length / CHUNK.maxSize);\n\n console.log(\n cliColors.cyanAndBold,\n `> Oh, your site is huge! Writing sitemap in chunks of ${numberOfChunks} pages and its index sitemap.xml`\n );\n\n for (let i = 0; i < items.length; i += CHUNK.maxSize) {\n const chunk = items.slice(i, i + CHUNK.maxSize);\n createFile(chunk, options, outDir, i / CHUNK.maxSize + 1);\n }\n createIndexFile(numberOfChunks, outDir, options, domain);\n }\n};\n\nconst createFile = (\n items: PagesJson[],\n options: Options,\n outDir: string,\n chunkId?: number\n): void => {\n const sitemap = createXml('urlset');\n addAttribution(sitemap, options);\n\n for (const item of items) {\n const page = sitemap.ele('url');\n page.ele('loc').txt(item.page);\n if (item.changeFreq) {\n page.ele('changefreq').txt(item.changeFreq);\n }\n if (item.lastMod) {\n page.ele('lastmod').txt(item.lastMod);\n }\n }\n\n const xml = finishXml(sitemap);\n\n const fileName = chunkId ? `sitemap-${chunkId}.xml` : 'sitemap.xml';\n\n try {\n fs.writeFileSync(`${outDir}/${fileName}`, xml);\n console.log(cliColors.green, successMsg(outDir, fileName));\n } catch (e) {\n console.error(cliColors.red, errorMsgWrite(outDir, fileName), e);\n }\n};\n\nconst createIndexFile = (\n numberOfChunks: number,\n outDir: string,\n options: Options,\n domain: string\n): void => {\n const FILENAME = 'sitemap.xml';\n const slash = getSlash(domain);\n\n const sitemap = createXml('sitemapindex');\n addAttribution(sitemap, options);\n\n for (let i = 1; i <= numberOfChunks; i++) {\n sitemap.ele('sitemap').ele('loc').txt(`${domain}${slash}sitemap-${i}.xml`);\n }\n\n const xml = finishXml(sitemap);\n\n try {\n fs.writeFileSync(`${outDir}/${FILENAME}`, xml);\n console.log(cliColors.green, successMsg(outDir, FILENAME));\n } catch (e) {\n console.error(cliColors.red, errorMsgWrite(outDir, FILENAME), e);\n }\n};\n\nconst prepareIgnored = (\n ignored: string | string[],\n outDir: string = OUT_DIR\n): string[] | undefined => {\n let ignore: string[] | undefined;\n if (ignored) {\n ignore = Array.isArray(ignored) ? ignored : [ignored];\n ignore = ignore.map((ignoredPage) => `${outDir}/${ignoredPage}`);\n }\n return ignore;\n};\n\nconst prepareChangeFreq = (options: Options): ChangeFreq => {\n let result: ChangeFreq = null;\n\n if (options?.changeFreq) {\n if (CHANGE_FREQ.includes(options.changeFreq)) {\n result = options.changeFreq;\n } else {\n console.log(\n cliColors.red,\n ` × Option \\`--change-freq ${options.changeFreq}\\` is not a valid value. See docs: https://github.com/bartholomej/svelte-sitemap#options`\n );\n }\n }\n return result;\n};\n\nconst getSlash = (domain: string) => (domain.split('/').pop() ? '/' : '');\n\nconst createXml = (elementName: 'urlset' | 'sitemapindex'): XMLBuilder => {\n return create({ version: '1.0', encoding: 'UTF-8' }).ele(elementName, {\n xmlns: 'http://www.sitemaps.org/schemas/sitemap/0.9'\n });\n};\n\nconst finishXml = (sitemap: XMLBuilder): string => {\n return sitemap.end({ prettyPrint: true });\n};\n\nconst addAttribution = (sitemap: XMLBuilder, options: Options): void => {\n if (options?.attribution !== false) {\n sitemap.com(\n ` This file was automatically generated by https://github.com/bartholomej/svelte-sitemap v${version} `\n );\n }\n};\n"],"mappings":";;;;;;;AAeA,MAAM,UAAUA;AAEhB,MAAM,UAAU,KAAa,QAAgB,YAAqB;CAChE,IAAI,QAAkB,SAAS,MAAM;CAErC,IAAI,UAAU,IACX,OAAO,SAAS,UAAA,WAAqB,GAAG,CAAC,CACzC,IAAI,CAAC,CACL,QAAQ,cAAc,EAAE;CAE3B,UAAU,WAAW,OAAO;CAG5B,IAAI,SAAS,iBACX,UAAU,QAAQ,UAAU,CAAC,QAAQ,SAAS,GAAG,IAAI,UAAU,MAAM;MAChE;EACL,UAAU,QAAQ,SAAS,GAAG,IAAI,QAAQ,MAAM,GAAG,EAAE,IAAI;EACzD,QAAQ,UAAU,QAAQ;CAC5B;CAIA,UAAU,QACP,MAAM,GAAG,CAAC,CACV,KAAK,YAAY;EAChB,IAAI;GACF,OAAO,mBAAmB,mBAAmB,OAAO,CAAC;EACvD,QAAQ;GACN,OAAO,mBAAmB,OAAO;EACnC;CACF,CAAC,CAAC,CACD,KAAK,GAAG;CAEX,OAAO,GAAG,SAAS,QAAQ;AAC7B;AAEA,MAAa,cAAc,aAAqB;CAC9C,IAAI,UAAU,SAAS,OAAO,GAC5B,OAAO,SAAS,MAAM,GAAG,EAAE;CAE7B,OAAO;AACT;AAEA,eAAsB,YAAY,QAAgB,SAAyC;CACzF,MAAM,SAAS,SAAS,UAAA;CAExB,MAAM,SAAS,eAAe,SAAS,QAAQ,SAAS,MAAM;CAC9D,MAAM,aAAa,kBAAkB,OAAO;CAC5C,MAAM,QAAkB,MAAM,GAAG,GAAG,OAAO,aAAa,EAAE,OAAO,CAAC;CAElE,IAAI,QAAQ,YAAY,MAAM,KAAK,GAA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,QAAQ,QAAQ,CAAC;CAC3D,SAAS,GAAG;EACV,QAAQ,MAAM,UAAU,KAAK,cAAc,QAAQ,QAAQ,GAAG,CAAC;CACjE;AACF;AAEA,MAAM,kBACJ,SACA,SAAiB,YACQ;CACzB,IAAI;CACJ,IAAI,SAAS;EACX,SAAS,MAAM,QAAQ,OAAO,IAAI,UAAU,CAAC,OAAO;EACpD,SAAS,OAAO,KAAK,gBAAgB,GAAG,OAAO,GAAG,aAAa;CACjE;CACA,OAAO;AACT;AAEA,MAAM,qBAAqB,YAAiC;CAC1D,IAAI,SAAqB;CAEzB,IAAI,SAAS,YACX,IAAI,YAAY,SAAS,QAAQ,UAAU,GACzC,SAAS,QAAQ;MAEjB,QAAQ,IACN,UAAU,KACV,8BAA8B,QAAQ,WAAW,yFACnD;CAGJ,OAAO;AACT;AAEA,MAAM,YAAY,WAAoB,OAAO,MAAM,GAAG,CAAC,CAAC,IAAI,IAAI,MAAM;AAEtE,MAAM,aAAa,gBAAuD;CACxE,OAAO,OAAO;EAAE,SAAS;EAAO,UAAU;CAAQ,CAAC,CAAC,CAAC,IAAI,aAAa,EACpE,OAAO,8CACT,CAAC;AACH;AAEA,MAAM,aAAa,YAAgC;CACjD,OAAO,QAAQ,IAAI,EAAE,aAAa,KAAK,CAAC;AAC1C;AAEA,MAAM,kBAAkB,SAAqB,YAA2B;CACtE,IAAI,SAAS,gBAAgB,OAC3B,QAAQ,IACN,4FAA4F,QAAQ,EACtG;AAEJ"}
|
|
1
|
+
{"version":3,"file":"global.helper.js","names":["pkg.version"],"sources":["../../src/helpers/global.helper.ts"],"sourcesContent":["import fg from 'fast-glob';\nimport fs from 'fs';\nimport path from 'path';\nimport { create } from 'xmlbuilder2';\nimport type { XMLBuilder } from 'xmlbuilder2/lib/interfaces.js';\nimport pkg from '../../package.json' with { type: 'json' };\nimport { CHANGE_FREQ, CHUNK, OUT_DIR } from '../const.js';\nimport type { ChangeFreq, Options, OptionsSvelteSitemap, PagesJson } from './../dto/index.js';\nimport {\n cliColors,\n errorMsgFolder,\n errorMsgHtmlFiles,\n errorMsgWrite,\n successMsg\n} from './vars.helper.js';\n\nconst version = pkg.version;\n\nconst getUrl = (url: string, domain: string, options: Options) => {\n let slash: '' | '/' = getSlash(domain);\n\n let trimmed = url\n .split((options?.outDir ?? OUT_DIR) + '/')\n .pop()\n .replace('index.html', '');\n\n trimmed = removeHtml(trimmed);\n\n // Add all traling slashes\n if (options?.trailingSlashes) {\n trimmed = trimmed.length && !trimmed.endsWith('/') ? trimmed + '/' : trimmed;\n } else {\n trimmed = trimmed.endsWith('/') ? trimmed.slice(0, -1) : trimmed;\n slash = trimmed ? slash : '';\n }\n\n // URI-encode each path segment to handle special characters (e.g. spaces → %20).\n // Decode first to avoid double-encoding already percent-encoded segments.\n trimmed = trimmed\n .split('/')\n .map((segment) => {\n try {\n return encodeURIComponent(decodeURIComponent(segment));\n } catch {\n return encodeURIComponent(segment);\n }\n })\n .join('/');\n\n return `${domain}${slash}${trimmed}`;\n};\n\nexport const removeHtml = (fileName: string) => {\n if (fileName?.endsWith('.html')) {\n return fileName.slice(0, -5);\n }\n return fileName;\n};\n\nexport async function prepareData(domain: string, options?: Options): Promise<PagesJson[]> {\n const FOLDER = options?.outDir ?? OUT_DIR;\n\n const ignore = prepareIgnored(options?.ignore, options?.outDir);\n const changeFreq = prepareChangeFreq(options);\n const pages: string[] = await fg(`${FOLDER}/**/*.html`, { ignore });\n\n if (options.additional) pages.push(...options.additional);\n\n const results = pages.map((page) => {\n return {\n page: getUrl(page, domain, options),\n changeFreq: changeFreq,\n lastMod: options?.resetTime ? new Date().toISOString().split('T')[0] : ''\n };\n });\n\n detectErrors(\n {\n folder: !fs.existsSync(FOLDER),\n htmlFiles: !pages.length\n },\n FOLDER\n );\n\n await checkPrerenderRoutes(pages, FOLDER, options);\n\n return results;\n}\n\nexport const detectErrors = (\n { folder, htmlFiles }: { folder: boolean; htmlFiles: boolean },\n outDir: string = OUT_DIR\n) => {\n if (folder && htmlFiles) {\n console.error(cliColors.red, errorMsgFolder(outDir));\n } else if (htmlFiles) {\n // If no page exists, then the static adapter is probably not used\n console.error(cliColors.red, errorMsgHtmlFiles(outDir));\n }\n};\n\nexport const checkPrerenderRoutes = async (pages: string[], outDir: string, options?: Options) => {\n // Check if it's a SvelteKit build by checking for the '_app' directory in output folder\n const appDirExists = fs.existsSync(path.join(outDir, '_app'));\n\n if (appDirExists && pages.length > 0) {\n const hasOnlyRootOrFallback = pages.every((page) => {\n const relative = path.relative(outDir, page);\n return relative === 'index.html' || relative === 'fallback.html';\n });\n\n const hasNoAdditional = !options?.additional || options.additional.length === 0;\n\n if (hasOnlyRootOrFallback && hasNoAdditional) {\n console.warn(\n cliColors.yellow,\n ` ⚠️ Warning: Only the homepage or fallback page was found in '${outDir}/'.\\n` +\n ` If your SvelteKit site has multiple routes, make sure you enabled prerendering for them.\\n` +\n ` For SPA (Single Page Apps), you can add routes manually using the 'additional' option.`\n );\n }\n }\n};\n\nexport const writeSitemap = (items: PagesJson[], options: Options, domain: string): void => {\n const outDir = options?.outDir ?? OUT_DIR;\n\n if (items?.length <= CHUNK.maxSize) {\n createFile(items, options, outDir);\n } else {\n // If the number of pages is greater than the chunk size, then we split the sitemap into multiple files\n // and create an index file that links to all of them\n // https://support.google.com/webmasters/answer/183668?hl=en\n const numberOfChunks = Math.ceil(items.length / CHUNK.maxSize);\n\n console.log(\n cliColors.cyanAndBold,\n `> Oh, your site is huge! Writing sitemap in chunks of ${numberOfChunks} pages and its index sitemap.xml`\n );\n\n for (let i = 0; i < items.length; i += CHUNK.maxSize) {\n const chunk = items.slice(i, i + CHUNK.maxSize);\n createFile(chunk, options, outDir, i / CHUNK.maxSize + 1);\n }\n createIndexFile(numberOfChunks, outDir, options, domain);\n }\n};\n\nconst createFile = (\n items: PagesJson[],\n options: Options,\n outDir: string,\n chunkId?: number\n): void => {\n const sitemap = createXml('urlset');\n addAttribution(sitemap, options);\n\n for (const item of items) {\n const page = sitemap.ele('url');\n page.ele('loc').txt(item.page);\n if (item.changeFreq) {\n page.ele('changefreq').txt(item.changeFreq);\n }\n if (item.lastMod) {\n page.ele('lastmod').txt(item.lastMod);\n }\n }\n\n const xml = finishXml(sitemap);\n\n const fileName = chunkId ? `sitemap-${chunkId}.xml` : 'sitemap.xml';\n\n try {\n fs.writeFileSync(`${outDir}/${fileName}`, xml);\n console.log(cliColors.green, successMsg(outDir, fileName));\n } catch (e) {\n console.error(cliColors.red, errorMsgWrite(outDir, fileName), e);\n }\n};\n\nconst createIndexFile = (\n numberOfChunks: number,\n outDir: string,\n options: Options,\n domain: string\n): void => {\n const FILENAME = 'sitemap.xml';\n const slash = getSlash(domain);\n\n const sitemap = createXml('sitemapindex');\n addAttribution(sitemap, options);\n\n for (let i = 1; i <= numberOfChunks; i++) {\n sitemap.ele('sitemap').ele('loc').txt(`${domain}${slash}sitemap-${i}.xml`);\n }\n\n const xml = finishXml(sitemap);\n\n try {\n fs.writeFileSync(`${outDir}/${FILENAME}`, xml);\n console.log(cliColors.green, successMsg(outDir, FILENAME));\n } catch (e) {\n console.error(cliColors.red, errorMsgWrite(outDir, FILENAME), e);\n }\n};\n\nconst prepareIgnored = (\n ignored: string | string[],\n outDir: string = OUT_DIR\n): string[] | undefined => {\n let ignore: string[] | undefined;\n if (ignored) {\n ignore = Array.isArray(ignored) ? ignored : [ignored];\n ignore = ignore.map((ignoredPage) => `${outDir}/${ignoredPage}`);\n }\n return ignore;\n};\n\nconst prepareChangeFreq = (options: Options): ChangeFreq => {\n let result: ChangeFreq = null;\n\n if (options?.changeFreq) {\n if (CHANGE_FREQ.includes(options.changeFreq)) {\n result = options.changeFreq;\n } else {\n console.log(\n cliColors.red,\n ` × Option \\`--change-freq ${options.changeFreq}\\` is not a valid value. See docs: https://github.com/bartholomej/svelte-sitemap#options`\n );\n }\n }\n return result;\n};\n\nconst getSlash = (domain: string) => (domain.split('/').pop() ? '/' : '');\n\nconst createXml = (elementName: 'urlset' | 'sitemapindex'): XMLBuilder => {\n return create({ version: '1.0', encoding: 'UTF-8' }).ele(elementName, {\n xmlns: 'http://www.sitemaps.org/schemas/sitemap/0.9'\n });\n};\n\nconst finishXml = (sitemap: XMLBuilder): string => {\n return sitemap.end({ prettyPrint: true });\n};\n\nconst addAttribution = (sitemap: XMLBuilder, options: Options): void => {\n if (options?.attribution !== false) {\n sitemap.com(\n ` This file was automatically generated by https://github.com/bartholomej/svelte-sitemap v${version} `\n );\n }\n};\n"],"mappings":";;;;;;;;AAgBA,MAAM,UAAUA;AAEhB,MAAM,UAAU,KAAa,QAAgB,YAAqB;CAChE,IAAI,QAAkB,SAAS,MAAM;CAErC,IAAI,UAAU,IACX,OAAO,SAAS,UAAA,WAAqB,GAAG,CAAC,CACzC,IAAI,CAAC,CACL,QAAQ,cAAc,EAAE;CAE3B,UAAU,WAAW,OAAO;CAG5B,IAAI,SAAS,iBACX,UAAU,QAAQ,UAAU,CAAC,QAAQ,SAAS,GAAG,IAAI,UAAU,MAAM;MAChE;EACL,UAAU,QAAQ,SAAS,GAAG,IAAI,QAAQ,MAAM,GAAG,EAAE,IAAI;EACzD,QAAQ,UAAU,QAAQ;CAC5B;CAIA,UAAU,QACP,MAAM,GAAG,CAAC,CACV,KAAK,YAAY;EAChB,IAAI;GACF,OAAO,mBAAmB,mBAAmB,OAAO,CAAC;EACvD,QAAQ;GACN,OAAO,mBAAmB,OAAO;EACnC;CACF,CAAC,CAAC,CACD,KAAK,GAAG;CAEX,OAAO,GAAG,SAAS,QAAQ;AAC7B;AAEA,MAAa,cAAc,aAAqB;CAC9C,IAAI,UAAU,SAAS,OAAO,GAC5B,OAAO,SAAS,MAAM,GAAG,EAAE;CAE7B,OAAO;AACT;AAEA,eAAsB,YAAY,QAAgB,SAAyC;CACzF,MAAM,SAAS,SAAS,UAAA;CAExB,MAAM,SAAS,eAAe,SAAS,QAAQ,SAAS,MAAM;CAC9D,MAAM,aAAa,kBAAkB,OAAO;CAC5C,MAAM,QAAkB,MAAM,GAAG,GAAG,OA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