1
0
mirror of https://github.com/dzeiocom/libs.git synced 2025-04-23 11:22:15 +00:00

Added Support for image:image in easy-sitemap

Signed-off-by: Florian BOUILLON <florian.bouillon@delta-wings.net>
This commit is contained in:
Florian Bouillon 2021-03-16 11:10:53 +01:00
parent 0948650263
commit 4f4b756fb8
4 changed files with 114 additions and 10 deletions

View File

@ -1,11 +1,19 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP // 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 changefreq if value is incorrect 1`] = `"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?><urlset xmlns=\\"http://www.sitemaps.org/schemas/sitemap/0.9\\" image:xmlns=\\"http://www.google.com/schemas/sitemap-image/1.1\\"><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 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\\" image:xmlns=\\"http://www.google.com/schemas/sitemap-image/1.1\\"><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 basic sitemap 1`] = `"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?><urlset xmlns=\\"http://www.sitemaps.org/schemas/sitemap/0.9\\" image:xmlns=\\"http://www.google.com/schemas/sitemap-image/1.1\\"><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 a sitemap 1`] = `"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?><urlset xmlns=\\"http://www.sitemaps.org/schemas/sitemap/0.9\\" image:xmlns=\\"http://www.google.com/schemas/sitemap-image/1.1\\"><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>"`; 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\\" image:xmlns=\\"http://www.google.com/schemas/sitemap-image/1.1\\"></urlset>"`;
exports[`image:image tests should build corretcly with multiple image:image 1`] = `"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?><urlset xmlns=\\"http://www.sitemaps.org/schemas/sitemap/0.9\\" image:xmlns=\\"http://www.google.com/schemas/sitemap-image/1.1\\"><url><loc>https://www.example.com/path</loc><image:image><image:loc>https://www.example.com/test</image:loc><image:caption>Image Caption</image:caption><image:geo_location>Nantes</image:geo_location><image:title>Title</image:title><image:license>Example license url</image:license></image:image><image:image><image:loc>https://www.example.com/test-2</image:loc><image:caption>Image Caption2</image:caption><image:geo_location>Paris</image:geo_location><image:title>Title2</image:title><image:license>Example license url</image:license></image:image></url></urlset>"`;
exports[`image:image tests should build corretcly with single image:image 1`] = `"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?><urlset xmlns=\\"http://www.sitemaps.org/schemas/sitemap/0.9\\" image:xmlns=\\"http://www.google.com/schemas/sitemap-image/1.1\\"><url><loc>https://www.example.com/path</loc><image:image><image:loc>https://www.example.com/test</image:loc><image:caption>Image Caption</image:caption><image:geo_location>Nantes</image:geo_location><image:title>Title</image:title><image:license>Example license url</image:license></image:image></url></urlset>"`;
exports[`image:image tests should skip image:image if no location is set 1`] = `"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?><urlset xmlns=\\"http://www.sitemaps.org/schemas/sitemap/0.9\\" image:xmlns=\\"http://www.google.com/schemas/sitemap-image/1.1\\"><url><loc>https://www.example.com/path</loc></url></urlset>"`;
exports[`image:image tests should skip image:image if there is more than 1000 images 1`] = `"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?><urlset xmlns=\\"http://www.sitemaps.org/schemas/sitemap/0.9\\" image:xmlns=\\"http://www.google.com/schemas/sitemap-image/1.1\\"><url><loc>https://www.example.com/path</loc></url></urlset>"`;

View File

@ -39,5 +39,58 @@ describe('Basic Sitemap Tests', () => {
}) })
expect(sitemap.build()).toMatchSnapshot() expect(sitemap.build()).toMatchSnapshot()
}) })
})
describe('image:image tests', () => {
it('should build corretcly with single image:image', () => {
const sitemap = new Sitemap('https://www.example.com')
sitemap.addEntry('/path', {
images: [{
location: '/test',
geoLocation: 'Nantes',
title: 'Title',
caption: 'Image Caption',
license: 'Example license url'
}]
})
expect(sitemap.build()).toMatchSnapshot()
})
it('should skip image:image if no location is set', () => {
const sitemap = new Sitemap('https://www.example.com')
sitemap.addEntry('/path', {
// @ts-expect-error
images: [{
geoLocation: 'Nantes',
}]
})
expect(sitemap.build()).toMatchSnapshot()
})
it('should skip image:image if there is more than 1000 images', () => {
const sitemap = new Sitemap('https://www.example.com')
sitemap.addEntry('/path', {
images: Array.from(new Array(1001)).map(() => ({
location: '/test',
}))
})
expect(sitemap.build()).toMatchSnapshot()
})
it('should build corretcly with multiple image:image', () => {
const sitemap = new Sitemap('https://www.example.com')
sitemap.addEntry('/path', {
images: [{
location: '/test',
geoLocation: 'Nantes',
title: 'Title',
caption: 'Image Caption',
license: 'Example license url'
}, {
location: '/test-2',
geoLocation: 'Paris',
title: 'Title2',
caption: 'Image Caption2',
license: 'Example license url'
}]
})
expect(sitemap.build()).toMatchSnapshot()
})
}) })

