Skip to content

Commit e10c2e0

Browse files
authored
Release 1.1.0
* Generate Sitemap Index and Splitting into multiple sitemaps (#51) * Update documentation
1 parent 76eecbc commit e10c2e0

File tree

11 files changed

+131
-25
lines changed

11 files changed

+131
-25
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
.DS_Store
33
/node_modules
44
sitemap.xml
5+
sitemap*.xml
56
index.es5.js
67
npm-debug.log

api.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,34 @@ Convert array of paths to sitemap.
9797
**Parameters**
9898

9999
- `hostname` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** The root name of your site.
100+
- `$1` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)**
101+
- `$1.limitCountPaths` (optional, default `49999`)
100102

101103
## save
102104

103-
Save the sitemap to a file.
105+
Save sitemaps and sitemap index in files.
104106

105107
**Parameters**
106108

107-
- `dist` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** The path and file name where the sitemap is saved.
109+
- `dist` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** The path and file name where the sitemap index is saved.
110+
- `publicPath` **\[[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)](default '/')** optional public path relative to hostname, default: '/'
111+
112+
# pathsSplitter
113+
114+
Module for splitting paths array in multiple arrays for support of large projects
115+
116+
**Parameters**
117+
118+
- `paths` **\[[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)]** Initial paths array (flattened) (optional, default `[]`)
119+
- `size` **\[[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)]** (optional, default `49999`)
120+
121+
**Examples**
122+
123+
```javascript
124+
import { pathsSplitter } from 'react-router-sitemap';
125+
126+
const splitted = pathsSplitter(paths, 49999); // 49999 because of Google sitemap limits
127+
```
108128

109129
# paramsApplier
110130

example/sitemap-builder.js

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require('babel-register');
22

33
const router = require('./router').default;
4-
const Sitemap = require('../').default;
4+
const Sitemap = require('../lib').default;
55

66
const filterConfig = {
77
isValid: false,
@@ -11,23 +11,48 @@ const filterConfig = {
1111
],
1212
};
1313

14+
15+
// Setup some random projects
16+
const alphabet = 'abcdefghijklmnopqrstuvwxyz'.split('');
17+
const lotOfNames = [];
18+
19+
function produceItem(arr, char) {
20+
return arr.map(c => { lotOfNames.push(`${c}-${char}`); });
21+
}
22+
23+
alphabet.map(c => produceItem(alphabet, c));
24+
25+
const projects = {
26+
projectName: lotOfNames,
27+
achievement: lotOfNames
28+
};
29+
30+
1431
const paramsConfig = {
1532
'/projects/:projectName': [
1633
{ projectName: 'hello-world' },
1734
{ projectName: 'second-project' },
1835
{ projectName: ['third-project', 'fourth-project'] },
36+
projects
1937
],
2038
'/projects/:projectName/view': [
2139
{ projectName: 'hello-world' },
2240
{ projectName: 'second-project' },
2341
{ projectName: ['third-project', 'fourth-project'] },
42+
projects
2443
],
44+
'/projects/:projectName/achievements/:achievement': [
45+
{ projectName: 'hello-world' },
46+
{ projectName: 'second-project' },
47+
{ projectName: ['third-project', 'fourth-project'] },
48+
projects
49+
]
2550
};
2651

2752
(
2853
new Sitemap(router)
2954
.filterPaths(filterConfig)
3055
.applyParams(paramsConfig)
31-
.build('http://my-site.ru')
32-
.save('./sitemap.xml')
56+
.build('http://my-site.ru', { limitCountPaths: 5000 })
57+
.save('./sitemap.xml', '/static/')
3358
);

lib/index.js

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
import fs from 'fs';
2+
import path from 'path';
3+
4+
import sm from 'sitemap';
25
import { createRoutes } from 'react-router';
6+
37
import parseRoutes from './routes-parser';
48
import filterPaths from './paths-filter';
59
import applyParams from './params-applier';
10+
import splitPaths from './paths-splitter';
611
import buildSitemap from './sitemap-builder';
712

