Skip to content

Commit 7c28673

Browse files
committed
Merge branch 'feat/additional'
2 parents aa5402c + ae1ef7a commit 7c28673

File tree

5 files changed

+66
-5
lines changed

5 files changed

+66
-5
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ And now you can run your script like this: `node my-script.js`
5858
| -------------------------- | ------------------------------------------------------------------------------------------------------------------------------- | ------- | -------------------------------------- |
5959
| `--domain`, `-d` | Use your domain **[required]** | - | `-d https://mydomain.com` |
6060
| `--out-dir`, `-o` | Set custom build folder | `build` | `-o dist` |
61+
| `--additional`, `-a` | Additional pages outside of SvelteKit | - | `-a my-page -a my-second-page` |
6162
| `--ignore`, `-i` | Ignore files or folders | [] | `-i '**/admin/**' -i 'my-secret-page'` |
6263
| `--trailing-slashes`, `-t` | Add trailing slashes | false | `--trailing-slashes` |
6364
| `--reset-time`, `-r` | Set lastModified time to now | false | `-r` |
@@ -170,7 +171,7 @@ yarn demo
170171

171172
## 📝 License
172173

173-
Copyright © 2023 [Lukas Bartak](http://bartweb.cz)
174+
Copyright © 2024 [Lukas Bartak](http://bartweb.cz)
174175

175176
Proudly powered by nature 🗻, wind 💨, tea 🍵 and beer 🍺 ;)
176177

index.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const REPO_URL = 'https://github.com/bartholomej/svelte-sitemap';
1010
let stop = false;
1111

1212
const args = minimist(process.argv.slice(2), {
13-
string: ['domain', 'out-dir', 'ignore', 'change-freq'],
13+
string: ['domain', 'out-dir', 'ignore', 'change-freq', 'additional'],
1414
boolean: ['attribution', 'reset-time', 'trailing-slashes', 'debug', 'version'],
1515
default: { attribution: true, 'trailing-slashes': false, default: false },
1616
alias: {
@@ -29,7 +29,9 @@ const args = minimist(process.argv.slice(2), {
2929
i: 'ignore',
3030
I: 'ignore',
3131
t: 'trailing-slashes',
32-
T: 'trailing-slashes'
32+
T: 'trailing-slashes',
33+
a: 'additional',
34+
A: 'additional'
3335
},
3436
unknown: (err: string) => {
3537
console.log('⚠ Those arguments are not supported:', err);
@@ -50,6 +52,7 @@ if (args.help || args.version === '' || args.version === true) {
5052
log(' -d, --domain Use your domain (eg. https://example.com)');
5153
log(' -o, --out-dir Custom output dir');
5254
log(' -i, --ignore Exclude some pages or folders');
55+
log(' -a, --additional Additional pages outside of SvelteKit (e.g. /, /contact)');
5356
log(' -t, --trailing-slashes Do you like trailing slashes?');
5457
log(' -r, --reset-time Set modified time to now');
5558
log(' -c, --change-freq Set change frequency `weekly` | `daily` | …');
@@ -72,6 +75,7 @@ if (args.help || args.version === '' || args.version === true) {
7275
} else {
7376
const domain: string = args.domain ? args.domain : undefined;
7477
const debug: boolean = args.debug === '' || args.debug === true ? true : false;
78+
const additional = Array.isArray(args['additional']) ? args['additional'] : args.additional ? [args.additional] : [];
7579
const resetTime: boolean =
7680
args['reset-time'] === '' || args['reset-time'] === true ? true : false;
7781
const trailingSlashes: boolean =
@@ -88,7 +92,8 @@ if (args.help || args.version === '' || args.version === true) {
8892
outDir,
8993
attribution,
9094
ignore,
91-
trailingSlashes
95+
trailingSlashes,
96+
additional,
9297
};
9398

9499
createSitemap(domain, options);

src/helpers/global.helper.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ export async function prepareData(domain: string, options?: Options): Promise<Pa
4949
const changeFreq = prepareChangeFreq(options);
5050
const pages: string[] = await fg(`${FOLDER}/**/*.html`, { ignore });
5151

52+
if (options.additional) pages.push(...options.additional);
53+
5254
const results = pages.map((page) => {
5355
return {
5456
page: getUrl(page, domain, options),

src/interfaces/global.interface.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export interface Options {
1111
attribution?: boolean;
1212
ignore?: string | string[];
1313
trailingSlashes?: boolean;
14+
additional?: string[];
1415
}
1516

1617
export interface PagesJson {
@@ -32,4 +33,4 @@ export const changeFreq = [
3233
/**
3334
* Specs: https://www.sitemaps.org/protocol.html
3435
*/
35-
export type ChangeFreq = typeof changeFreq[number];
36+
export type ChangeFreq = (typeof changeFreq)[number];

tests/main.test.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,58 @@ describe('Create JSON model', () => {
104104
);
105105
});
106106

107+
test('Sitemap with additional pages', async () => {
108+
const json = await prepareData('https://example.com', {
109+
...optionsTest,
110+
additional: ['my-page', 'my-page2']
111+
});
112+
113+
expect(sortbyPage(json)).toMatchObject(
114+
sortbyPage([
115+
{
116+
page: 'https://example.com/flat',
117+
lastMod: ''
118+
},
119+
{
120+
page: 'https://example.com',
121+
lastMod: ''
122+
},
123+
{
124+
page: 'https://example.com/page1',
125+
lastMod: ''
126+
},
127+
{
128+
page: 'https://example.com/page1/flat1',
129+
lastMod: ''
130+
},
131+
{
132+
page: 'https://example.com/page2',
133+
lastMod: ''
134+
},
135+
{
136+
page: 'https://example.com/page1/subpage1',
137+
lastMod: ''
138+
},
139+
{
140+
page: 'https://example.com/page2/subpage2',
141+
lastMod: ''
142+
},
143+
{
144+
page: 'https://example.com/page2/subpage2/subsubpage2',
145+
lastMod: ''
146+
},
147+
{
148+
lastMod: '',
149+
page: 'https://example.com/my-page'
150+
},
151+
{
152+
lastMod: '',
153+
page: 'https://example.com/my-page2'
154+
}
155+
])
156+
);
157+
});
158+
107159
test('Sitemap with reset time', async () => {
108160
const json = await prepareData('https://example.com', { ...optionsTest, resetTime: true });
109161

0 commit comments

Comments
 (0)