cook-ssg 2.0.0

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.
Files changed (37) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +445 -0
  3. package/bin/cook.js +66 -0
  4. package/package.json +75 -0
  5. package/patches/jsdom-fetch-fix.js +52 -0
  6. package/scripts/build.js +212 -0
  7. package/scripts/dev.js +119 -0
  8. package/scripts/plugins/apply-template.js +226 -0
  9. package/scripts/plugins/auto-components.js +215 -0
  10. package/scripts/plugins/bundle.js +338 -0
  11. package/scripts/plugins/copy-src.js +89 -0
  12. package/scripts/plugins/create-dir-from-file.js +144 -0
  13. package/scripts/plugins/create-dist.js +55 -0
  14. package/scripts/plugins/custom-plugins.js +118 -0
  15. package/scripts/plugins/declarative-shadow-dom.js +164 -0
  16. package/scripts/plugins/escape-code-blocks.js +54 -0
  17. package/scripts/plugins/generate-formats.js +361 -0
  18. package/scripts/plugins/generate-fragments.js +126 -0
  19. package/scripts/plugins/generate-sitemap-xml.js +216 -0
  20. package/scripts/plugins/minify-src.js +253 -0
  21. package/scripts/plugins/optimize-images.js +389 -0
  22. package/scripts/plugins/process-html-frontmatter.js +75 -0
  23. package/scripts/plugins/process-markdown.js +384 -0
  24. package/scripts/plugins/repeat-collection.js +296 -0
  25. package/scripts/plugins/replace-components.js +496 -0
  26. package/scripts/plugins/replace-external-link-protocol.js +171 -0
  27. package/scripts/plugins/replace-include.js +273 -0
  28. package/scripts/plugins/replace-inline.js +217 -0
  29. package/scripts/plugins/replace-template-strings.js +121 -0
  30. package/scripts/plugins/set-active-links.js +175 -0
  31. package/scripts/utils/config/config.js +277 -0
  32. package/scripts/utils/get-src/get-src.js +241 -0
  33. package/scripts/utils/logger/logger.js +47 -0
  34. package/scripts/utils/performance/performance.js +35 -0
  35. package/scripts/utils/spinner/spinner.js +112 -0
  36. package/scripts/utils/timer/timer.js +40 -0
  37. package/scripts/utils/util/util.js +719 -0
