@sphido/sitemap 2.0.23 → 3.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/dist/sitemap.d.ts +17 -0
- package/dist/sitemap.d.ts.map +1 -0
- package/dist/sitemap.js +44 -0
- package/package.json +23 -15
- package/readme.md +5 -5
- package/lib/sitemap.js +0 -53
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
interface SitemapParams {
|
|
2
|
+
url?: string;
|
|
3
|
+
date?: Date;
|
|
4
|
+
priority?: number;
|
|
5
|
+
changefreq?: "daily" | "weekly" | "monthly" | "yearly";
|
|
6
|
+
}
|
|
7
|
+
export type Sitemap = {
|
|
8
|
+
add: (params: SitemapParams) => void;
|
|
9
|
+
end: () => void;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Generate XML sitemap
|
|
13
|
+
* @see https://www.sitemaps.org/protocol.html
|
|
14
|
+
*/
|
|
15
|
+
export declare function createSitemap(file?: string): Promise<Sitemap>;
|
|
16
|
+
export {};
|
|
17
|
+
//# sourceMappingURL=sitemap.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sitemap.d.ts","sourceRoot":"","sources":["../lib/sitemap.ts"],"names":[],"mappings":"AAcA,UAAU,aAAa;IACtB,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,IAAI,CAAC,EAAE,IAAI,CAAC;IACZ,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,UAAU,CAAC,EAAE,OAAO,GAAG,QAAQ,GAAG,SAAS,GAAG,QAAQ,CAAC;CACvD;AAED,MAAM,MAAM,OAAO,GAAG;IACrB,GAAG,EAAE,CAAC,MAAM,EAAE,aAAa,KAAK,IAAI,CAAC;IACrC,GAAG,EAAE,MAAM,IAAI,CAAC;CAChB,CAAC;AAEF;;;GAGG;AACH,wBAAsB,aAAa,CAAC,IAAI,SAAuB,GAAG,OAAO,CAAC,OAAO,CAAC,CA+BjF"}
|
package/dist/sitemap.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { createWriteStream, existsSync } from "node:fs";
|
|
2
|
+
import { mkdir, unlink } from "node:fs/promises";
|
|
3
|
+
import { dirname } from "node:path";
|
|
4
|
+
/** Escape text for XML element content (order: `&` first). */
|
|
5
|
+
function escapeXml(text) {
|
|
6
|
+
return text
|
|
7
|
+
.replaceAll("&", "&")
|
|
8
|
+
.replaceAll("<", "<")
|
|
9
|
+
.replaceAll(">", ">")
|
|
10
|
+
.replaceAll('"', """)
|
|
11
|
+
.replaceAll("'", "'");
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Generate XML sitemap
|
|
15
|
+
* @see https://www.sitemaps.org/protocol.html
|
|
16
|
+
*/
|
|
17
|
+
export async function createSitemap(file = "public/sitemap.xml") {
|
|
18
|
+
// Create directory
|
|
19
|
+
if (!existsSync(dirname(file))) {
|
|
20
|
+
await mkdir(dirname(file), { recursive: true });
|
|
21
|
+
}
|
|
22
|
+
// Truncate file
|
|
23
|
+
if (existsSync(file)) {
|
|
24
|
+
await unlink(file);
|
|
25
|
+
}
|
|
26
|
+
// Create stream
|
|
27
|
+
const sitemap = createWriteStream(file, { flags: "a" });
|
|
28
|
+
sitemap.write('<?xml version="1.0" encoding="UTF-8"?>\r\n');
|
|
29
|
+
sitemap.write('<urlset xmlns="https://www.sitemaps.org/schemas/sitemap/0.9">\r\n');
|
|
30
|
+
return {
|
|
31
|
+
add({ url, date = new Date(), priority = 0.5, changefreq = "monthly" } = {}) {
|
|
32
|
+
sitemap.write("\t<url>\r\n");
|
|
33
|
+
sitemap.write(`\t\t<loc>${escapeXml(String(url))}</loc>\r\n`);
|
|
34
|
+
sitemap.write(`\t\t<lastmod>${date.toISOString()}</lastmod>\r\n`);
|
|
35
|
+
sitemap.write(`\t\t<priority>${priority}</priority>\r\n`);
|
|
36
|
+
sitemap.write(`\t\t<changefreq>${changefreq}</changefreq>\r\n`);
|
|
37
|
+
sitemap.write("\t</url>\r\n");
|
|
38
|
+
},
|
|
39
|
+
end() {
|
|
40
|
+
sitemap.write("</urlset>");
|
|
41
|
+
sitemap.end();
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
}
|
package/package.json
CHANGED
|
@@ -1,24 +1,32 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sphido/sitemap",
|
|
3
|
-
"version": "
|
|
4
|
-
"author": {
|
|
5
|
-
"name": "Roman Ožana",
|
|
6
|
-
"email": "roman@ozana.cz",
|
|
7
|
-
"url": "https://ozana.cz"
|
|
8
|
-
},
|
|
3
|
+
"version": "3.0.0",
|
|
9
4
|
"type": "module",
|
|
10
|
-
"
|
|
5
|
+
"author": "Roman Ožana <roman@ozana.cz> (https://ozana.cz)",
|
|
6
|
+
"homepage": "https://sphido.cz",
|
|
7
|
+
"repository": "sphido/sphido",
|
|
8
|
+
"description": "Sitemap XML for Sphido CMS",
|
|
9
|
+
"files": [
|
|
10
|
+
"dist"
|
|
11
|
+
],
|
|
12
|
+
"exports": "./dist/sitemap.js",
|
|
13
|
+
"types": "dist/sitemap.d.ts",
|
|
14
|
+
"main": "dist/sitemap.js",
|
|
15
|
+
"keywords": [
|
|
16
|
+
"sphido",
|
|
17
|
+
"sitemap"
|
|
18
|
+
],
|
|
11
19
|
"engines": {
|
|
12
|
-
"node": ">=
|
|
20
|
+
"node": ">=22"
|
|
13
21
|
},
|
|
14
|
-
"description": "Sitemap XML for Sphido CMS",
|
|
15
22
|
"license": "MIT",
|
|
16
23
|
"devDependencies": {
|
|
17
|
-
"@sindresorhus/slugify": "^
|
|
18
|
-
"
|
|
24
|
+
"@sindresorhus/slugify": "^3.0.0",
|
|
25
|
+
"typescript": "^5.9.3",
|
|
26
|
+
"vitest": "^4.0.17"
|
|
19
27
|
},
|
|
20
28
|
"scripts": {
|
|
21
|
-
"test": "
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
}
|
|
29
|
+
"test": "vitest run --passWithNoTests",
|
|
30
|
+
"build": "rm -rf dist/* && tsc"
|
|
31
|
+
}
|
|
32
|
+
}
|
package/readme.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
## Install
|
|
4
4
|
|
|
5
5
|
```bash
|
|
6
|
-
|
|
6
|
+
pnpm add @sphido/sitemap
|
|
7
7
|
```
|
|
8
8
|
|
|
9
9
|
## Example
|
|
@@ -11,10 +11,10 @@ yarn add @sphido/sitemap
|
|
|
11
11
|
```javascript
|
|
12
12
|
#!/usr/bin/env node
|
|
13
13
|
|
|
14
|
-
import {dirname, relative, join} from 'node:path';
|
|
15
|
-
import {getPages, allPages} from '@sphido/core';
|
|
14
|
+
import { dirname, relative, join } from 'node:path';
|
|
15
|
+
import { getPages, allPages } from '@sphido/core';
|
|
16
16
|
import slugify from '@sindresorhus/slugify';
|
|
17
|
-
import {createSitemap} from '@sphido/sitemap';
|
|
17
|
+
import { createSitemap } from '@sphido/sitemap';
|
|
18
18
|
import got from 'got';
|
|
19
19
|
|
|
20
20
|
const pages = await getPages({path: 'content'});
|
|
@@ -39,6 +39,6 @@ for (const page of await allPages(pages)) {
|
|
|
39
39
|
map.end();
|
|
40
40
|
```
|
|
41
41
|
|
|
42
|
-
## Source
|
|
42
|
+
## Source code
|
|
43
43
|
|
|
44
44
|
[@sphido/sitemap](https://github.com/sphido/sphido/tree/main/packages/sphido-sitemap)
|
package/lib/sitemap.js
DELETED
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
import {createWriteStream, existsSync} from 'node:fs';
|
|
2
|
-
import {mkdir, unlink} from 'node:fs/promises';
|
|
3
|
-
import {dirname} from 'node:path';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Generate XML sitemap
|
|
7
|
-
* @see https://www.sitemaps.org/protocol.html
|
|
8
|
-
* @param {string} file
|
|
9
|
-
* @return {Promise<{add({url: string, date?: Date, priority?: Number, changefreq?: string}=): void, end(): void}>}
|
|
10
|
-
*/
|
|
11
|
-
export async function createSitemap(file = 'public/sitemap.xml') {
|
|
12
|
-
// Create directory
|
|
13
|
-
if (!existsSync(dirname(file))) {
|
|
14
|
-
await mkdir(dirname(file), {recursive: true});
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
// Truncate file
|
|
18
|
-
if (existsSync(file)) {
|
|
19
|
-
await unlink(file);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
// Create stream
|
|
23
|
-
const sitemap = createWriteStream(file, {flags: 'a'});
|
|
24
|
-
sitemap.write('<?xml version="1.0" encoding="UTF-8"?>\r\n');
|
|
25
|
-
sitemap.write('<urlset xmlns="https://www.sitemaps.org/schemas/sitemap/0.9">\r\n');
|
|
26
|
-
|
|
27
|
-
return {
|
|
28
|
-
/**
|
|
29
|
-
* Add URL to sitemap
|
|
30
|
-
* @param {string} url
|
|
31
|
-
* @param {Date} date
|
|
32
|
-
* @param {Number} priority
|
|
33
|
-
* @param {string} changefreq
|
|
34
|
-
*/
|
|
35
|
-
|
|
36
|
-
add({url, date = new Date(), priority = 0.5, changefreq = 'monthly'} = {}) {
|
|
37
|
-
sitemap.write('\t<url>\r\n');
|
|
38
|
-
sitemap.write(`\t\t<loc>${url}</loc>\r\n`);
|
|
39
|
-
sitemap.write(`\t\t<lastmod>${date.toISOString()}</lastmod>\r\n`);
|
|
40
|
-
sitemap.write(`\t\t<priority>${priority}</priority>\r\n`);
|
|
41
|
-
sitemap.write(`\t\t<changefreq>${changefreq}</changefreq>\r\n`);
|
|
42
|
-
sitemap.write('\t</url>\r\n');
|
|
43
|
-
},
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Save sitemap end
|
|
47
|
-
*/
|
|
48
|
-
end() {
|
|
49
|
-
sitemap.write('</urlset>');
|
|
50
|
-
sitemap.end();
|
|
51
|
-
},
|
|
52
|
-
};
|
|
53
|
-
}
|