813
/**
914
* @class Sitemap
10-
* @description Generate sitemap by [React Router](https://www.npmjs.com/package/react-router) configuration.
15+
* @description Generate a sitemap using the [React Router](https://www.npmjs.com/package/react-router) configuration.
1116
*
1217
* @example
1318
* import Sitemap from 'react-router-sitemap';
@@ -22,7 +27,7 @@ class Sitemap {
2227

2328
/**
2429
* @constructor
25-
* @description Convert React Router config to array of paths.
30+
* @description Convert a React Router config to an array of paths.
2631
* @param {Route} router - React Router configuration.
2732
*
2833
* @example
@@ -46,7 +51,7 @@ class Sitemap {
4651
}
4752

4853
/**
49-
* @description Filters path on the specified rules.
54+
* @description Filter paths using the specified rules.
5055
* @param {Object} filterConfig - Filter configuration
5156
* @property {Array<RegExp>} rules - List filter rules.
5257
* @property {Boolean} isValid - Flag that defines a way to filter paths.
@@ -74,20 +79,20 @@ class Sitemap {
7479
}
7580

7681
/**
77-
* @description Replaces the dynamic parameters in paths the passed value.
78-
* @param {Object.<String, Array>} paramsConfig - Configuration for replace params.
82+
* @description Replace the dynamic parameters in paths using the given values.
83+
* @param {Object.<String, Array>} paramsConfig - Configuration for replacing params.
7984
*
8085
* @example
81-
* <caption>Config for replace params `:param` in path `/path/:param`</caption>
86+
* <caption>Config for replacing params `:param` in the path `/path/:param`</caption>
8287
* {
8388
* '/path/:param': [
8489
* { param: 'value' }
8590
* ]
8691
* }
8792
*
8893
* @example
89-
* <caption>Config for replace params `:param` and `:subparam`
90-
* in path `/path/:param/:subparam`</caption>
94+
* <caption>Config for replacing params `:param` and `:subparam`
95+
* in the path `/path/:param/:subparam`</caption>
9196
* {
9297
* '/path/:param/:subparam': [
9398
* { param: 'value', subparam: ['subvalue1', 'subvalue2'] }
@@ -104,17 +109,40 @@ class Sitemap {
104109
* @description Convert array of paths to sitemap.
105110
* @param {String} hostname - The root name of your site.
106111
*/
107-
build(hostname) {
108-
this.sitemap = buildSitemap(hostname, this.paths);
112+
build(hostname, { limitCountPaths = 49999 }) {
113+
this.hostname = hostname;
114+
this.splitted = splitPaths(this.paths, limitCountPaths);
115+
this.sitemaps = this.splitted.map(paths => buildSitemap(hostname, paths));
109116
return this;
110117
}
111118

112119
/**
113-
* @description Save sitemap in file.
114-
* @param {String} dist - The path and file name where the sitemap is saved.
120+
* @description Save sitemaps and sitemap index in files.
121+
* @param {String} dist - The path and file name where the sitemap index is saved.
122+
* @param {String} publicPath - optional public path relative to hostname, default: '/'
115123
*/
116-
save(dist) {
117-
fs.writeFileSync(dist, this.sitemap.toString());
124+
save(dist, publicPath = '/') {
125+
const sitemapPaths = [];
126+
127+
this.sitemaps.map((sitemap, index) => {
128+
const savePath = dist.replace('.xml', `-${index}.xml`);
129+
130+
// write sitemap
131+
fs.writeFileSync(savePath, sitemap.toString());
132+
133+
// push public path for indexing
134+
sitemapPaths.push(this.hostname + publicPath + path.basename(savePath));
135+
});
136+
137+
// create index string
138+
const sitemapIndex = sm.buildSitemapIndex({
139+
urls: sitemapPaths,
140+
hostname: this.hostname
141+
});
142+
143+
// write sitemap index
144+
fs.writeFileSync(dist, sitemapIndex);
145+
118146
return this;
119147
}
120148

@@ -126,3 +154,4 @@ export { default as routesParser } from './routes-parser';
126154
export { default as pathsFilter } from './paths-filter';
127155
export { default as paramsApplier } from './params-applier';
128156
export { default as sitemapBuilder } from './sitemap-builder';
157+
export { default as pathsSplitter } from './paths-splitter';

lib/params-applier/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const hasRules = (path = '', paramsConfig = {}) => {
55
};
66

77
/**
8-
* Module for apply params in dynamic paths.
8+
* Module for applying params in dynamic paths.
99
* @module paramsApplier
1010
* @param {Array<String>} [paths] Array of paths
1111
* @param {Object<String, Array>} [paramsConfig] Configuration matching parameters and values

lib/paths-filter/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import prepareParams from './params-preparer';
22

33
/**
4-
* Module for filter array of paths.
4+
* Module for filtering an array of paths.
55
* @function pathsFilter
66
* @param {Array<String>} [paths] Array of paths
77
* @param {Array<RegExp>} [rules] Filter rules

lib/paths-splitter/index.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Module for splitting paths array in multiple arrays for support of large projects
3+
* @module pathsSplitter
4+
* @param {Array} [paths = []] Initial paths array (flattened)
5+
* @param {Number} [size = 49999]
6+
* @return {Array Array<String>} Array of Array of paths
7+
*
8+
* @example
9+
* import { pathsSplitter } from 'react-router-sitemap';
10+
*
11+
* const splitted = pathsSplitter(paths, 49999); // 49999 because of Google sitemap limits
12+
*/
13+
14+
const pathsSplitter = (paths, size) => paths.map((path, i) => {
15+
return (i % size === 0) ? paths.slice(i, i + size) : null;
16+
}).filter(e => e);
17+
18+
export default pathsSplitter;

lib/routes-parser/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import buildPath from './path-builder';
22

33
/**
4-
* Module for parsing result of execute function `createRoutes(<Route>)`
4+
* Module for parsing the result of the `createRoutes(<Route>)` function.
55
* from [react-router](https://www.npmjs.com/package/react-router) package.
66
* @module routesParser
77
* @param {Array|Object} [routes = []] Result of execute function

lib/sitemap-builder/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import sitemap from 'sitemap';
22

33
/**
4-
* Module for build sitemap by array of paths. Using [sitemap](https://www.npmjs.com/package/sitemap) package.
4+
* Module for building a sitemap using an array of paths. Uses the [sitemap](https://www.npmjs.com/package/sitemap) package.
55
* @module sitemapBuilder
66
* @param {String} [hostname] The root name of your site
77
* @param {Array<String>} [paths] Array of paths
8-
* @return {Sitemap} Instance of [Sitemap](https://www.npmjs.com/package/sitemap).
8+
* @return {Sitemap} instance of [Sitemap](https://www.npmjs.com/package/sitemap).
99
*
1010
* @example
1111
* import { sitemapBuilder as buildSitemap } from 'react-router-sitemap';

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-router-sitemap",
3-
"version": "1.0.8",
3+
"version": "1.1.0",
44
"description": "Module to generate a sitemap for react-router configuration",
55
"repository": {
66
"type": "git",

0 commit comments

Comments
 (0)