Skip to content

Commit 4992a35

Browse files
authored
Merge pull request #880 from PathwayCommons/iss842_sitemap-images
Supplement the sitemap urls with PNG image URL and metadata
2 parents 5733c06 + 104f9c2 commit 4992a35

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

src/server/sitemap.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import convert from 'xml-js';
44
// import logger from './logger';
55
import { BASE_URL } from '../config';
66

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

910
// https://www.sitemaps.org/protocol.html
@@ -24,7 +25,8 @@ class Sitemap {
2425
type: 'element',
2526
name: 'urlset',
2627
attributes: {
27-
xmlns: 'http://www.sitemaps.org/schemas/sitemap/0.9'
28+
'xmlns': 'http://www.sitemaps.org/schemas/sitemap/0.9',
29+
'xmlns:image': 'http://www.google.com/schemas/sitemap-image/1.1'
2830
},
2931
elements: []
3032
}
@@ -54,6 +56,15 @@ class Sitemap {
5456
elements.push( elt );
5557
};
5658

59+
const getImageElt = doc => {
60+
const imageElt = eltFactory( 'image:image' );
61+
const imageLocElt = eltFactory( 'image:loc', {}, `${BASE_URL}/api${doc.publicUrl}.png` );
62+
const imageTitleElt = eltFactory( 'image:title', {}, _.get( doc, ['citation', 'title'] ) );
63+
const imageCaptionElt = eltFactory( 'image:caption', {}, _.get( doc, 'text', '' ).replace( newlineRegex, ' ' ) );
64+
[ imageLocElt, imageTitleElt, imageCaptionElt ].forEach( elt => appendChild( imageElt, elt ) );
65+
return imageElt;
66+
};
67+
5768
const urlset = getElementByName( this.sitemapJSON, 'urlset' );
5869

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

66-
[ loc, lastmod, changefreq, priority ].forEach( elt => appendChild( url, elt ) );
78+
[ loc, lastmod, changefreq, priority, image ].forEach( elt => appendChild( url, elt ) );
6779
appendChild( urlset, url );
6880
});
6981

test/sitemap/docs2Sitemap.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ describe('docs2Sitemap - Element: <urlset>', function(){
3333

3434
before( () => {
3535
urls = etree.findall('./url');
36-
docUrls = _.filter( urls, url =>{
37-
url.findtext('./loc') !== `${BASE_URL}/`;
38-
});
36+
docUrls = _.filter( urls, url => url.findtext('./loc') !== `${BASE_URL}/` );
3937
});
4038

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

69+
//https://support.google.com/webmasters/answer/178636?hl=en&ref_topic=2370565
70+
it('Should have the correct <image:image>', () => {
71+
docUrls.forEach( url => {
72+
const imageLocText = url.findtext('./image:image/image:loc');
73+
const hasDocLoc = docsDataJSON.some( docJSON => imageLocText && imageLocText.includes( docJSON.id ) );
74+
expect( hasDocLoc ).to.be.true;
75+
76+
const imageTitleText = url.findtext('./image:image/image:title');
77+
const hasDocTitle = docsDataJSON.some( docJSON => docJSON.citation.title === imageTitleText );
78+
expect( hasDocTitle ).to.be.true;
79+
80+
const imageCaptionText = url.findtext('./image:image/image:caption');
81+
const hasDocCaption = docsDataJSON.some( docJSON => docJSON.text === imageCaptionText );
82+
expect( hasDocCaption ).to.be.true;
83+
});
84+
}); // image:image
85+
7186
}); // url
7287

7388
}); // docs2Sitemap

0 commit comments

Comments
 (0)