Skip to content

Commit

Permalink
Release 1.2.0 (#84)
Browse files Browse the repository at this point in the history
* Add support React Router v4 (#82)
* Release 1.2.0
  • Loading branch information
kuflash authored Jan 30, 2019
1 parent 66247e6 commit a42a357
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 33 deletions.
70 changes: 45 additions & 25 deletions api.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ Filter paths using the specified rules.

**Parameters**

- `filterConfig` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** Filter configuration
- `filterConfig` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** Filter configuration

**Properties**

- `rules` **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[RegExp](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp)>** List filter rules.
- `isValid` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** Flag that defines a way to filter paths.
- `rules` **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[RegExp](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/RegExp)>** List filter rules.
- `isValid` **[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** Flag that defines a way to filter paths.
If `true`, the path satisfying the rules will be included.
If `false`, the path satisfying the rules will be excluded.

Expand All @@ -65,7 +65,7 @@ Replace the dynamic parameters in paths using the given values.

**Parameters**

- `paramsConfig` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)<[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String), [Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)>** Configuration for replacing params.
- `paramsConfig` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)<[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String), [Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)>** Configuration for replacing params.

**Examples**

Expand Down Expand Up @@ -96,8 +96,8 @@ Convert array of paths to sitemap.

**Parameters**

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

## save
Expand All @@ -106,17 +106,17 @@ Save sitemaps and sitemap index in files.

**Parameters**

- `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.
- `publicPath` **\[[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)](default '/')** optional public path relative to hostname, default: '/'
- `dist` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** The path and file name where the sitemap index is saved.
- `publicPath` **\[[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)](default '/')** optional public path relative to hostname, default: '/'

# pathsSplitter

Module for splitting paths array in multiple arrays for support of large projects

**Parameters**

- `paths` **\[[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)]** Initial paths array (flattened) (optional, default `[]`)
- `size` **\[[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)]** (optional, default `49999`)
- `paths` **\[[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)]** Initial paths array (flattened) (optional, default `[]`)
- `size` **\[[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)]** (optional, default `49999`)

**Examples**

Expand All @@ -132,8 +132,8 @@ Module for applying params in dynamic paths.

**Parameters**

- `paths` **\[[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)>]** Array of paths
- `paramsConfig` **\[[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)<[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String), [Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)>]** Configuration matching parameters and values
- `paths` **\[[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)>]** Array of paths
- `paramsConfig` **\[[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)<[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String), [Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)>]** Configuration matching parameters and values

**Examples**

Expand Down Expand Up @@ -167,17 +167,17 @@ const paths = applyParams(paths, config);
// ['/path/a/1', '/path/b/2', '/path/b/3']
```

Returns **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)>** Array of paths
Returns **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)>** Array of paths

# pathsFilter

Module for filtering an array of paths.

**Parameters**