View File

@ -25,6 +25,11 @@
}, },
"keywords": [ "keywords": [
"sitemap", "sitemap",
"sitemap.xml" "sitemap.xml",
"xml",
"easy-sitemap",
"generator",
"SEO",
"search-engine"
] ]
} }

View File

@ -4,7 +4,7 @@ export default class Sitemap {
private static allowedChangefreq = ['always', 'hourly', 'daily', 'weekly', 'monthly', 'yearly', 'never'] 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">' private datas = '<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" image:xmlns="http://www.google.com/schemas/sitemap-image/1.1">'
public constructor( public constructor(
private domain: string, private options?: { private domain: string, private options?: {
@ -17,10 +17,22 @@ export default class Sitemap {
} }
} }
/**
* Add a new Entry into the Sitemap
* @param path the url path
* @param options aditional datas you want in the sitemap for the `path`
*/
public addEntry(path: string, options?: { public addEntry(path: string, options?: {
changefreq?: 'always' | 'hourly' | 'daily' | 'weekly' | 'monthly' | 'yearly' | 'never' changefreq?: 'always' | 'hourly' | 'daily' | 'weekly' | 'monthly' | 'yearly' | 'never'
lastmod?: Date lastmod?: Date
priority?: 1 | 0.9 | 0.8 | 0.7 | 0.6 | 0.5 | 0.4 | 0.3 | 0.2 | 0.1 | 0 priority?: 1 | 0.9 | 0.8 | 0.7 | 0.6 | 0.5 | 0.4 | 0.3 | 0.2 | 0.1 | 0
images?: Array<{
location: string
caption?: string
geoLocation?: string
title?: string
license?: string
}>
}) { }) {
let entryString = '<url>' let entryString = '<url>'
@ -35,7 +47,7 @@ export default class Sitemap {
if (options) { if (options) {
if (options.changefreq) { if (options.changefreq) {
if (!Sitemap.allowedChangefreq.includes(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`) console.warn(`changefreq is not valid (${options.changefreq})`)
} else { } else {
entryString += `<changefreq>${options.changefreq}</changefreq>` entryString += `<changefreq>${options.changefreq}</changefreq>`
} }
@ -47,7 +59,26 @@ export default class Sitemap {
if (options.priority <= 1 && options.priority >= 0 && options.priority.toString().length <= 3) { if (options.priority <= 1 && options.priority >= 0 && options.priority.toString().length <= 3) {
entryString += `<priority>${options.priority}</priority>` entryString += `<priority>${options.priority}</priority>`
} else { } else {
console.warn(`Priority is not between 0 and 1 and only containing one decimal (${options.priority})\nPriority won't be added`) console.warn(`Priority is not valid (${options.priority})`)
}
}
if (options.images) {
if (options.images.length > 1000) {
console.warn('image cant have more than 1000 images, skipping')
} else {
for (const image of options.images) {
if (!image.location) {
console.warn('Images need a Location')
continue
}
entryString += '<image:image>'
entryString += `<image:loc>${image.location.startsWith('/') ? `${this.domain}${image.location}` : image.location}</image:loc>`
entryString += this.optionalEntry('image:caption', image.caption)
entryString += this.optionalEntry('image:geo_location', image.geoLocation)
entryString += this.optionalEntry('image:title', image.title)
entryString += this.optionalEntry('image:license', image.license)
entryString += '</image:image>'
}
} }
} }
} }
@ -59,6 +90,9 @@ export default class Sitemap {
} }
} }
/**
* Finish the Sitemap
*/
public build(): string { public build(): string {
if (this.options?.response) { if (this.options?.response) {
this.options.response.write('</urlset>') this.options.response.write('</urlset>')
@ -67,4 +101,8 @@ export default class Sitemap {
} }
return this.datas + '</urlset>' return this.datas + '</urlset>'
} }
private optionalEntry(tag: string, entry?: string) {
return entry ? `<${tag}>${entry}</${tag}>` : ''
}
} }