@@ -4,6 +4,7 @@ import convert from 'xml-js';
44// import logger from './logger';
55import { BASE_URL } from '../config' ;
66
7+ const newlineRegex = / ( \r \n | \n | \r ) / gm;
78const 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
0 commit comments