Skip to content

Commit 75a4d84

Browse files
committed
Add -m, --match <globs...> option
1 parent 08d5b34 commit 75a4d84

File tree

4 files changed

+30
-22
lines changed

4 files changed

+30
-22
lines changed

README.md

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ CLI to generate XML sitemaps for static sites from local filesystem
4343
Options:
4444
-b, --base <url> base URL (required)
4545
-r, --root <dir> root working directory (default: ".")
46+
-m, --match <glob...> globs to match (default: ["**/*.html"])
4647
-i, --ignore <glob...> globs to ignore (default: ["404.html"])
4748
-c, --changefreq <glob,changefreq...> comma-separated glob-changefreq pairs
4849
-p, --priority <glob,priority...> comma-separated glob-priority pairs
@@ -59,7 +60,7 @@ Options:
5960

6061
#### HTML parsing
6162

62-
By default, all matched files are piped through a fast
63+
By default, all matched `.html` files are piped through a fast
6364
[HTML parser](https://github.com/fb55/htmlparser2) to detect if the `noindex`
6465
[meta tag](https://developers.google.com/search/docs/advanced/crawling/block-indexing#meta-tag) is
6566
set - typically in the form of `<meta name="robots" content="noindex" />` - in which case that file
@@ -99,13 +100,13 @@ Disabled by default; pass option `--slash` to enable.
99100
[always added](https://github.com/zerodevx/static-sitemap-cli/tree/v1#to-slash-or-not-to-slash) to
100101
root domains.
101102

102-
#### Ignore some files
103+
#### Match or ignore files
103104

104-
The `-i` flag allows multiple entries. By default, it's set to the `["404.html"]`. Change the glob
105-
ignore patterns to suit your use-case like so:
105+
The `-m` and `-i` flags allow multiple entries. By default, they are set to the `["**/*.html"]` and
106+
`["404.html"]` respectively. Change the glob patterns to suit your use-case like so:
106107

107108
```
108-
$ sscli ... -i '404.html' '**/ignore/**' 'this/other/specific/file.html'
109+
$ sscli ... -m '**/*.{html,jpg,png}' -i '404.html' 'ignore/**' 'this/other/specific/file.html'
109110
```
110111

111112
#### Glob-[*] pairs
@@ -143,7 +144,13 @@ $ sscli -b https://x.com -r dist -f xml -o > www/sm.xml
143144
#### Get subset of a directory
144145

145146
```
146-
$ sscli -b https://x.com/foo -r dist/foo -f txt -o > dist/sitemap.txt
147+
$ sscli -b https://x.com/foo -r dist/foo -f xml -o > dist/sitemap.xml
148+
```
149+
150+
#### Generate TXT sitemap for image assets
151+
152+
```
153+
$ sscli -b https://x.com -r dist -m '**/*.{jpg,jpeg,gif,png,bmp,webp,svg}' -f txt
147154
```
148155

149156
## Programmatic Use
@@ -160,6 +167,7 @@ import {
160167
const options = {
161168
base: 'https://x.com',
162169
root: 'path/to/root',
170+
match: ['**/*html'],
163171
ignore: ['404.html'],
164172
changefreq: [],
165173
priority: [],

package.json

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "static-sitemap-cli",
3-
"version": "2.0.1",
3+
"version": "2.1.0",
44
"description": "CLI to generate XML sitemaps for static sites from local filesystem",
55
"author": "Jason Lee <[email protected]>",
66
"type": "module",
@@ -38,10 +38,7 @@
3838
],
3939
"license": "ISC",
4040
"homepage": "https://npmjs.com/package/static-sitemap-cli",
41-
"repository": {
42-
"type": "git",
43-
"url": "https://github.com/zerodevx/static-sitemap-cli.git"
44-
},
41+
"repository": "github:zerodevx/static-sitemap-cli",
4542
"keywords": [
4643
"sscli",
4744
"sitemap",

src/cli.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ program
1616
.description('CLI to generate XML sitemaps for static sites from local filesystem')
1717
.option('-b, --base <url>', 'base URL (required)')
1818
.option('-r, --root <dir>', 'root working directory', '.')
19+
.option('-m, --match <glob...>', 'globs to match', ['**/*.html'])
1920
.option('-i, --ignore <glob...>', 'globs to ignore', ['404.html'])
2021
.option('-c, --changefreq <glob,changefreq...>', 'comma-separated glob-changefreq pairs')
2122
.option('-p, --priority <glob,priority...>', 'comma-separated glob-priority pairs')

src/index.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ function log(msg) {
1010
console.warn('\x1b[36m%s\x1b[0m', `[sscli] ${msg}`)
1111
}
1212

13-
async function getFiles({ root, ignore, verbose }) {
14-
const files = await fastglob('**/*.html', { cwd: root, stats: true, ignore })
13+
async function getFiles({ root, match, ignore, verbose }) {
14+
const files = await fastglob(match, { cwd: root, stats: true, ignore })
1515
if (!files.length) {
1616
throw new Error('NO_MATCHES')
1717
}
@@ -43,16 +43,18 @@ function detectNoindex(path) {
4343
}
4444

4545
async function transformUrl(
46-
file,
46+
{ path, stats: { mtime } },
4747
{ root, base, changefreq, priority, robots, clean, slash, verbose }
4848
) {
49-
if (robots) {
50-
if (await detectNoindex(nodepath.join(root, file.path))) {
51-
if (verbose) log(`noindex: ${file.path}`)
52-
return
53-
}
49+
if (
50+
robots &&
51+
nodepath.extname(path) === '.html' &&
52+
(await detectNoindex(nodepath.join(root, path)))
53+
) {
54+
if (verbose) log(`noindex: ${path}`)
55+
return
5456
}
55-
let url = base + file.path.split(nodepath.sep).join('/')
57+
let url = base + path.split(nodepath.sep).join('/')
5658
if (clean) {
5759
if (url.slice(-11) === '/index.html') url = url.slice(0, -11)
5860
else if (url.slice(-5) === '.html') url = url.slice(0, -5)
@@ -61,12 +63,12 @@ async function transformUrl(
6163
const check = (pairs, tagname) => {
6264
for (let a = pairs.length - 1; a >= 0; a--) {
6365
const p = pairs[a].split(',')
64-
if (micromatch.isMatch(file.path, p[0])) return { [tagname]: p[1] }
66+
if (micromatch.isMatch(path, p[0])) return { [tagname]: p[1] }
6567
}
6668
}
6769
return {
6870
loc: url,
69-
lastmod: file.stats.mtime.toISOString(),
71+
lastmod: mtime.toISOString(),
7072
...(changefreq && changefreq.length && check(changefreq, 'changefreq')),
7173
...(priority && priority.length && check(priority, 'priority'))
7274
}

0 commit comments

Comments
 (0)