@sphido/sitemap 1.1.0 → 2.0.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  MIT License
2
2
  -----------
3
3
 
4
- Copyright (c) 2021 Roman Ožana (https://ozana.cz/)
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 sitemap XML from posts
3
- * @param {array} pages
4
- * @param {string} domain
5
- * @returns {string}
6
+ * Generate XML sitemap
7
+ * @param {string} file
8
+ * @returns {Object<{add: add, end: end}>}
6
9
  */
7
- export function sitemap(pages, domain) {
8
- return '<?xml version="1.0" encoding="UTF-8"?>\n<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n'
9
- + `<url><loc>${domain}</loc><lastmod>${new Date().toISOString()}</lastmod><priority>1.0</priority></url>\n`
10
- + pages.map(post => `<url><loc>${typeof post.link === 'function' ? post.link(domain) : post.link}</loc><lastmod>${post.date.toISOString()}</lastmod><priority>0.80</priority></url>\n`).join('')
11
- + '</urlset>';
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": "1.1.0",
3
+ "version": "2.0.2",
4
4
  "author": {
5
5
  "name": "Roman Ožana",
6
6
  "email": "roman@ozana.cz",
@@ -14,11 +14,11 @@
14
14
  "description": "Sitemap XML for Sphido CMS",
15
15
  "license": "MIT",
16
16
  "devDependencies": {
17
- "ava": "^4.2.0",
18
- "fs-extra": "^10.0.1"
17
+ "@sindresorhus/slugify": "^2.1.0",
18
+ "ava": "^4.2.0"
19
19
  },
20
20
  "scripts": {
21
21
  "test": "ava"
22
22
  },
23
- "gitHead": "06fc68158f4f3d76c8f3f6e9f3d411c635913a91"
23
+ "gitHead": "df860e9742cc0228338438748f62ea9733ff0872"
24
24
  }
package/readme.md CHANGED
@@ -9,26 +9,34 @@ yarn add @sphido/sitemap
9
9
  ## Example
10
10
 
11
11
  ```javascript
12
- import fs from 'fs-extra';
13
- import path from 'path';
14
- import {fileURLToPath} from 'url';
15
- import {sitemap} from '@sphido/sitemap';
16
-
17
- const __dirname = path.dirname(fileURLToPath(import.meta.url));
18
-
19
- const posts = [
20
- {link: 'https://example.com/first', date: new Date()},
21
- {link: 'https://example.com/second', date: new Date()},
22
- ];
23
-
24
- (async () => {
25
- await fs.outputFile(
26
- __dirname + '/sitemap.xml',
27
- sitemap(posts, 'https://example.com')
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
33
33
 
34
- https://github.com/sphido/sphido/tree/main/packages/sphido-sitemap
34
+ https://github.com/sphido/sphido/tree/main/packages/sphido-sitemap
35
+
36
+ ## TODO
37
+
38
+ Ping Google about changes:
39
+
40
+ ```text
41
+ https://www.google.com/webmasters/tools/ping?sitemap=https://www.mydomain.com/sitemap.xml
42
+ ```