Skip to content

Commit

Permalink
Merge branch 'feat/additional'
Browse files Browse the repository at this point in the history
  • Loading branch information
bartholomej committed Jan 5, 2025
2 parents aa5402c + ae1ef7a commit 7c28673
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 5 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ And now you can run your script like this: `node my-script.js`
| -------------------------- | ------------------------------------------------------------------------------------------------------------------------------- | ------- | -------------------------------------- |
| `--domain`, `-d` | Use your domain **[required]** | - | `-d https://mydomain.com` |
| `--out-dir`, `-o` | Set custom build folder | `build` | `-o dist` |
| `--additional`, `-a` | Additional pages outside of SvelteKit | - | `-a my-page -a my-second-page` |
| `--ignore`, `-i` | Ignore files or folders | [] | `-i '**/admin/**' -i 'my-secret-page'` |
| `--trailing-slashes`, `-t` | Add trailing slashes | false | `--trailing-slashes` |
| `--reset-time`, `-r` | Set lastModified time to now | false | `-r` |
Expand Down Expand Up @@ -170,7 +171,7 @@ yarn demo

## 📝 License

Copyright © 2023 [Lukas Bartak](http://bartweb.cz)
Copyright © 2024 [Lukas Bartak](http://bartweb.cz)

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

Expand Down
11 changes: 8 additions & 3 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const REPO_URL = 'https://github.com/bartholomej/svelte-sitemap';
let stop = false;

const args = minimist(process.argv.slice(2), {
string: ['domain', 'out-dir', 'ignore', 'change-freq'],
string: ['domain', 'out-dir', 'ignore', 'change-freq', 'additional'],
boolean: ['attribution', 'reset-time', 'trailing-slashes', 'debug', 'version'],
default: { attribution: true, 'trailing-slashes': false, default: false },
alias: {
Expand All @@ -29,7 +29,9 @@ const args = minimist(process.argv.slice(2), {
i: 'ignore',
I: 'ignore',
t: 'trailing-slashes',
T: 'trailing-slashes'
T: 'trailing-slashes',
a: 'additional',
A: 'additional'
},
unknown: (err: string) => {
console.log('⚠ Those arguments are not supported:', err);
Expand All @@ -50,6 +52,7 @@ if (args.help || args.version === '' || args.version === true) {
log(' -d, --domain Use your domain (eg. https://example.com)');
log(' -o, --out-dir Custom output dir');
log(' -i, --ignore Exclude some pages or folders');
log(' -a, --additional Additional pages outside of SvelteKit (e.g. /, /contact)');
log(' -t, --trailing-slashes Do you like trailing slashes?');
log(' -r, --reset-time Set modified time to now');
log(' -c, --change-freq Set change frequency `weekly` | `daily` | …');
Expand All @@ -72,6 +75,7 @@ if (args.help || args.version === '' || args.version === true) {
} else {
const domain: string = args.domain ? args.domain : undefined;
const debug: boolean = args.debug === '' || args.debug === true ? true : false;
const additional = Array.isArray(args['additional']) ? args['additional'] : args.additional ? [args.additional] : [];
const resetTime: boolean =
args['reset-time'] === '' || args['reset-time'] === true ? true : false;
const trailingSlashes: boolean =
Expand All @@ -88,7 +92,8 @@ if (args.help || args.version === '' || args.version === true) {
outDir,
attribution,
ignore,
trailingSlashes
trailingSlashes,
additional,
};

createSitemap(domain, options);
Expand Down
2 changes: 2 additions & 0 deletions src/helpers/global.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ export async function prepareData(domain: string, options?: Options): Promise<Pa
const changeFreq = prepareChangeFreq(options);
const pages: string[] = await fg(`${FOLDER}/**/*.html`, { ignore });

if (options.additional) pages.push(...options.additional);

const results = pages.map((page) => {
return {
page: getUrl(page, domain, options),
Expand Down
3 changes: 2 additions & 1 deletion src/interfaces/global.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface Options {
attribution?: boolean;
ignore?: string | string[];
trailingSlashes?: boolean;
additional?: string[];
}

export interface PagesJson {
Expand All @@ -32,4 +33,4 @@ export const changeFreq = [
/**
* Specs: https://www.sitemaps.org/protocol.html
*/
export type ChangeFreq = typeof changeFreq[number];
export type ChangeFreq = (typeof changeFreq)[number];
52 changes: 52 additions & 0 deletions tests/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,58 @@ describe('Create JSON model', () => {
);
});

test('Sitemap with additional pages', async () => {
const json = await prepareData('https://example.com', {
...optionsTest,
additional: ['my-page', 'my-page2']
});

expect(sortbyPage(json)).toMatchObject(
sortbyPage([
{
page: 'https://example.com/flat',
lastMod: ''
},
{
page: 'https://example.com',
lastMod: ''
},
{
page: 'https://example.com/page1',
lastMod: ''
},
{
page: 'https://example.com/page1/flat1',
lastMod: ''
},
{
page: 'https://example.com/page2',
lastMod: ''
},
{
page: 'https://example.com/page1/subpage1',
lastMod: ''
},
{
page: 'https://example.com/page2/subpage2',
lastMod: ''
},
{
page: 'https://example.com/page2/subpage2/subsubpage2',
lastMod: ''
},
{
lastMod: '',
page: 'https://example.com/my-page'
},
{
lastMod: '',
page: 'https://example.com/my-page2'
}
])
);
});

test('Sitemap with reset time', async () => {
const json = await prepareData('https://example.com', { ...optionsTest, resetTime: true });

Expand Down

0 comments on commit 7c28673

Please sign in to comment.