Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/server/sitemap.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import convert from 'xml-js';
// import logger from './logger';
import { BASE_URL } from '../config';

const newlineRegex = /(\r\n|\n|\r)/gm;
const xmlJSopts = { compact: false, ignoreComment: true, spaces: 2 };

// https://www.sitemaps.org/protocol.html
Expand All @@ -24,7 +25,8 @@ class Sitemap {
type: 'element',
name: 'urlset',
attributes: {
xmlns: 'http://www.sitemaps.org/schemas/sitemap/0.9'
'xmlns': 'http://www.sitemaps.org/schemas/sitemap/0.9',
'xmlns:image': 'http://www.google.com/schemas/sitemap-image/1.1'
},
elements: []
}
Expand Down Expand Up @@ -54,6 +56,15 @@ class Sitemap {
elements.push( elt );
};

const getImageElt = doc => {
const imageElt = eltFactory( 'image:image' );
const imageLocElt = eltFactory( 'image:loc', {}, `${BASE_URL}/api${doc.publicUrl}.png` );
const imageTitleElt = eltFactory( 'image:title', {}, _.get( doc, ['citation', 'title'] ) );
const imageCaptionElt = eltFactory( 'image:caption', {}, _.get( doc, 'text', '' ).replace( newlineRegex, ' ' ) );
[ imageLocElt, imageTitleElt, imageCaptionElt ].forEach( elt => appendChild( imageElt, elt ) );
return imageElt;
};

const urlset = getElementByName( this.sitemapJSON, 'urlset' );

this.docs.forEach( doc => {
Expand All @@ -62,8 +73,9 @@ class Sitemap {
const lastmod = eltFactory( 'lastmod', {}, `${doc.lastEditedDate}` );
const changefreq = eltFactory( 'changefreq', {}, this.changefreq );
const priority = eltFactory( 'priority', {}, this.priority );
const image = getImageElt( doc );

[ loc, lastmod, changefreq, priority ].forEach( elt => appendChild( url, elt ) );
[ loc, lastmod, changefreq, priority, image ].forEach( elt => appendChild( url, elt ) );
appendChild( urlset, url );
});

Expand Down
21 changes: 18 additions & 3 deletions test/sitemap/docs2Sitemap.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ describe('docs2Sitemap - Element: <urlset>', function(){

before( () => {
urls = etree.findall('./url');
docUrls = _.filter( urls, url =>{
url.findtext('./loc') !== `${BASE_URL}/`;
});
docUrls = _.filter( urls, url => url.findtext('./loc') !== `${BASE_URL}/` );
});

it('Should have the correct number of <url> children', () => {
Expand Down Expand Up @@ -68,6 +66,23 @@ describe('docs2Sitemap - Element: <urlset>', function(){
expect( hasBaseUrl ).to.be.true;
}); // homepage

//https://support.google.com/webmasters/answer/178636?hl=en&ref_topic=2370565
it('Should have the correct <image:image>', () => {
docUrls.forEach( url => {
const imageLocText = url.findtext('./image:image/image:loc');
const hasDocLoc = docsDataJSON.some( docJSON => imageLocText && imageLocText.includes( docJSON.id ) );
expect( hasDocLoc ).to.be.true;

const imageTitleText = url.findtext('./image:image/image:title');
const hasDocTitle = docsDataJSON.some( docJSON => docJSON.citation.title === imageTitleText );
expect( hasDocTitle ).to.be.true;

const imageCaptionText = url.findtext('./image:image/image:caption');
const hasDocCaption = docsDataJSON.some( docJSON => docJSON.text === imageCaptionText );
expect( hasDocCaption ).to.be.true;
});
}); // image:image

}); // url

}); // docs2Sitemap