11const fs = require ( 'fs' )
2- const util = require ( 'util' )
32const path = require ( 'path' )
3+ const util = require ( 'util' )
4+
5+ const globby = require ( 'globby' )
46const mkdirp = require ( 'mkdirp' )
57const sm = require ( 'sitemap' )
6- const globby = require ( 'globby' )
78
8- module . exports = async function makeSitemap ( opts = { } ) {
9- const { distPath, fileName, homepage, exclude, prettyURLs, trailingSlash, failBuild } = opts
9+ const getPaths = async ( { distPath, exclude } ) => {
1010 const htmlFiles = `${ distPath } /**/**.html`
11- const excludeFiles = ( exclude || [ ] ) . map ( ( filePath ) => {
12- return `!${ filePath . replace ( / ^ ! / , '' ) } `
13- } )
11+ const excludeFiles = ( exclude || [ ] ) . map ( ( filePath ) => `!${ filePath . replace ( / ^ ! / , '' ) } ` )
1412
15- const lookup = [ htmlFiles ] . concat ( excludeFiles )
13+ const lookup = [ htmlFiles ] . concat ( excludeFiles )
1614 const paths = await globby ( lookup )
17- const urls = paths . map ( file => {
18- let urlPath = file . startsWith ( distPath ) ? file . replace ( distPath , '' ) : distPath
19- if ( prettyURLs ) {
20- urlPath = urlPath . replace ( / \/ i n d e x \. h t m l $ / , '' ) . replace ( / \. h t m l $ / , '' )
21-
22- if ( trailingSlash ) {
23- urlPath += urlPath . endsWith ( "/" ) ? "" : "/" ;
24- }
25- }
15+ return paths
16+ }
17+
18+ const getUrlFromFile = ( { file, distPath, prettyURLs, trailingSlash } ) => {
19+ const url = file . startsWith ( distPath ) ? file . replace ( distPath , '' ) : distPath
2620
21+ if ( ! prettyURLs ) {
22+ return url
23+ }
24+
25+ const prettyUrl = url . replace ( / \/ i n d e x \. h t m l $ / , '' ) . replace ( / \. h t m l $ / , '' )
26+
27+ if ( ! trailingSlash ) {
28+ return prettyUrl
29+ }
30+
31+ return prettyUrl . endsWith ( '/' ) ? prettyUrl : `${ prettyUrl } /`
32+ }
33+
34+ const DEFAULT_CHANGE_FREQ = 'weekly'
35+ const DEFAULT_PRIORITY = 0.8
36+ // 600 sec cache period
37+ const DEFAULT_CACHE_TIME = 600000
38+
39+ module . exports = async function makeSitemap ( opts = { } ) {
40+ const { distPath, fileName, homepage, exclude, prettyURLs, trailingSlash, failBuild } = opts
41+ const paths = await getPaths ( { distPath, exclude } )
42+ const urls = paths . map ( ( file ) => {
43+ const url = getUrlFromFile ( { file, distPath, prettyURLs, trailingSlash } )
2744 return {
28- url : urlPath ,
29- changefreq : opts . changeFreq || 'weekly' ,
30- priority : opts . priority || 0.8 ,
45+ url,
46+ changefreq : opts . changeFreq || DEFAULT_CHANGE_FREQ ,
47+ priority : opts . priority || DEFAULT_PRIORITY ,
3148 lastmodrealtime : true ,
3249 lastmodfile : file ,
3350 }
3451 } )
3552 const options = {
3653 hostname : `${ homepage . replace ( / \/ $ / , '' ) } /` ,
37- cacheTime : 600000 , // 600 sec cache period
54+ cacheTime : DEFAULT_CACHE_TIME ,
3855 urls,
3956 }
4057 // Creates a sitemap object given the input configuration with URLs
@@ -57,6 +74,6 @@ module.exports = async function makeSitemap(opts = {}) {
5774 // Return info
5875 return {
5976 sitemapPath : sitemapFileName ,
60- sitemap : sitemap
77+ sitemap,
6178 }
6279}
0 commit comments