1
0
mirror of https://github.com/dzeiocom/libs.git synced 2025-06-06 08:19:53 +00:00

Added easy-sitemap lib

Signed-off-by: Avior <florian.bouillon@delta-wings.net>
This commit is contained in:
Florian Bouillon 2021-01-20 12:30:32 +01:00
parent 9fd1cbde68
commit ba1f4f441a
Signed by: Florian Bouillon
GPG Key ID: 50BD648F12C86AB6
10 changed files with 248 additions and 0 deletions

View File

@ -0,0 +1,5 @@
module.exports = {
"parserOptions": {
"project": __dirname + "/tsconfig.json"
}
}

View File

@ -0,0 +1,13 @@
node_modules
src
test
.babelrc
.eslintrc.js
.gitignore
.npmignore
tsconfig.*
webpack.config.js
yarn-error.log
coverage
__tests__
jest.config.js

View File

@ -0,0 +1,18 @@
# Changelog
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased]()
## [1.0.0] - 2021-01-20
### Added
- The sitemap is ready !
[1.0.4]: https://github.com/dzeiocom/libs/releases/tag/%40dzeio%2Fobject-util%401.0.4
[1.0.2]: https://github.com/dzeiocom/libs/releases/tag/%40dzeio%2Fobject-util%401.0.2
[1.0.1]: https://github.com/dzeiocom/libs/releases/tag/%40dzeio%2Fobject-util%401.0.1
[1.0.0]: https://github.com/dzeiocom/libs/releases/tag/%40easy-sitemap%401.0.0

View File

@ -0,0 +1,48 @@
# URL Manager
A very easy to use sitemap generator
## Usage
- Import easy-sitemap
```typescript
import Sitemap from 'easy-sitemap'
// or
const Sitemap = require('easy-sitemap').default
```
- Initialize it
```typescript
// Create a new instance
const sitemap = new Sitemap('https://www.example.com') // initialize using your website base
// if you want to let the library manage the response object the line is this
const sitemap = new Sitemap('https://www.example.com', {
response: res
})
```
- add you're paths
```typescript
sitemap.addEntry('/path')
// you can also add optionnal elements
sitemap.addEntry('/path', {
// each one are optionnal and they don't all need to be added
changefreq: 'always', // webpage change freq
lastmod: new Date('2021-01-20'), // webpage lastmod Date
priority: 1 // crawler priority
})
```
- finally build it !
```typescript
// it will return a string containing the whole sitemap
// if you are letting the library manage the response Object it will return an empty string but the page will render !
const result = sitemap.build()
```

View File

@ -0,0 +1,11 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`Basic Sitemap Tests should not add changefreq if value is incorrect 1`] = `"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?><urlset xmlns=\\"http://www.sitemaps.org/schemas/sitemap/0.9\\"><url><loc>https://www.example.com/path</loc></url></urlset>"`;
exports[`Basic Sitemap Tests should not add priority when it is incorrect 1`] = `"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?><urlset xmlns=\\"http://www.sitemaps.org/schemas/sitemap/0.9\\"><url><loc>https://www.example.com/path</loc></url></urlset>"`;
exports[`Basic Sitemap Tests should return a basic sitemap 1`] = `"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?><urlset xmlns=\\"http://www.sitemaps.org/schemas/sitemap/0.9\\"><url><loc>https://www.example.com/path</loc></url><url><loc>https://www.example.com/</loc></url></urlset>"`;
exports[`Basic Sitemap Tests should return a sitemap 1`] = `"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?><urlset xmlns=\\"http://www.sitemaps.org/schemas/sitemap/0.9\\"><url><loc>https://www.example.com/path</loc><changefreq>always</changefreq><lastmod>2021-01-20T00:00:00.000Z</lastmod><priority>1</priority></url><url><loc>https://www.example.com/</loc></url></urlset>"`;
exports[`Basic Sitemap Tests should return an empty sitemap 1`] = `"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?><urlset xmlns=\\"http://www.sitemaps.org/schemas/sitemap/0.9\\"></urlset>"`;

View File

