Skip to content

Commit

Permalink
chore: safer transforms
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Dec 28, 2023
1 parent 9a83b0b commit f5afb52
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/runtime/sitemap/urlset/normalise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,15 @@ export function normaliseDate(date: string | Date): string
export function normaliseDate(d: Date | string) {
// lastmod must adhere to W3C Datetime encoding rules
if (typeof d === 'string') {
// we may have a value like this "2023-12-21T13:49:27.963745", this needs to be converted to w3c datetime
// we may have milliseconds at the end with a dot prefix like ".963745", we should remove this
d = d.replace(/\.\d+$/, '')
console.log('match', d, d.match(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}$/))
// we may have a value like this "2023-12-21T13:49:27", this needs to be converted to w3c datetime
// accept if they are already in the right format, accept small format too such as "2023-12-21"
if (d.match(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}$/) || d.match(/^\d{4}-\d{2}-\d{2}$/))
if (d.match(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}$/) || d.match(/^\d{4}-\d{2}-\d{2}$/)) {
console.log('pass')
return d
}
// otherwise we need to parse it
d = new Date(d)
// check for invalid date
Expand Down
2 changes: 1 addition & 1 deletion test/integration/single/lastmod.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ describe('lastmod', () => {
</url>
<url>
<loc>https://nuxtseo.com/foo</loc>
<lastmod>2023-12-21T02:49:27+00:00</lastmod>
<lastmod>2023-12-21T13:49:27</lastmod>
</url>
<url>
<loc>https://nuxtseo.com/quux</loc>
Expand Down

0 comments on commit f5afb52

Please sign in to comment.