Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alternate implementation for #65. #92

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,25 @@ Default: `true`

Indicates whether [Google AMP pages](https://www.ampproject.org/) should be ignored and not be added to the sitemap.

### onAdd(url)

Modify the URL before it's added to the sitemap. A false or undefined return value ignores the URL, and it will not be added to the sitemap. Useful for crawling locally before hosting.

Type: `function`
Default: `null`

Example:

```JavaScript
// create generator
const generator = SitemapGenerator(host, {
onAdd: (url) => {
// Replace host in sitemap.
return url.replace(host, 'https://example.com')
}
});
```

### lastMod

Type: `boolean`
Expand Down
10 changes: 6 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ module.exports = function SitemapGenerator(uri, opts) {
changeFreq: '',
priorityMap: [],
ignoreAMP: true,
ignore: null
ignore: null,
onAdd: null
};

if (!uri) {
Expand Down Expand Up @@ -95,14 +96,15 @@ module.exports = function SitemapGenerator(uri, opts) {

// fetch complete event
crawler.on('fetchcomplete', (queueItem, page) => {
const { url, depth } = queueItem;
const { depth } = queueItem;
const url = opts.onAdd ? opts.onAdd(queueItem.url) : queueItem.url;

if (
(opts.ignore && opts.ignore(url)) ||
(!url || opts.ignore && opts.ignore(url)) ||
/(<meta(?=[^>]+noindex).*?>)/.test(page) || // check if robots noindex is present
(options.ignoreAMP && /<html[^>]+(amp|⚡)[^>]*>/.test(page)) // check if it's an amp page
) {
emitter.emit('ignore', url);
emitter.emit('ignore', queueItem.url);
} else {
emitter.emit('add', url);

Expand Down