summaryrefslogtreecommitdiff
path: root/packages/astro-rss/src/util.ts
diff options
context:
space:
mode:
authorGravatar Ben Holmes <hey@bholmes.dev> 2022-05-03 18:26:13 -0400
committerGravatar GitHub <noreply@github.com> 2022-05-03 18:26:13 -0400
commitfbfb6190ab5da60a556a3d5c338c8237c376df84 (patch)
tree8a79a56685b2b2e03a240eb50d604b66efa1dcd2 /packages/astro-rss/src/util.ts
parente2a037be944d4c00b4b909b25574ebbc245cc720 (diff)
downloadastro-fbfb6190ab5da60a556a3d5c338c8237c376df84.tar.gz
astro-fbfb6190ab5da60a556a3d5c338c8237c376df84.tar.zst
astro-fbfb6190ab5da60a556a3d5c338c8237c376df84.zip
Feat: `@astrojs/rss` package! (#3271)
* feat: introduce @astrojs/rss package! * feat: add config "site" to env variable * docs: add @astrojs/rss readme * chore: changeset * fix: testing script * deps: add mocha, chai, chai-promises * tests: add rss test! * feat: add canonicalUrl arg * chore: remove console.log * fix: remove null check on env (breaks build) * docs: stray ` * chore: update error message to doc link * chore: remove getStylesheet * docs: update stylesheet reference
Diffstat (limited to 'packages/astro-rss/src/util.ts')
-rw-r--r--packages/astro-rss/src/util.ts19
1 files changed, 19 insertions, 0 deletions
diff --git a/packages/astro-rss/src/util.ts b/packages/astro-rss/src/util.ts
new file mode 100644
index 000000000..0dad6b239
--- /dev/null
+++ b/packages/astro-rss/src/util.ts
@@ -0,0 +1,19 @@
+import npath from 'path-browserify';
+
+/** Normalize URL to its canonical form */
+export function createCanonicalURL(url: string, 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 (!npath.extname(pathname)) pathname = pathname.replace(/(\/+)?$/, '/'); // add trailing slash if there’s no extension
+ pathname = pathname.replace(/\/+/g, '/'); // remove duplicate slashes (URL() won’t)
+ return new URL(pathname, base);
+}
+
+/** Check if a URL is already valid */
+export function isValidURL(url: string): boolean {
+ try {
+ new URL(url);
+ return true;
+ } catch (e) {}
+ return false;
+}