@docmd/plugin-sitemap 0.4.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 +21 -0
- package/README.md +19 -0
- package/index.js +68 -0
- package/package.json +7 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 docmd (docmd.io)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# @docmd/plugin-sitemap
|
|
2
|
+
|
|
3
|
+
Automatically generates `sitemap.xml` for **docmd** sites.
|
|
4
|
+
|
|
5
|
+
**Note:** You must set `siteUrl` in your `docmd.config.js` for this plugin to work.
|
|
6
|
+
|
|
7
|
+
## Configuration
|
|
8
|
+
```javascript
|
|
9
|
+
// docmd.config.js
|
|
10
|
+
module.exports = {
|
|
11
|
+
siteUrl: 'https://mysite.com', // Required
|
|
12
|
+
plugins: {
|
|
13
|
+
sitemap: {
|
|
14
|
+
defaultChangefreq: 'weekly',
|
|
15
|
+
defaultPriority: 0.8
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
```
|
package/index.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
const path = require('path');
|
|
2
|
+
const fs = require('fs/promises');
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Hook to run after the build is complete.
|
|
6
|
+
* @param {Object} context
|
|
7
|
+
* @param {Object} context.config - The parsed project config
|
|
8
|
+
* @param {Array} context.pages - Array of page objects { outputPath, frontmatter }
|
|
9
|
+
* @param {string} context.outputDir - Absolute path to output directory
|
|
10
|
+
* @param {Function} context.log - Logger function
|
|
11
|
+
*/
|
|
12
|
+
async function onPostBuild({ config, pages, outputDir, log }) {
|
|
13
|
+
// 1. Check if enabled
|
|
14
|
+
if (config.plugins?.sitemap === false || !config.siteUrl) {
|
|
15
|
+
if (!config.siteUrl && log) log('⚠️ Skipping sitemap: "siteUrl" is missing in config.');
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const siteUrl = config.siteUrl.replace(/\/$/, '');
|
|
20
|
+
|
|
21
|
+
// 2. Build XML Header
|
|
22
|
+
let sitemapXml = '<?xml version="1.0" encoding="UTF-8"?>\n';
|
|
23
|
+
sitemapXml += '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n';
|
|
24
|
+
|
|
25
|
+
// 3. Defaults
|
|
26
|
+
const defaultChangefreq = config.plugins?.sitemap?.defaultChangefreq || 'weekly';
|
|
27
|
+
const defaultPriority = config.plugins?.sitemap?.defaultPriority || 0.8;
|
|
28
|
+
const rootPriority = config.plugins?.sitemap?.rootPriority || 1.0;
|
|
29
|
+
|
|
30
|
+
// 4. Loop Pages
|
|
31
|
+
for (const page of pages) {
|
|
32
|
+
const fm = page.frontmatter || {};
|
|
33
|
+
|
|
34
|
+
// Skip hidden pages
|
|
35
|
+
if (fm.sitemap === false || fm.noindex === true) continue;
|
|
36
|
+
|
|
37
|
+
const pagePath = page.outputPath; // e.g. "guide/index.html"
|
|
38
|
+
let url = siteUrl;
|
|
39
|
+
|
|
40
|
+
// URL Construction Logic
|
|
41
|
+
if (pagePath === 'index.html') {
|
|
42
|
+
url += '/';
|
|
43
|
+
} else if (pagePath.endsWith('/index.html')) {
|
|
44
|
+
url += '/' + pagePath.replace('/index.html', '/');
|
|
45
|
+
} else {
|
|
46
|
+
url += '/' + pagePath;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Metadata Logic
|
|
50
|
+
let priority = fm.priority || (pagePath === 'index.html' ? rootPriority : defaultPriority);
|
|
51
|
+
const changefreq = fm.changefreq || defaultChangefreq;
|
|
52
|
+
|
|
53
|
+
sitemapXml += ' <url>\n';
|
|
54
|
+
sitemapXml += ` <loc>${url}</loc>\n`;
|
|
55
|
+
if (fm.lastmod) sitemapXml += ` <lastmod>${fm.lastmod}</lastmod>\n`;
|
|
56
|
+
sitemapXml += ` <changefreq>${changefreq}</changefreq>\n`;
|
|
57
|
+
sitemapXml += ` <priority>${priority}</priority>\n`;
|
|
58
|
+
sitemapXml += ' </url>\n';
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
sitemapXml += '</urlset>';
|
|
62
|
+
|
|
63
|
+
// 5. Write File
|
|
64
|
+
await fs.writeFile(path.join(outputDir, 'sitemap.xml'), sitemapXml);
|
|
65
|
+
if (log) log('✅ Sitemap generated.');
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
module.exports = { onPostBuild };
|