@@ -0,0 +1,361 @@
1
+ /**
2
+ * @file generate-formats.js
3
+ * @description Generate multi-format output for each page.
4
+ *
5
+ * Produces:
6
+ * - Markdown copies in dist/md/{path}/index.md (for markdown-sourced pages)
7
+ * - JSON metadata in dist/api/{path}.json (for all pages)
8
+ * - Atom feed at dist/feed.xml (from a configurable collection)
9
+ * - llms.txt at dist/llms.txt (index of all content with markdown URLs)
10
+ */
11
+
12
+ // IMPORTS
13
+ // -----------------------------
14
+ import chalk from 'chalk';
15
+ import { promises as fs } from 'node:fs';
16
+ import { existsSync } from 'node:fs';
17
+ import path from 'node:path';
18
+ import Util from '../utils/util/util.js';
19
+ import Logger from '../utils/logger/logger.js';
20
+
21
+ // Config
22
+ import { distPath, srcPath, formats as formatsConfig } from '../utils/config/config.js';
23
+
24
+
25
+ // DEFINE
26
+ // -----------------------------
27
+ class GenerateFormats {
28
+ constructor({ store, data }) {
29
+ this.store = store;
30
+ this.data = data || {};
31
+ this.config = {
32
+ markdown: true,
33
+ json: true,
34
+ feed: null,
35
+ llmsTxt: true,
36
+ ...formatsConfig,
37
+ };
38
+
39
+ this.stats = { markdown: 0, json: 0, feed: false, llmsTxt: false };
40
+
41
+ // Init terminal logging
42
+ Util.initLogging.call(this);
43
+ }
44
+
45
+ // INIT
46
+ // -----------------------------
47
+ async init() {
48
+ // Early Exit: No formats configured
49
+ if (!this.config.markdown && !this.config.json && !this.config.feed && !this.config.llmsTxt) return;
50
+
51
+ // ADD TERMINAL SECTION HEADING
52
+ Logger.persist.header(`\nGenerate Formats`);
53
+
54
+ // START LOGGING
55
+ this.startLog();
56
+
57
+ try {
58
+ // Get all index.html files in dist (the actual pages)
59
+ const htmlFiles = Util.getPaths(distPath, distPath, []);
60
+ const indexFiles = htmlFiles
61
+ .filter(f => f.endsWith('/index.html') || f === `${distPath}/index.html`)
62
+ .filter(f => !f.includes('/404/'));
63
+
64
+ // Process each page
65
+ for (const filePath of indexFiles) {
66
+ await this.processPage(filePath);
67
+ }
68
+
69
+ // Generate feed
70
+ if (this.config.feed) {
71
+ await this.generateFeed();
72
+ }
73
+
74
+ // Generate llms.txt
75
+ if (this.config.llmsTxt) {
76
+ await this.generateLlmsTxt(indexFiles);
77
+ }
78
+ } catch (err) {
79
+ Util.customError(err, 'generate-formats.js');
80
+ }
81
+
82
+ // END LOGGING
83
+ this.endLog();
84
+ }
85
+
86
+ /**
87
+ * Process a single page: generate markdown copy and JSON metadata
88
+ * @param {string} filePath - Path to the index.html file in dist
89
+ */
90
+ async processPage(filePath) {
91
+ // Get the URL path from the dist file path
92
+ // e.g., dist/blog/my-post/index.html → /blog/my-post/
93
+ const relativePath = filePath.replace(distPath, '');
94
+ const urlPath = relativePath.replace(/\/index\.html$/, '/').replace(/^$/, '/');
95
+
96
+ // Try to find corresponding markdown source
97
+ const mdSourcePath = this.findMarkdownSource(urlPath);
98
+
99
+ // Generate markdown copy
100
+ if (this.config.markdown && mdSourcePath) {
101
+ await this.generateMarkdownCopy(mdSourcePath, urlPath);
102
+ }
103
+
104
+ // Generate JSON metadata
105
+ if (this.config.json) {
106
+ await this.generateJsonMeta(filePath, urlPath, mdSourcePath);
107
+ }
108
+ }
109
+
110
+ /**
111
+ * Find the markdown source file for a given URL path
112
+ * @param {string} urlPath - URL path (e.g., /blog/my-post/)
113
+ * @returns {string|null} Path to .md source or null
114
+ */
115
+ findMarkdownSource(urlPath) {
116
+ // Convert URL path to possible markdown source paths
117
+ // /blog/my-post/ → src/blog/my-post.md or src/blog/my-post/index.md
118
+ const trimmedPath = urlPath.replace(/^\//, '').replace(/\/$/, '');
119
+
120
+ const candidates = [
121
+ path.resolve(`${srcPath}/${trimmedPath}.md`),
122
+ path.resolve(`${srcPath}/${trimmedPath}/index.md`),
123
+ ];
124
+
125
+ for (const candidate of candidates) {
126
+ if (existsSync(candidate)) return candidate;
127
+ }
128
+
129
+ return null;
130
+ }
131
+
132
+ /**
133
+ * Copy markdown source to dist/md/{path}/index.md
134
+ * @param {string} mdSourcePath - Path to the source .md file
135
+ * @param {string} urlPath - URL path
136
+ */
137
+ async generateMarkdownCopy(mdSourcePath, urlPath) {
138
+ try {
139
+ const content = await fs.readFile(mdSourcePath, 'utf-8');
140
+ const trimmedPath = urlPath.replace(/^\//, '').replace(/\/$/, '');
141
+ const outputPath = trimmedPath
142
+ ? path.resolve(`${distPath}/md/${trimmedPath}/index.md`)
143
+ : path.resolve(`${distPath}/md/index.md`);
144
+
145
+ await fs.mkdir(path.dirname(outputPath), { recursive: true });
146
+ await fs.writeFile(outputPath, content, 'utf-8');
147
+ this.stats.markdown++;
148
+ } catch (err) {
149
+ Util.customError(err, `generate-formats.js: markdown for ${urlPath}`);
150
+ }
151
+ }
152
+
153
+ /**
154
+ * Generate JSON metadata for a page at dist/api/{path}.json
155
+ * @param {string} filePath - Path to the HTML file
156
+ * @param {string} urlPath - URL path
157
+ * @param {string|null} mdSourcePath - Path to markdown source if exists
158
+ */
159
+ async generateJsonMeta(filePath, urlPath, mdSourcePath) {
160
+ try {
161
+ const src = await fs.readFile(filePath, 'utf-8');
162
+ const dom = Util.jsdom.dom({ src });
163
+ const document = dom.window.document;
164
+
165
+ // Extract metadata from the HTML
166
+ const title = document.querySelector('title')?.textContent || '';
167
+ const description = document.querySelector('meta[name="description"]')?.getAttribute('content') || '';
168
+ const h1 = document.querySelector('h1')?.textContent || '';
169
+
170
+ const meta = {
171
+ url: urlPath,
172
+ title: h1 || title,
173
+ description,
174
+ hasMarkdown: !!mdSourcePath,
175
+ };
176
+
177
+ if (mdSourcePath) {
178
+ const trimmedPath = urlPath.replace(/^\//, '').replace(/\/$/, '');
179
+ meta.markdownUrl = trimmedPath ? `/md/${trimmedPath}/index.md` : `/md/index.md`;
180
+ }
181
+
182
+ meta.fragmentUrl = urlPath + '_fragment.html';
183
+
184
+ // Add collection item data if available
185
+ const collectionItem = this.findCollectionItem(urlPath);
186
+ if (collectionItem) {
187
+ if (collectionItem.date) meta.date = collectionItem.date;
188
+ if (collectionItem.author) meta.author = collectionItem.author;
189
+ if (collectionItem.tags) meta.tags = collectionItem.tags;
190
+ }
191
+
192
+ // Write JSON
193
+ const trimmedPath = urlPath.replace(/^\//, '').replace(/\/$/, '') || 'index';
194
+ const outputPath = path.resolve(`${distPath}/api/${trimmedPath}.json`);
195
+
196
+ await fs.mkdir(path.dirname(outputPath), { recursive: true });
197
+ await fs.writeFile(outputPath, JSON.stringify(meta, null, 2), 'utf-8');
198
+ this.stats.json++;
199
+ } catch (err) {
200
+ Util.customError(err, `generate-formats.js: json for ${urlPath}`);
201
+ }
202
+ }
203
+
204
+ /**
205
+ * Find a collection item matching a URL path
206
+ * @param {string} urlPath - URL path
207
+ * @returns {Object|null} Collection item or null
208
+ */
209
+ findCollectionItem(urlPath) {
210
+ const collections = this.store.collections || this.data.collections;
211
+ if (!collections) return null;
212
+
213
+ for (const items of Object.values(collections)) {
214
+ if (!Array.isArray(items)) continue;
215
+ const match = items.find(item => item.url === urlPath);
216
+ if (match) return match;
217
+ }
218
+ return null;
219
+ }
220
+
221
+ /**
222
+ * Generate an Atom feed from a configured collection
223
+ */
224
+ async generateFeed() {
225
+ const feedConfig = this.config.feed;
226
+ if (!feedConfig || !feedConfig.collection) return;
227
+
228
+ const collections = this.store.collections || this.data.collections;
229
+ if (!collections) return;
230
+
231
+ let items = collections[feedConfig.collection];
232
+ if (!items || !Array.isArray(items)) return;
233
+
234
+ // Apply limit
235
+ if (feedConfig.limit) {
236
+ items = items.slice(0, feedConfig.limit);
237
+ }
238
+
239
+ const siteUrl = this.data.siteUrl || feedConfig.siteUrl || 'https://example.com';
240
+ const feedTitle = feedConfig.title || this.data.siteTitle || 'Feed';
241
+ const now = new Date().toISOString();
242
+
243
+ let xml = `<?xml version="1.0" encoding="utf-8"?>
244
+ <feed xmlns="http://www.w3.org/2005/Atom">
245
+ <title>${this.escapeXml(feedTitle)}</title>
246
+ <link href="${siteUrl}" />
247
+ <link rel="self" href="${siteUrl}/feed.xml" />
248
+ <updated>${now}</updated>
249
+ <id>${siteUrl}/</id>
250
+ `;
251
+
252
+ for (const item of items) {
253
+ const itemUrl = `${siteUrl}${item.url}`;
254
+ const date = item._dateSort ? item._dateSort.toISOString() : (item.date ? new Date(item.date).toISOString() : now);
255
+
256
+ xml += ` <entry>
257
+ <title>${this.escapeXml(item.title || '')}</title>
258
+ <link href="${itemUrl}" />
259
+ <id>${itemUrl}</id>
260
+ <updated>${date}</updated>
261
+ <summary>${this.escapeXml(item.description || '')}</summary>
262
+ </entry>
263
+ `;
264
+ }
265
+
266
+ xml += `</feed>
267
+ `;
268
+
269
+ await fs.writeFile(path.resolve(`${distPath}/feed.xml`), xml, 'utf-8');
270
+ this.stats.feed = true;
271
+ }
272
+
273
+ /**
274
+ * Generate llms.txt listing all content with markdown URLs
275
+ * @param {string[]} indexFiles - All index.html file paths
276
+ */
277
+ async generateLlmsTxt(indexFiles) {
278
+ const siteTitle = this.data.siteTitle || 'Site';
279
+ const siteDescription = this.data.siteDescription || '';
280
+ const siteUrl = this.data.siteUrl || '';
281
+
282
+ let content = `# ${siteTitle}\n\n`;
283
+ if (siteDescription) content += `> ${siteDescription}\n\n`;
284
+
285
+ // List pages that have markdown sources
286
+ const mdPages = [];
287
+ for (const filePath of indexFiles) {
288
+ const relativePath = filePath.replace(distPath, '');
289
+ const urlPath = relativePath.replace(/\/index\.html$/, '/').replace(/^$/, '/');
290
+ const mdSource = this.findMarkdownSource(urlPath);
291
+ if (mdSource) {
292
+ const trimmedPath = urlPath.replace(/^\//, '').replace(/\/$/, '');
293
+ // Find title from collection data or use path
294
+ const collectionItem = this.findCollectionItem(urlPath);
295
+ const title = collectionItem?.title || trimmedPath || 'index';
296
+ const mdUrl = trimmedPath ? `/md/${trimmedPath}/index.md` : `/md/index.md`;
297
+ mdPages.push({ title, url: urlPath, mdUrl });
298
+ }
299
+ }
300
+
301
+ if (mdPages.length) {
302
+ content += `## Content\n\n`;
303
+ for (const page of mdPages) {
304
+ const fullMdUrl = siteUrl ? `${siteUrl}${page.mdUrl}` : page.mdUrl;
305
+ content += `- [${page.title}](${fullMdUrl})\n`;
306
+ }
307
+ }
308
+
309
+ await fs.writeFile(path.resolve(`${distPath}/llms.txt`), content, 'utf-8');
310
+ this.stats.llmsTxt = true;
311
+ }
312
+
313
+ /**
314
+ * Escape special XML characters
315
+ * @param {string} str - Input string
316
+ * @returns {string} Escaped string
317
+ */
318
+ escapeXml(str) {
319
+ return str
320
+ .replace(/&/g, '&amp;')
321
+ .replace(/</g, '&lt;')
322
+ .replace(/>/g, '&gt;')
323
+ .replace(/"/g, '&quot;')
324
+ .replace(/'/g, '&apos;');
325
+ }
326
+
327
+
328
+ // LOGGING
329
+ // -----------------------------
330
+
331
+ startLog() {
332
+ this.loading.start(chalk.magenta('Generating formats'));
333
+ this.timer.start();
334
+ }
335
+
336
+ endLog() {
337
+ const parts = [];
338
+ if (this.stats.markdown) parts.push(`${this.stats.markdown} markdown`);
339
+ if (this.stats.json) parts.push(`${this.stats.json} json`);
340
+ if (this.stats.feed) parts.push('feed.xml');
341
+ if (this.stats.llmsTxt) parts.push('llms.txt');
342
+
343
+ if (parts.length) {
344
+ this.loading.stop(`Generated ${chalk.magenta(parts.join(', '))} ${this.timer.end()}`);
345
+ } else {
346
+ this.loading.kill();
347
+ }
348
+ }
349
+
350
+
351
+ // EXPORT WRAPPER
352
+ // -----------------------------
353
+ static async export(opts) {
354
+ return new GenerateFormats(opts).init();
355
+ }
356
+ }
357
+
358
+
359
+ // EXPORT
360
+ // -----------------------------
361
+ export default GenerateFormats.export;
@@ -0,0 +1,126 @@
1
+ /**
2
+ * @file generate-fragments.js
3
+ * @description Generate content-only HTML fragments for each page.
4
+ *
5
+ * For every index.html in dist, extracts the innerHTML of a configurable
6
+ * selector (default: "main") and writes it as a sibling _fragment.html.
7
+ * These fragments enable html-star style SPA navigation (fetch + swap).
8
+ */
9
+
10
+ // IMPORTS
11
+ // -----------------------------
12
+ import chalk from 'chalk';
13
+ import fs from 'fs-extra';
14
+ import path from 'node:path';
15
+ import Util from '../utils/util/util.js';
16
+ import Logger from '../utils/logger/logger.js';
17
+
18
+ // Config
19
+ import { distPath, fragments as fragmentsConfig } from '../utils/config/config.js';
20
+
21
+
22
+ // DEFINE
23
+ // -----------------------------
24
+ class GenerateFragments {
25
+ constructor() {
26
+ this.total = 0;
27
+
28
+ // Merge defaults with user config
29
+ this.config = {
30
+ enabled: true,
31
+ selector: 'main',
32
+ filename: '_fragment.html',
33
+ ...fragmentsConfig,
34
+ };
35
+
36
+ // Init terminal logging
37
+ Util.initLogging.call(this);
38
+ }
39
+
40
+ // INIT
41
+ // -----------------------------
42
+ async init() {
43
+ // Early Exit: Disabled
44
+ if (!this.config.enabled) return;
45
+
46
+ // ADD TERMINAL SECTION HEADING
47
+ Logger.persist.header(`\nGenerate Fragments`);
48
+
49
+ // START LOGGING
50
+ this.startLog();
51
+
52
+ try {
53
+ // Get all index.html files in dist
54
+ const htmlFiles = Util.getPaths(distPath, distPath, []);
55
+ const indexFiles = htmlFiles.filter(f => f.endsWith('/index.html') || f === `${distPath}/index.html`);
56
+
57
+ for (const filePath of indexFiles) {
58
+ await this.generateFragment(filePath);
59
+ }
60
+ } catch (err) {
61
+ Util.customError(err, 'generate-fragments.js');
62
+ }
63
+
64
+ // END LOGGING
65
+ this.endLog();
66
+ }
67
+
68
+ /**
69
+ * Generate a fragment file for a single HTML page
70
+ * @param {string} filePath - Path to the index.html file
71
+ */
72
+ async generateFragment(filePath) {
73
+ try {
74
+ const src = await fs.readFile(filePath, 'utf-8');
75
+
76
+ // Parse with JSDOM
77
+ const dom = Util.jsdom.dom({ src });
78
+ const document = dom.window.document;
79
+
80
+ // Find the target element
81
+ const target = document.querySelector(this.config.selector);
82
+ if (!target) return;
83
+
84
+ // Get the inner HTML of the target element
85
+ const fragmentContent = target.innerHTML;
86
+
87
+ // Write fragment file alongside index.html
88
+ const dir = path.dirname(filePath);
89
+ const fragmentPath = path.join(dir, this.config.filename);
90
+ await fs.writeFile(fragmentPath, fragmentContent, 'utf-8');
91
+
92
+ this.total++;
93
+ } catch (err) {
94
+ Util.customError(err, `generate-fragments.js: ${filePath}`);
95
+ }
96
+ }
97
+
98
+
99
+ // LOGGING
100
+ // -----------------------------
101
+
102
+ startLog() {
103
+ this.loading.start(chalk.magenta('Generating fragments'));
104
+ this.timer.start();
105
+ }
106
+
107
+ endLog() {
108
+ if (this.total > 0) {
109
+ this.loading.stop(`Generated ${chalk.magenta(this.total)} fragments ${this.timer.end()}`);
110
+ } else {
111
+ this.loading.kill();
112
+ }
113
+ }
114
+
115
+
116
+ // EXPORT WRAPPER
117
+ // -----------------------------
118
+ static async export(opts) {
119
+ return new GenerateFragments(opts).init();
120
+ }
121
+ }
122
+
123
+
124
+ // EXPORT
125
+ // -----------------------------
126
+ export default GenerateFragments.export;
@@ -0,0 +1,216 @@
1
+ // Generate `sitemap.xml` file
2
+
3
+ // IMPORT
4
+ // -----------------------------
5
+ import chalk from 'chalk';
6
+ import fs from 'fs-extra';
7
+ import Logger from '../utils/logger/logger.js';
8
+ import Util from '../utils/util/util.js';
9
+
10
+ // CONFIG
11
+ import { distPath, sitemap, srcPath } from '../utils/config/config.js';
12
+
13
+
14
+ // DEFINE
15
+ // -----------------------------
16
+ /**
17
+ * @description Generate `sitemap.xml` for search engines from the finished site tree.
18
+ * Is one of the last build processes since we want to get all new, dynamically-generated pages and their final path locations.
19
+ */
20
+ class GenerateSitemap {
21
+ constructor() {
22
+ // Store total # of file entries generated
23
+ this.total = 0;
24
+
25
+ // Init terminal logging
26
+ Util.initLogging.call(this);
27
+ }
28
+
29
+ // INIT
30
+ // -----------------------------
31
+ // Note: `process.env.DEV_CHANGED_PAGE` is defined in `browserSync.watch()` in dev.js
32
+ async init() {
33
+ // Early Exit: No user-defined site domain in `/config/main.js`
34
+ if (!sitemap || !sitemap.url || !sitemap.url.length) return;
35
+
36
+ // ADD TERMINAL SECTION HEADING
37
+ Logger.persist.header(`\nCreate /sitemap.xml`);
38
+
39
+ // START LOGGING
40
+ this.startLog();
41
+
42
+ try {
43
+ // Get directory and .html file paths from `/dist`
44
+ let files = await fs.readdir(distPath);
45
+ this.total = files.length;
46
+ // Format file paths for XML
47
+ files = this.formatFilesForXML(files);
48
+ // Filter out unwanted entry paths
49
+ files = this.excludeFiles(files);
50
+ // Build XML source
51
+ let xmlSrc = this.buildXML(files);
52
+ // Create `sitemap.xml` and write source to it
53
+ await fs.writeFile(`${distPath}/sitemap.xml`, xmlSrc);
54
+ }
55
+ catch (e) {
56
+ Util.customError(e, 'Generate sitemap.xml');
57
+ Logger.error(`Requires /${distPath} directory - please run the build process first`);
58
+ }
59
+
60
+ // END LOGGING
61
+ this.endLog();
62
+ }
63
+
64
+ // PROCESS METHODS
65
+ // -----------------------------
66
+
67
+ /**
68
+ * @description Build the .xml file source from the site files' paths
69
+ * @param {Array} files - The array of site file paths
70
+ * @returns {String}
71
+ */
72
+ buildXML(files) {
73
+ let date = this.formattedDate();
74
+ let xml = '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
75
+ // If `sitemap.url` has a trailing /, remove it to avoid potential double slashes (ex: https://domain.com//path/to/file)
76
+ const domain = sitemap.url[sitemap.url.length - 1] === '/' ? sitemap.url.slice(0, -1) : sitemap.url;
77
+ files.forEach(f => {
78
+ // Set homepage to higher priority
79
+ const priority = f === '' ? '1.0' : '0.9';
80
+ // Add entry
81
+ xml += `
82
+ <url>
83
+ <loc>${domain}${f}</loc>
84
+ <lastmod>${date}</lastmod>
85
+ <changefreq>monthly</changefreq>
86
+ <priority>${priority}</priority>
87
+ </url>
88
+ `.trim();
89
+ });
90
+ xml += '</urlset>';
91
+ // Return src
92
+ return xml;
93
+ }
94
+
95
+ /**
96
+ * @description Exclude a file path from the sitemap if it matches a
97
+ * default or user-provided regex exclusion.
98
+ * @param {Array} files - Array of file paths to be used for creating the sitemap
99
+ * @returns {Array}
100
+ * @private
101
+ */
102
+ excludeFiles(files) {
103
+ // Default path regexes: Any `/assets`, `/includes`, and root-level `404.html` files
104
+ const defaultExcludePaths = [/^\/assets/, /^\/includes/, /^\/404.html/];
105
+ // User defined regexes from `/config/main.js`
106
+ const userExcludePaths = sitemap && sitemap.excludePaths || [];
107
+ // Combine the default and user regexes
108
+ const excludePaths = [...defaultExcludePaths, ...userExcludePaths];
109
+ // Filter out unwanted paths
110
+ return files.filter(f => {
111
+ // Allow by default
112
+ let state = true;
113
+ // For each exclude path regex
114
+ excludePaths.forEach(path => {
115
+ // If the file entry path matches, change state to FALSE, so it gets filtered out
116
+ if (f.match(path)) state = false;
117
+ });
118
+ // Return current state to filter in or out the entry
119
+ return state;
120
+ });
121
+ }
122
+
123
+ /**
124
+ * @description Remove `/dist` path and drop any `index.html` parts from the file path
125
+ * @param {Array} files - The array of site file paths
126
+ * @returns {Array}
127
+ */
128
+ formatFilesForXML(files) {
129
+ // Allowed page extensions
130
+ const allowedExt = ['html'];
131
+ const excludedPaths = [];
132
+ // Get files in `/dist`
133
+ files = Util.getPaths(distPath, distPath, excludedPaths);
134
+ // Get only the allowed files by extension (.css, .html)
135
+ files = files.filter(fileName => Util.isExtension(fileName, allowedExt));
136
+ // Format for xml: Remove `/dist`
137
+ files = files.map(fileName => fileName.split(distPath)[1]);
138
+ // Format for xml: Remove `filename`
139
+ files = files.map(fileName => this.formatDirPath(fileName));
140
+ // Alphabetize files
141
+ files.sort();
142
+ // Return formatted files
143
+ return files;
144
+ }
145
+
146
+ // HELPER METHODS
147
+ // -----------------------------
148
+
149
+ /**
150
+ * @description Format current date in `yyyy-mm-dd` format
151
+ * @returns {String}
152
+ * @private
153
+ */
154
+ formattedDate() {
155
+ const dateRaw = new Date();
156
+ const formatter = new Intl.DateTimeFormat('en-us', {
157
+ day: '2-digit',
158
+ month: '2-digit',
159
+ year: 'numeric',
160
+ });
161
+ const dateStringRaw = formatter.formatToParts(dateRaw);
162
+ const dateStringParts = [,,];
163
+ dateStringRaw.forEach(({type, value}) => {
164
+ switch (type) {
165
+ case 'year': dateStringParts[0] = value; break;
166
+ case 'month': dateStringParts[1] = value; break;
167
+ case 'day': dateStringParts[2] = value; break;
168
+ }
169
+ });
170
+ return `${dateStringParts[0]}-${dateStringParts[1]}-${dateStringParts[2]}`;
171
+ }
172
+
173
+ /**
174
+ * @description Remove `index.html` from file path
175
+ * @param {String} filePath - The current file path string
176
+ * @returns {String}
177
+ * @private
178
+ */
179
+ formatDirPath(filePath) {
180
+ let filePathSplit = filePath.split('/');
181
+ const last = filePathSplit.pop();
182
+ if (last !== 'index.html') filePathSplit[filePathSplit.length] = last;
183
+ return filePathSplit.join('/');
184
+ }
185
+
186
+
187
+ // LOGGING
188
+ // -----------------------------
189
+
190
+ startLog() {
191
+ // Start Spinner
192
+ this.loading.start(`Building ${chalk.magenta('sitemap.xml')}`);
193
+ // Start timer
194
+ this.timer.start();
195
+ }
196
+
197
+ endLog() {
198
+ // Stop Spinner and Timer
199
+ if (this.total > 0) this.loading.stop(`Generated ${chalk.magenta(this.total)} site entries ${this.timer.end()}`);
200
+ // If no matches found, stop logger but don't show line in terminal
201
+ else this.loading.kill();
202
+ }
203
+
204
+
205
+ // EXPORT WRAPPER
206
+ // -----------------------------
207
+ // Export function wrapper instead of class for `build.js` simplicity
208
+ static async export(opts) {
209
+ return new GenerateSitemap(opts).init();
210
+ }
211
+ }
212
+
213
+
214
+ // EXPORT
215
+ // -----------------------------
216
+ export default GenerateSitemap.export;