@@ -22,6 +22,7 @@ import { websiteBaseurl } from "./website-config.ts";
2222import { kDraft } from "../../../format/html/format-html-shared.ts" ;
2323import { copyTo } from "../../../core/copy.ts" ;
2424import {
25+ inputFileForOutputFile ,
2526 inputTargetIndexForOutputFile ,
2627 inputTargetIsEmpty ,
2728} from "../../project-index.ts" ;
@@ -74,35 +75,50 @@ export async function updateSitemap(
7475 const fileLastMod = ( file : string ) =>
7576 ( Deno . statSync ( file ) . mtime || new Date ( 0 ) )
7677 . toISOString ( ) ;
77- const urlsetEntry = ( outputFile : ProjectOutputFile ) => {
78+
79+ const inputModified = async ( output : string , project : ProjectContext ) => {
80+ const inputFile = await inputFileForOutputFile ( project , output ) ;
81+ if ( inputFile ) {
82+ return fileLastMod ( inputFile ) ;
83+ } else {
84+ return fileLastMod ( output ) ;
85+ }
86+ } ;
87+
88+ const urlsetEntry = async ( outputFile : ProjectOutputFile ) => {
7889 const file = outputFile . file ;
7990 const isDraft = ! ! outputFile . format . metadata [ kDraft ] ;
8091
81- return { loc : fileLoc ( file ) , lastmod : fileLastMod ( file ) , draft : isDraft } ;
92+ return {
93+ loc : fileLoc ( file ) ,
94+ lastmod : await inputModified ( file , context ) ,
95+ draft : isDraft ,
96+ } ;
8297 } ;
8398
8499 // full render or no existing sitemap creates a fresh sitemap.xml
85100 if ( ! incremental || ! existsSync ( sitemapPath ) ) {
86101 // write sitemap
87- writeSitemap ( sitemapPath , outputFiles . map ( urlsetEntry ) ) ;
88- } else { // otherwise parse the sitemap, update and write a new one
89- const urlset = outputFiles . reduce (
90- ( urlset : Urlset , outputFile : ProjectOutputFile ) => {
91- const file = outputFile . file ;
92- const loc = fileLoc ( file ) ;
93- const url = urlset . find ( ( url ) => url . loc === loc ) ;
94-
95- if ( url ) {
96- url . lastmod = fileLastMod ( file ) ;
97- url . draft = ! ! outputFile . format . metadata [ kDraft ] ;
98- } else {
99- urlset . push ( urlsetEntry ( outputFile ) ) ;
100- }
101- return urlset ;
102- } ,
103- await readSitemap ( sitemapPath ) ,
104- ) ;
102+ const urlset : Urlset = [ ] ;
103+ for ( const file of outputFiles ) {
104+ urlset . push ( await urlsetEntry ( file ) ) ;
105+ }
105106 writeSitemap ( sitemapPath , urlset ) ;
107+ } else { // otherwise parse the sitemap, update and write a new one
108+ const urlSet : Urlset = await readSitemap ( sitemapPath ) ;
109+ for ( const outputFile of outputFiles ) {
110+ const file = outputFile . file ;
111+ const loc = fileLoc ( file ) ;
112+ const url = urlSet . find ( ( url ) => url . loc === loc ) ;
113+
114+ if ( url ) {
115+ url . lastmod = await inputModified ( file , context ) ;
116+ url . draft = ! ! outputFile . format . metadata [ kDraft ] ;
117+ } else {
118+ urlSet . push ( await urlsetEntry ( outputFile ) ) ;
119+ }
120+ }
121+ writeSitemap ( sitemapPath , urlSet ) ;
106122 }
107123
108124 // create robots.txt if necessary
0 commit comments