Skip to content

Commit 8f48f3a

Browse files
committed
Use input last modified in site map
Fixes #3251
1 parent 477604c commit 8f48f3a

File tree

2 files changed

+37
-20
lines changed

2 files changed

+37
-20
lines changed

news/changelog-1.3.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
- Render `page-footer` even when a Navbar isn't present ([#4053](https://github.com/quarto-dev/quarto-cli/issues/4053))
105105
- Don't treat links with no `href` as external when `link-external-icon` is enabled ([#3645](https://github.com/quarto-dev/quarto-cli/issues/3645))
106106
- Escape HTML from code cells that appears inline in search results ([#4404](https://github.com/quarto-dev/quarto-cli/issues/4404))
107+
- Use input last modified timestamp when updating sitemap ([#3251](https://github.com/quarto-dev/quarto-cli/issues/3251))
107108

108109
## Books
109110

src/project/types/website/website-sitemap.ts

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { websiteBaseurl } from "./website-config.ts";
2222
import { kDraft } from "../../../format/html/format-html-shared.ts";
2323
import { copyTo } from "../../../core/copy.ts";
2424
import {
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

Comments
 (0)