@ -0,0 +1,43 @@
/// <reference types="jest" />
import Sitemap from '../src/Sitemap'
describe('Basic Sitemap Tests', () => {
it('should return an empty sitemap', () => {
const sitemap = new Sitemap('https://www.example.com')
expect(sitemap.build()).toMatchSnapshot()
})
it('should return a basic sitemap', () => {
const sitemap = new Sitemap('https://www.example.com')
sitemap.addEntry('/path')
sitemap.addEntry('/')
expect(sitemap.build()).toMatchSnapshot()
})
it('should return a sitemap', () => {
const sitemap = new Sitemap('https://www.example.com')
sitemap.addEntry('/path', {
changefreq: 'always',
lastmod: new Date('2021-01-20'),
priority: 1
})
sitemap.addEntry('/')
expect(sitemap.build()).toMatchSnapshot()
})
it('should not add priority when it is incorrect', () => {
const sitemap = new Sitemap('https://www.example.com')
sitemap.addEntry('/path', {
// @ts-expect-error
priority: 255
})
expect(sitemap.build()).toMatchSnapshot()
})
it('should not add changefreq if value is incorrect', () => {
const sitemap = new Sitemap('https://www.example.com')
sitemap.addEntry('/path', {
// @ts-expect-error
changefreq: 'pouet'
})
expect(sitemap.build()).toMatchSnapshot()
})
})

View File

@ -0,0 +1 @@
module.exports = require('../../jest.base.json')

View File

@ -0,0 +1,30 @@
{
"name": "easy-sitemap",
"version": "1.0.0",
"description": "A very easy sitemap maker library",
"repository": {
"type": "git",
"url": "https://github.com/dzeiocom/libs.git",
"directory": "packages/easy-sitemap"
},
"author": "Aviortheking",
"license": "MIT",
"main": "./dist/Sitemap.js",
"types": "./dist/Sitemap.d.ts",
"devDependencies": {
"@types/chai": "^4.2.12",
"@types/jest": "^26.0.10",
"jest": "^26.4.2",
"ts-node": "^9.0.0",
"typescript": "^4.0.2"
},
"scripts": {
"prepublishOnly": "yarn build",
"build": "tsc",
"test": "jest --coverage"
},
"keywords": [
"sitemap",
"sitemap.xml"
]
}

View File

@ -0,0 +1,70 @@
import { ServerResponse } from 'http'
export default class Sitemap {
private static allowedChangefreq = ['always', 'hourly', 'daily', 'weekly', 'monthly', 'yearly', 'never']
private datas = '<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'
public constructor(
private domain: string, private options?: {
response?: ServerResponse
}
) {
if (this.options?.response) {
this.options.response.setHeader('Content-Type', 'text/xml')
this.options.response.write(this.datas)
}
}
public addEntry(path: string, options?: {
changefreq?: 'always' | 'hourly' | 'daily' | 'weekly' | 'monthly' | 'yearly' | 'never'
lastmod?: Date
priority?: 1 | 0.9 | 0.8 | 0.7 | 0.6 | 0.5 | 0.4 | 0.3 | 0.2 | 0.1 | 0
}) {
let entryString = '<url>'
const url = `${this.domain}${path}`
.replace(/&/g, '&amp;')
.replace(/'/g, '&apos;')
.replace(/"/g, '&quot;')
.replace(/>/g, '&gt;')
.replace(/</g, '&lt;')
entryString += `<loc>${url}</loc>`
if (options) {
if (options.changefreq) {
if (!Sitemap.allowedChangefreq.includes(options.changefreq)) {
console.warn(`changefreq is not one of the allowed words (${options.changefreq})\nChangeFreq won't be added`)
} else {
entryString += `<changefreq>${options.changefreq}</changefreq>`
}
}
if (options.lastmod) {
entryString += `<lastmod>${options.lastmod.toISOString()}</lastmod>`
}
if (options.priority) {
if (options.priority <= 1 && options.priority >= 0 && options.priority.toString().length <= 3) {
entryString += `<priority>${options.priority}</priority>`
} else {
console.warn(`Priority is not between 0 and 1 and only containing one decimal (${options.priority})\nPriority won't be added`)
}
}
}
entryString += '</url>'
if (this.options?.response) {
this.options.response.write(entryString)
} else {
this.datas += entryString
}
}
public build(): string {
if (this.options?.response) {
this.options.response.write('</urlset>')
this.options.response.end()
return ''
}
return this.datas + '</urlset>'
}
}

View File

@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"outDir": "dist"
},
"files": [
"src/Sitemap.ts"
]
}