diff options
author | 2023-03-09 07:57:03 +0000 | |
---|---|---|
committer | 2023-03-09 07:57:03 +0000 | |
commit | 2e362042c222298fd6cd80a64c1d7b7f3f608a79 (patch) | |
tree | 0b335fe67285ca21d59b4df0610b9aafd52d9b9e /packages/astro-rss/src/util.ts | |
parent | 77a046e886c370b737208574b6934f5a1cf2b177 (diff) | |
download | astro-2e362042c222298fd6cd80a64c1d7b7f3f608a79.tar.gz astro-2e362042c222298fd6cd80a64c1d7b7f3f608a79.tar.zst astro-2e362042c222298fd6cd80a64c1d7b7f3f608a79.zip |
feat(rss): add option to remove the trailing slash (#6453)
* feat(rss): add option to remove the trailing slash
* Apply suggestions from code review
Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
* suggestions
---------
Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
Diffstat (limited to 'packages/astro-rss/src/util.ts')
-rw-r--r-- | packages/astro-rss/src/util.ts | 16 |
1 files changed, 14 insertions, 2 deletions
diff --git a/packages/astro-rss/src/util.ts b/packages/astro-rss/src/util.ts index ad0e40a68..a87828bfc 100644 --- a/packages/astro-rss/src/util.ts +++ b/packages/astro-rss/src/util.ts @@ -1,10 +1,22 @@ import { z } from 'astro/zod'; +import { RSSOptions } from './index'; /** Normalize URL to its canonical form */ -export function createCanonicalURL(url: string, base?: string): URL { +export function createCanonicalURL( + url: string, + trailingSlash?: RSSOptions['trailingSlash'], + base?: string +): URL { let pathname = url.replace(/\/index.html$/, ''); // index.html is not canonical pathname = pathname.replace(/\/1\/?$/, ''); // neither is a trailing /1/ (impl. detail of collections) - if (!getUrlExtension(url)) pathname = pathname.replace(/(\/+)?$/, '/'); // add trailing slash if there’s no extension + if (trailingSlash === false) { + // remove the trailing slash + pathname = pathname.replace(/(\/+)?$/, ''); + } else if (!getUrlExtension(url)) { + // add trailing slash if there’s no extension or `trailingSlash` is true + pathname = pathname.replace(/(\/+)?$/, '/'); + } + pathname = pathname.replace(/\/+/g, '/'); // remove duplicate slashes (URL() won’t) return new URL(pathname, base); } |