@sphido/sitemap 1.0.9 → 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.
- package/LICENSE.md +1 -1
- package/lib/sitemap.js +33 -9
- package/package.json +5 -5
- package/readme.md +18 -18
package/LICENSE.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
MIT License
|
|
2
2
|
-----------
|
|
3
3
|
|
|
4
|
-
Copyright (c)
|
|
4
|
+
Copyright (c) 2022 Roman Ožana (https://ozana.cz/)
|
|
5
5
|
Permission is hereby granted, free of charge, to any person
|
|
6
6
|
obtaining a copy of this software and associated documentation
|
|
7
7
|
files (the "Software"), to deal in the Software without
|
package/lib/sitemap.js
CHANGED
|
@@ -1,12 +1,36 @@
|
|
|
1
|
+
import {createWriteStream, existsSync} from 'node:fs';
|
|
2
|
+
import {mkdir, unlink} from 'node:fs/promises';
|
|
3
|
+
import {dirname} from 'node:path';
|
|
4
|
+
|
|
1
5
|
/**
|
|
2
|
-
* Generate
|
|
3
|
-
* @param {
|
|
4
|
-
* @
|
|
5
|
-
* @returns {string}
|
|
6
|
+
* Generate XML sitemap
|
|
7
|
+
* @param {string} file
|
|
8
|
+
* @returns {Object<{addUrl: add, end: end}>}
|
|
6
9
|
*/
|
|
7
|
-
export function
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
export async function createSitemap(file = 'public/sitemap.xml') {
|
|
11
|
+
// Create directory
|
|
12
|
+
if (!existsSync(dirname(file))) {
|
|
13
|
+
await mkdir(dirname(file), {recursive: true});
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// Truncate file
|
|
17
|
+
if (existsSync(file)) {
|
|
18
|
+
await unlink(file);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// Create stream
|
|
22
|
+
const sitemap = createWriteStream(file, {flags: 'a'});
|
|
23
|
+
sitemap.write('<?xml version="1.0" encoding="UTF-8"?>\n<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n');
|
|
24
|
+
|
|
25
|
+
return {
|
|
26
|
+
|
|
27
|
+
add(url, lastmod = new Date(), priority = 0.8) {
|
|
28
|
+
sitemap.write(`\t<url><loc>${url}</loc><lastmod>${lastmod.toISOString()}</lastmod><priority>${priority}</priority></url>\r\n`);
|
|
29
|
+
},
|
|
30
|
+
|
|
31
|
+
end() {
|
|
32
|
+
sitemap.write('</urlset>');
|
|
33
|
+
sitemap.end();
|
|
34
|
+
},
|
|
35
|
+
};
|
|
12
36
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sphido/sitemap",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"author": {
|
|
5
5
|
"name": "Roman Ožana",
|
|
6
6
|
"email": "roman@ozana.cz",
|
|
@@ -9,16 +9,16 @@
|
|
|
9
9
|
"type": "module",
|
|
10
10
|
"exports": "./lib/sitemap.js",
|
|
11
11
|
"engines": {
|
|
12
|
-
"node": ">=
|
|
12
|
+
"node": ">=14"
|
|
13
13
|
},
|
|
14
14
|
"description": "Sitemap XML for Sphido CMS",
|
|
15
15
|
"license": "MIT",
|
|
16
16
|
"devDependencies": {
|
|
17
|
-
"
|
|
18
|
-
"
|
|
17
|
+
"@sindresorhus/slugify": "^2.1.0",
|
|
18
|
+
"ava": "^4.2.0"
|
|
19
19
|
},
|
|
20
20
|
"scripts": {
|
|
21
21
|
"test": "ava"
|
|
22
22
|
},
|
|
23
|
-
"gitHead": "
|
|
23
|
+
"gitHead": "d040ed1e915c034652a75003a246e985de05d2ec"
|
|
24
24
|
}
|
package/readme.md
CHANGED
|
@@ -9,24 +9,24 @@ yarn add @sphido/sitemap
|
|
|
9
9
|
## Example
|
|
10
10
|
|
|
11
11
|
```javascript
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
import {
|
|
15
|
-
import {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
(
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
12
|
+
#!/usr/bin/env node
|
|
13
|
+
|
|
14
|
+
import {dirname, relative, join} from 'node:path';
|
|
15
|
+
import {getPages, allPages} from '@sphido/core';
|
|
16
|
+
import slugify from '@sindresorhus/slugify';
|
|
17
|
+
import {createSitemap} from '../lib/sitemap.js';
|
|
18
|
+
|
|
19
|
+
const pages = await getPages({path: 'content'});
|
|
20
|
+
const map = await createSitemap('sitemap.xml');
|
|
21
|
+
|
|
22
|
+
map.add('https://sphido.org', new Date(), 1);
|
|
23
|
+
|
|
24
|
+
for (const page of await allPages(pages)) {
|
|
25
|
+
page.slug = join('/', relative('content', dirname(page.path)), slugify(page.name) + '.html');
|
|
26
|
+
map.add('https://sphido.org' + page.slug, new Date());
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
map.end();
|
|
30
30
|
```
|
|
31
31
|
|
|
32
32
|
## Source codes
|