react-router-sitemap-maker
Advanced tools
Comparing version 1.0.3 to 1.1.0
import type { ReactElement } from "react"; | ||
import ParseRoutes from './ParseRoutes'; | ||
import GetEndpoints from './GetEndpoints'; | ||
import SitemapData, { Options } from './SitemapData'; | ||
/** | ||
* Parses the routes of the input element and generates SitemapData | ||
* @param {ReactElement} routes A React element containing one or multiple elements | ||
* @param {Options} options The options with which the SitemapData will generate the output sitemap | ||
* @returns {SitemapData} An object containing all of the nessesairy data to generate the a functional sitemap | ||
*/ | ||
declare const GenerateSitemap: (routes: ReactElement, options: Options) => SitemapData; | ||
export { ParseRoutes, GenerateSitemap, SitemapData }; | ||
export { GetEndpoints, GetEndpoints as ParseRoutes, GenerateSitemap, SitemapData }; | ||
export default GenerateSitemap; |
@@ -6,11 +6,18 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.SitemapData = exports.GenerateSitemap = exports.ParseRoutes = void 0; | ||
const ParseRoutes_1 = __importDefault(require("./ParseRoutes")); | ||
exports.ParseRoutes = ParseRoutes_1.default; | ||
exports.SitemapData = exports.GenerateSitemap = exports.ParseRoutes = exports.GetEndpoints = void 0; | ||
const GetEndpoints_1 = __importDefault(require("./GetEndpoints")); | ||
exports.GetEndpoints = GetEndpoints_1.default; | ||
exports.ParseRoutes = GetEndpoints_1.default; | ||
const SitemapData_1 = __importDefault(require("./SitemapData")); | ||
exports.SitemapData = SitemapData_1.default; | ||
/** | ||
* Parses the routes of the input element and generates SitemapData | ||
* @param {ReactElement} routes A React element containing one or multiple elements | ||
* @param {Options} options The options with which the SitemapData will generate the output sitemap | ||
* @returns {SitemapData} An object containing all of the nessesairy data to generate the a functional sitemap | ||
*/ | ||
const GenerateSitemap = (routes, options) => { | ||
return new SitemapData_1.default((0, ParseRoutes_1.default)(routes), options); | ||
return new SitemapData_1.default((0, GetEndpoints_1.default)(routes), options); | ||
}; | ||
exports.GenerateSitemap = GenerateSitemap; | ||
exports.default = GenerateSitemap; |
@@ -11,13 +11,53 @@ import { PathLike } from 'fs'; | ||
declare class SitemapData { | ||
/** | ||
* The endpoints that need to be in the sitemap | ||
* @private | ||
*/ | ||
private Endpoints; | ||
/** | ||
* The routes that will end up being in the sitemap | ||
* @private | ||
*/ | ||
private Routes; | ||
/** | ||
* The options used to generate the sitemap | ||
* @private | ||
*/ | ||
private Options; | ||
constructor(endpoints: Array<string>, options: Options); | ||
/** @returns {Array<string>} The routes that will end up being in the sitemap */ | ||
getRoutes: () => Array<string>; | ||
/** @returns {Array<string>} The endpoints that need to be in the sitemap (used to generate the routes) */ | ||
getEndpoints: () => Array<string>; | ||
/** | ||
* Gets the routes and serializes them into JSON | ||
* | ||
* Note: This isn't an actual valid sitemap format | ||
* @returns {Promise<string>} A JSON string array containg all of the routes | ||
*/ | ||
toJSONString: () => Promise<string>; | ||
/** | ||
* Gets the routes and serializes them into an XML sitemap | ||
* | ||
* Note: This IS a valid sitemap format | ||
* @returns {Promise<string>} An XML string containg all of the routes | ||
*/ | ||
toXMLString: () => Promise<string>; | ||
/** | ||
* Gets the routes and serializes them into a plain text sitemap | ||
* | ||
* Note: This IS a valid sitemap format | ||
* @returns {Promise<string>} A plain text string containg all of the routes | ||
*/ | ||
toTextString: () => Promise<string>; | ||
toFile: (location: PathLike) => Promise<boolean>; | ||
/** | ||
* Writes the sitemap to the specified location. | ||
* Output is determined by the options defined when creating the SitemapData object. | ||
* | ||
* The default outputType is XML (most popular Sitemap format) | ||
* @param location The location the file should be writen to (example: "/public/sitemap.xml") | ||
* @returns {Promise<void>} A promise that will finish once the file is done writing | ||
*/ | ||
toFile: (location: PathLike) => Promise<void>; | ||
} | ||
export default SitemapData; |
@@ -20,4 +20,16 @@ "use strict"; | ||
class SitemapData { | ||
/** | ||
* The endpoints that need to be in the sitemap | ||
* @private | ||
*/ | ||
Endpoints; | ||
/** | ||
* The routes that will end up being in the sitemap | ||
* @private | ||
*/ | ||
Routes; | ||
/** | ||
* The options used to generate the sitemap | ||
* @private | ||
*/ | ||
Options; | ||
@@ -34,21 +46,43 @@ constructor(endpoints, options) { | ||
if (options.hashrouting) | ||
this.Routes.push(`${options.baseUrl}/`); | ||
this.Routes.unshift(`${options.baseUrl}/`); | ||
} | ||
/** @returns {Array<string>} The routes that will end up being in the sitemap */ | ||
getRoutes = () => this.Routes; | ||
/** @returns {Array<string>} The endpoints that need to be in the sitemap (used to generate the routes) */ | ||
getEndpoints = () => this.Endpoints; | ||
/** | ||
* Gets the routes and serializes them into JSON | ||
* | ||
* Note: This isn't an actual valid sitemap format | ||
* @returns {Promise<string>} A JSON string array containg all of the routes | ||
*/ | ||
toJSONString = async () => serializers.json(this.Routes, this.Options); | ||
/** | ||
* Gets the routes and serializes them into an XML sitemap | ||
* | ||
* Note: This IS a valid sitemap format | ||
* @returns {Promise<string>} An XML string containg all of the routes | ||
*/ | ||
toXMLString = async () => serializers.xml(this.Routes, this.Options); | ||
/** | ||
* Gets the routes and serializes them into a plain text sitemap | ||
* | ||
* Note: This IS a valid sitemap format | ||
* @returns {Promise<string>} A plain text string containg all of the routes | ||
*/ | ||
toTextString = async () => serializers.txt(this.Routes, this.Options); | ||
/** | ||
* Writes the sitemap to the specified location. | ||
* Output is determined by the options defined when creating the SitemapData object. | ||
* | ||
* The default outputType is XML (most popular Sitemap format) | ||
* @param location The location the file should be writen to (example: "/public/sitemap.xml") | ||
* @returns {Promise<void>} A promise that will finish once the file is done writing | ||
*/ | ||
toFile = async (location) => { | ||
const serializer = serializers[this.Options.outputType]; | ||
const sitemapContents = await serializer(this.Routes, this.Options); | ||
try { | ||
const file = await (0, promises_1.open)(location, "w"); | ||
await file.writeFile(sitemapContents); | ||
await file.close(); | ||
return true; | ||
} | ||
catch (error) { | ||
return false; | ||
} | ||
const file = await (0, promises_1.open)(location, "w"); | ||
await file.writeFile(sitemapContents); | ||
await file.close(); | ||
}; | ||
@@ -55,0 +89,0 @@ } |
{ | ||
"name": "react-router-sitemap-maker", | ||
"version": "1.0.3", | ||
"version": "1.1.0", | ||
"description": "Generates a sitemap from react-router-dom routes", | ||
@@ -5,0 +5,0 @@ "author": "RikThePixel", |
@@ -1,2 +0,75 @@ | ||
# react-router-sitemap | ||
Generates a sitemap from react-router-dom routes | ||
# react-router-sitemap-maker | ||
[![Testing](https://github.com/Rikthepixel/react-router-sitemap-maker/actions/workflows/test.yml/badge.svg?branch=main)](https://github.com/Rikthepixel/react-router-sitemap-maker/actions/workflows/test.yml) | ||
Generates a sitemap from react-router-dom routes. | ||
Available on NPM: | ||
[![NPM](https://nodei.co/npm/react-router-sitemap-maker.png)](https://www.npmjs.com/package/react-router-sitemap-maker) | ||
## Usage | ||
### Routes layout | ||
The sitemap generator expects a Routes element containing one or multiple Route elements in it. Like the following example: | ||
```javascript | ||
// src/routes.jsx | ||
import { Routes, Route } from "react-router-dom"; | ||
const MyRoutes = () => { | ||
return ( | ||
<Routes> | ||
<Route path="/" element={<AnyElement />}></Route> | ||
<Route index element={<AnyElement />} /> | ||
<Route path="Contact" element={<AnyElement />} /> | ||
<Route path="*" element={<AnyElement />} /> | ||
</Routes> | ||
); | ||
}; | ||
``` | ||
> _Note: The Routes cannot be wrapped in a HashRouter or a BrowserRouter, because this requires the DOM, thus the code cannot be ran server side._ | ||
### Creating the builder file | ||
To create the sitemap you need a builder file. | ||
You can possibly run this after you ran the actual build and put the sitemap in the **dist** or the **build** folder. | ||
Alternatively you could run it before the building process and put it in the **public** folder. | ||
```javascript | ||
// builders/sitemap.js | ||
import GenerateSitemap from "react-router-sitemap-maker"; | ||
import Routes from "../src/routes"; | ||
const sitemapData = await GenerateSitemap(Routes(), { | ||
baseUrl: "https://rikthepixel.github.io", | ||
hashrouting: true, | ||
changeFrequency: "monthly" | ||
}); | ||
sitemapData.toFile("./dist/sitemap.xml"); | ||
``` | ||
### Run the builder | ||
These are _some_ possible ways to run the sitemap builder file. | ||
#### **Using babel-node**: | ||
(Additional setup may be required to get babel-node to run JSX) | ||
```shell | ||
babel-node ./builders/sitemap.js | ||
``` | ||
#### **Using vite-node**: | ||
(vite-node should run JSX out of the box) | ||
```shell | ||
vite-node ./builders/sitemap.js | ||
``` |
19112
11
356
76