@sphido/sitemap 2.0.25 → 3.0.1
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 +48 -0
- package/package.json +15 -9
- package/readme.md +8 -7
- 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: () => Promise<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,EAAE,MAAM,CAAC;IACZ,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,OAAO,CAAC,IAAI,CAAC,CAAC;CACzB,CAAC;AAEF;;;GAGG;AACH,wBAAsB,aAAa,CAAC,IAAI,SAAuB,GAAG,OAAO,CAAC,OAAO,CAAC,CAmCjF"}
|
package/dist/sitemap.js
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
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"?>\n');
|
|
29
|
+
sitemap.write('<urlset xmlns="https://www.sitemaps.org/schemas/sitemap/0.9">\n');
|
|
30
|
+
return {
|
|
31
|
+
add({ url, date = new Date(), priority = 0.5, changefreq = "monthly" }) {
|
|
32
|
+
sitemap.write("\t<url>\n");
|
|
33
|
+
sitemap.write(`\t\t<loc>${escapeXml(String(url))}</loc>\n`);
|
|
34
|
+
sitemap.write(`\t\t<lastmod>${date.toISOString()}</lastmod>\n`);
|
|
35
|
+
sitemap.write(`\t\t<priority>${priority}</priority>\n`);
|
|
36
|
+
sitemap.write(`\t\t<changefreq>${changefreq}</changefreq>\n`);
|
|
37
|
+
sitemap.write("\t</url>\n");
|
|
38
|
+
},
|
|
39
|
+
end() {
|
|
40
|
+
return new Promise((resolve, reject) => {
|
|
41
|
+
sitemap.write("</urlset>");
|
|
42
|
+
sitemap.end();
|
|
43
|
+
sitemap.on("finish", resolve);
|
|
44
|
+
sitemap.on("error", reject);
|
|
45
|
+
});
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
}
|
package/package.json
CHANGED
|
@@ -1,26 +1,32 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sphido/sitemap",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"author": "Roman Ožana <roman@ozana.cz> (https://ozana.cz)",
|
|
6
6
|
"homepage": "https://sphido.cz",
|
|
7
7
|
"repository": "sphido/sphido",
|
|
8
8
|
"description": "Sitemap XML for Sphido CMS",
|
|
9
|
-
"
|
|
9
|
+
"files": [
|
|
10
|
+
"dist"
|
|
11
|
+
],
|
|
12
|
+
"exports": "./dist/sitemap.js",
|
|
13
|
+
"types": "dist/sitemap.d.ts",
|
|
14
|
+
"main": "dist/sitemap.js",
|
|
10
15
|
"keywords": [
|
|
11
16
|
"sphido",
|
|
12
17
|
"sitemap"
|
|
13
18
|
],
|
|
14
19
|
"engines": {
|
|
15
|
-
"node": ">=
|
|
20
|
+
"node": ">=22"
|
|
16
21
|
},
|
|
17
22
|
"license": "MIT",
|
|
18
23
|
"devDependencies": {
|
|
19
|
-
"@sindresorhus/slugify": "^
|
|
20
|
-
"
|
|
24
|
+
"@sindresorhus/slugify": "^3.0.0",
|
|
25
|
+
"typescript": "^5.9.3",
|
|
26
|
+
"vitest": "^4.0.17"
|
|
21
27
|
},
|
|
22
28
|
"scripts": {
|
|
23
|
-
"test": "
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
}
|
|
29
|
+
"test": "vitest run --passWithNoTests",
|
|
30
|
+
"build": "rm -rf dist/* && tsc"
|
|
31
|
+
}
|
|
32
|
+
}
|
package/readme.md
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
# @sphido/sitemap
|
|
2
2
|
|
|
3
|
+
Generates [XML sitemap](https://www.sitemaps.org/protocol.html) files for Sphido CMS.
|
|
4
|
+
|
|
3
5
|
## Install
|
|
4
6
|
|
|
5
7
|
```bash
|
|
6
|
-
|
|
8
|
+
pnpm add @sphido/sitemap
|
|
7
9
|
```
|
|
8
10
|
|
|
9
11
|
## Example
|
|
@@ -11,11 +13,10 @@ yarn add @sphido/sitemap
|
|
|
11
13
|
```javascript
|
|
12
14
|
#!/usr/bin/env node
|
|
13
15
|
|
|
14
|
-
import {dirname, relative, join} from 'node:path';
|
|
15
|
-
import {getPages, allPages} from '@sphido/core';
|
|
16
|
+
import { dirname, relative, join } from 'node:path';
|
|
17
|
+
import { getPages, allPages } from '@sphido/core';
|
|
16
18
|
import slugify from '@sindresorhus/slugify';
|
|
17
|
-
import {createSitemap} from '@sphido/sitemap';
|
|
18
|
-
import got from 'got';
|
|
19
|
+
import { createSitemap } from '@sphido/sitemap';
|
|
19
20
|
|
|
20
21
|
const pages = await getPages({path: 'content'});
|
|
21
22
|
const map = await createSitemap('sitemap.xml');
|
|
@@ -36,9 +37,9 @@ for (const page of await allPages(pages)) {
|
|
|
36
37
|
map.add(page);
|
|
37
38
|
}
|
|
38
39
|
|
|
39
|
-
map.end();
|
|
40
|
+
await map.end();
|
|
40
41
|
```
|
|
41
42
|
|
|
42
|
-
## Source
|
|
43
|
+
## Source code
|
|
43
44
|
|
|
44
45
|
[@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
|
-
}
|