- `paths` **\[[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)>]** Array of paths
- `rules` **\[[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[RegExp](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp)>]** Filter rules
- `isValidRules` **\[[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)]** Flag that defines a way to filter paths.
- `paths` **\[[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)>]** Array of paths
- `rules` **\[[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[RegExp](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/RegExp)>]** Filter rules
- `isValidRules` **\[[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)]** Flag that defines a way to filter paths.
If `true`, the path satisfying the rules will be included.
If `false`, the path satisfying the rules will be excluded.

Expand Down Expand Up @@ -205,48 +205,68 @@ const paths = filterPaths(paths, rules, isValidRules);
// ['/auth']
```

Returns **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)>** Array of paths.
Returns **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)>** Array of paths.

# createRoutes

Creates and returns an array of routes from the given object which
may be a JSX route, a plain object route, or an array of either.

**Parameters**

- `routes` **Route** React Router configuration.

**Examples**

```javascript
import { routesCreater as createRoutes } from 'react-router-sitemap';
import { routesParser as parseRoutes } from 'react-router-sitemap';

const routes = createRoutes(<Route path='/home'>);
const paths = parseRoutes(routes); // ['/home']
```

Returns **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)>**

# routesParser

Module for parsing the result of the `createRoutes(<Route>)` function.
from [react-router](https://www.npmjs.com/package/react-router) package.

**Parameters**

- `routes` **\[([Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array) \| [Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object))]** Result of execute function
- `routes` **\[([Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array) \| [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object))]** Result of execute function
`createRoutes(<Route>)` (optional, default `[]`)
- `basePath` **\[[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)]** Prefix for all paths (optional, default `''`)
- `basePath` **\[[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)]** Prefix for all paths (optional, default `''`)

**Examples**

```javascript
import { createRoutes } from 'react-router';
import { routesCreater as createRoutes } from 'react-router-sitemap';
import { routesParser as parseRoutes } from 'react-router-sitemap';

const routes = createRoutes(<Route path='/home'>);
const paths = parseRoutes(routes); // ['/home']
```

```javascript
import { createRoutes } from 'react-router';
import { routesCreater as createRoutes } from 'react-router-sitemap';
import { routesParser as parseRoutes } from 'react-router-sitemap';

const routes = createRoutes(<Route path='/home'>);
const prefix = '/prefix';
const paths = parseRoutes(routes, prefix); // ['/prefix/home']
```

Returns **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)>** Array of paths
Returns **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)>** Array of paths

# sitemapBuilder

Module for building a sitemap using an array of paths. Uses the [sitemap](https://www.npmjs.com/package/sitemap) package.

**Parameters**

- `hostname` **\[[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)]** The root name of your site
- `paths` **\[[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)>]** Array of paths
- `hostname` **\[[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)]** The root name of your site
- `paths` **\[[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)>]** Array of paths

**Examples**

Expand Down
3 changes: 2 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import fs from 'fs';
import path from 'path';

import sm from 'sitemap';
import { createRoutes } from 'react-router';

import createRoutes from './routes-creater';
import parseRoutes from './routes-parser';
import filterPaths from './paths-filter';
import applyParams from './params-applier';
Expand Down Expand Up @@ -158,6 +158,7 @@ class Sitemap {

export default Sitemap;

export { default as routesCreater } from './routes-creater';
export { default as routesParser } from './routes-parser';
export { default as pathsFilter } from './paths-filter';
export { default as paramsApplier } from './params-applier';
Expand Down
26 changes: 26 additions & 0 deletions lib/routes-creater/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { isReactChildren, createRoutesFromReactChildren } from './routeUtils';

/**
* @description Creates and returns an array of routes from the given object which
* may be a JSX route, a plain object route, or an array of either.
* @param {Route} routes - React Router configuration.
* @return {Array<String>}
*
* @example
* import { routesCreater as createRoutes } from 'react-router-sitemap';
* import { routesParser as parseRoutes } from 'react-router-sitemap';
*
* const routes = createRoutes(<Route path='/home'>);
* const paths = parseRoutes(routes); // ['/home']
*/
const createRoutes = routes => {
if (isReactChildren(routes)) {
routes = createRoutesFromReactChildren(routes);
} else if (routes && !Array.isArray(routes)) {
routes = [routes];
}

return routes;
};

export default createRoutes;
76 changes: 76 additions & 0 deletions lib/routes-creater/routeUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import React from 'react';

/**
* @description Use React method to test if is a valid React Element.
* @param {Object} object - Which to test if is valid React Element.
* @return {Boolean}
* @ignore
*/
const isValidChild = object => {
return object === null || React.isValidElement(object);
};

/**
* @param {Object|array}
* @return {Boolean}
* @ignore
*/
const isReactChildren = object => {
return isValidChild(object) || (Array.isArray(object) && object.every(isValidChild));
};

/**
* @description Creates and returns a routes object from the given ReactChildren. JSX
* provides a convenient way to visualize how routes in the hierarchy are
* nested.
* @param {ReactChildren} children - ReactChildren in JSX
* @return {Object} routes object
* @ignore
*/
const createRoutesFromReactChildren = children => {
const routes = [];

/**
* @param {Object} element - ReactChild
* @return {Object} route object
* @ignore
*/
const createRouteFromReactElement = element => {
const type = element.type;
const route = Object.assign({}, type.defaultProps, element.props);

if (route.children) {
const childRoutes = createRoutesFromReactChildren(route.children, route);

if (childRoutes.length) {
route.childRoutes = childRoutes;
}
delete route.children;
}

return route;
};

React.Children.forEach(children, function (element) {
if (React.isValidElement(element)) {
// Component classes may have a static create* method.
if (element.type.createRouteFromReactElement) {
const route = element.type.createRouteFromReactElement(element);

if (route) {
routes.push(route);
}
} else {
routes.push(createRouteFromReactElement(element));
}
}
});

return routes;
};

export {
isValidChild,
isReactChildren,
createRoutesFromReactChildren
};
5 changes: 2 additions & 3 deletions lib/routes-parser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,21 @@ import buildPath from './path-builder';

/**
* Module for parsing the result of the `createRoutes(<Route>)` function.
* from [react-router](https://www.npmjs.com/package/react-router) package.
* @module routesParser
* @param {Array|Object} [routes = []] Result of execute function
* `createRoutes(<Route>)`
* @param {String} [basePath = ''] Prefix for all paths
* @return {Array<String>} Array of paths
*
* @example
* import { createRoutes } from 'react-router';
* import { routesCreater as createRoutes } from 'react-router-sitemap';
* import { routesParser as parseRoutes } from 'react-router-sitemap';
*
* const routes = createRoutes(<Route path='/home'>);
* const paths = parseRoutes(routes); // ['/home']
*
* @example
* import { createRoutes } from 'react-router';
* import { routesCreater as createRoutes } from 'react-router-sitemap';
* import { routesParser as parseRoutes } from 'react-router-sitemap';
*
* const routes = createRoutes(<Route path='/home'>);
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-router-sitemap",
"version": "1.1.5",
"version": "1.2.0",
"description": "Module to generate a sitemap for react-router configuration",
"repository": {
"type": "git",
Expand All @@ -19,7 +19,7 @@
"test": "karma start ./config/karma.config.js",
"build": "npm run lint && npm run test && webpack --config ./config/build.config.js",
"example": "cd example && node ./sitemap-builder.js",
"documentation": "documentation build ./lib ./lib/routes-parser ./lib/paths-filter ./lib/params-applier ./lib/sitemap-builder -f md > api.md",
"documentation": "documentation build ./lib ./lib/routes-creater ./lib/routes-parser ./lib/paths-filter ./lib/params-applier ./lib/sitemap-builder -f md > api.md",
"prepublish": "npm run build"
},
"author": "Igor Uvarov <[email protected]> (http://kuflash.ru)",
Expand Down Expand Up @@ -58,7 +58,7 @@
},
"peerDependencies": {
"react": "^15.1.0 || ^16.0.0",
"react-router": "^2.3.0 || ^3.2.1"
"react-router": "^2.3.0 || ^3.2.1 || ^4.3.0"
},
"dependencies": {
"sitemap": "^1.6.0"
Expand Down
17 changes: 17 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,23 @@ export default (
</Route>
);
```
If you are using v4 `react-router`, your `router.jsx` might be:
```js
import React from 'react';
import { Switch, Route } from 'react-router';

export default (
// Switch is added in v4 react-router
<Switch>
<Route path='/' />
<Route path='/about' />
<Route path='/projects' />
<Route path='/contacts' />
<Route path='/auth' />
<Route /> // No-match case
</Switch>
);
```
And you need to create a script which will run from the command line or on the server.

_Please note that in this case you need a module 'babel-register' to work with the ES2105 syntax and `.jsx` format._
Expand Down

0 comments on commit a42a357

Please sign in to comment.