react-router-sitemap-generator
Advanced tools
Comparing version 0.0.7 to 0.0.8
@@ -12,2 +12,8 @@ "use strict"; | ||
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; } | ||
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); } | ||
@@ -31,4 +37,2 @@ | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
var fs = require('fs'); | ||
@@ -38,89 +42,26 @@ | ||
var DEFAULT_CONFIG = { | ||
lastmod: new Date().toISOString().slice(0, 10), | ||
changefreq: 'monthly', | ||
priority: 0.8 | ||
}; | ||
var Generator = /*#__PURE__*/function () { | ||
function Generator(baseUrl, baseComponent) { | ||
function Generator(baseUrl, baseComponent, config) { | ||
_classCallCheck(this, Generator); | ||
_defineProperty(this, "_paths", []); | ||
if (! /*#__PURE__*/_react["default"].isValidElement(baseComponent)) { | ||
throw 'Invalid component. Try `Router()` instead of `Router`'; | ||
} | ||
this._baseUrl = baseUrl; | ||
this._baseComponent = baseComponent; | ||
this._config = config; | ||
} | ||
_createClass(Generator, [{ | ||
key: "_generate", | ||
value: function _generate() { | ||
this._paths = []; | ||
var components = []; | ||
components.push(this._baseComponent()); | ||
while (components.length !== 0) { | ||
var component = components.pop(); | ||
var props = component.props; | ||
if (props != null) { | ||
var nestedComponent = props.children, | ||
path = props.path, | ||
propsComponents = props.component; | ||
if (path != null) { | ||
this._paths.push(path); | ||
} | ||
components.push.apply(components, _toConsumableArray(this._getComponents(nestedComponent))); | ||
components.push.apply(components, _toConsumableArray(this._getComponents(propsComponents === null || propsComponents === void 0 ? void 0 : propsComponents({ | ||
match: { | ||
url: path | ||
} | ||
})))); | ||
} | ||
} | ||
} | ||
}, { | ||
key: "_getComponents", | ||
value: function _getComponents(components) { | ||
var _components = []; | ||
if (Array.isArray(components)) { | ||
components === null || components === void 0 ? void 0 : components.forEach(function (child) { | ||
_components.push(child); | ||
}); | ||
} else if (components != null) { | ||
_components.push(components); | ||
} | ||
return _components; | ||
} | ||
}, { | ||
key: "getXML", | ||
value: function getXML() { | ||
var _this = this; | ||
this._generate(); | ||
var options = { | ||
compact: true, | ||
spaces: 4 | ||
}; | ||
var map = { | ||
_declaration: { | ||
_attributes: { | ||
version: '1.0', | ||
encoding: 'UTF-8' | ||
} | ||
}, | ||
urlset: { | ||
url: this._paths.map(function (path) { | ||
return { | ||
loc: _this._baseUrl + path, | ||
lastmod: '2021-01-01', | ||
changefreq: 'monthly', | ||
priority: 0.8 | ||
}; | ||
}), | ||
_attributes: { | ||
xmlns: 'http://www.sitemaps.org/schemas/sitemap/0.9' | ||
} | ||
} | ||
}; | ||
return convertor.js2xml(map, options); | ||
var paths = componentToPaths(this._baseComponent); | ||
return pathsToXml(this._baseUrl, paths, this._config); | ||
} | ||
@@ -130,3 +71,4 @@ }, { | ||
value: function save(path) { | ||
var xml = this.getXML(); | ||
var paths = componentToPaths(this._baseComponent); | ||
var xml = pathsToXml(this._baseUrl, paths, this._config); | ||
fs.writeFileSync(path, xml); | ||
@@ -139,2 +81,84 @@ } | ||
exports["default"] = Generator; | ||
exports["default"] = Generator; | ||
function componentToPaths(_baseComponent) { | ||
var paths = []; | ||
var components = [_baseComponent]; | ||
while (components.length !== 0) { | ||
var component = components.pop(); | ||
if (! /*#__PURE__*/_react["default"].isValidElement(component)) continue; | ||
var props = component.props; | ||
if (props == null) continue; | ||
var path = props.path, | ||
propsComponents = props.component; | ||
_react["default"].Children.forEach(component.props.children, function (child) { | ||
components.push.apply(components, _toConsumableArray(getComponents(child))); | ||
}); | ||
if (component.type.name === 'Route') { | ||
if (path != null) { | ||
paths.push(path); | ||
} | ||
if (typeof propsComponents === 'function') { | ||
components.push.apply(components, _toConsumableArray(getComponents(propsComponents({ | ||
match: { | ||
url: path | ||
} | ||
})))); | ||
} | ||
} | ||
} | ||
return paths; | ||
} | ||
function getComponents(components) { | ||
var _components = []; | ||
if (Array.isArray(components)) { | ||
components === null || components === void 0 ? void 0 : components.forEach(function (child) { | ||
_components.push(child); | ||
}); | ||
} else if (components != null) { | ||
_components.push(components); | ||
} | ||
return _components; | ||
} | ||
function pathsToXml(baseUrl, paths, config) { | ||
var _DEFAULT_CONFIG$confi = _objectSpread(_objectSpread({}, DEFAULT_CONFIG), config), | ||
lastmod = _DEFAULT_CONFIG$confi.lastmod, | ||
changefreq = _DEFAULT_CONFIG$confi.changefreq, | ||
priority = _DEFAULT_CONFIG$confi.priority; | ||
var options = { | ||
compact: true, | ||
spaces: 4 | ||
}; | ||
var map = { | ||
_declaration: { | ||
_attributes: { | ||
version: '1.0', | ||
encoding: 'UTF-8' | ||
} | ||
}, | ||
urlset: { | ||
url: paths.map(function (path) { | ||
return { | ||
loc: baseUrl + path, | ||
lastmod: lastmod, | ||
changefreq: changefreq, | ||
priority: priority | ||
}; | ||
}), | ||
_attributes: { | ||
xmlns: 'http://www.sitemaps.org/schemas/sitemap/0.9' | ||
} | ||
} | ||
}; | ||
return convertor.js2xml(map, options); | ||
} |
{ | ||
"name": "react-router-sitemap-generator", | ||
"version": "0.0.7", | ||
"version": "0.0.8", | ||
"main": "dist/index.js", | ||
@@ -12,5 +12,6 @@ "files": [ | ||
"scripts": { | ||
"lint": "eslint . --fix && prettier . --write", | ||
"lint": "eslint . --fix && prettier . --write && flow", | ||
"test": "jest", | ||
"build": "babel src --out-dir dist" | ||
"build": "babel src --out-dir dist", | ||
"flow-typed": "flow-typed install -o -s && flow-typed install jest@26" | ||
}, | ||
@@ -22,2 +23,3 @@ "devDependencies": { | ||
"@babel/preset-env": "^7.14.1", | ||
"@babel/preset-flow": "^7.13.13", | ||
"@babel/preset-react": "^7.13.13", | ||
@@ -27,2 +29,3 @@ "babel-eslint": "^10.1.0", | ||
"eslint-config-prettier": "^8.3.0", | ||
"eslint-plugin-flowtype": "^5.7.2", | ||
"eslint-plugin-import": "^2.22.1", | ||
@@ -33,2 +36,4 @@ "eslint-plugin-jsx-a11y": "^6.4.1", | ||
"eslint-plugin-react-hooks": "^4.2.0", | ||
"flow-bin": "^0.150.1", | ||
"flow-typed": "^3.3.1", | ||
"jest": "^26.6.3", | ||
@@ -42,3 +47,6 @@ "prettier": "^2.2.1", | ||
"xml-js": "^1.6.11" | ||
}, | ||
"peerDependencies": { | ||
"react": "*" | ||
} | ||
} |
@@ -41,3 +41,8 @@ # react-router-sitemap-generator | ||
'https://react-router-sitemap-generator.com', | ||
Router | ||
Router(), | ||
{ | ||
lastmod: new Date().toISOString().slice(0, 10), | ||
changefreq: 'monthly', | ||
priority: 0.8, | ||
} | ||
); | ||
@@ -44,0 +49,0 @@ generator.save('public/sitemap.xml'); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9214
129
60
3
21