fbfb6190ab
* 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
19 lines
744 B
TypeScript
19 lines
744 B
TypeScript
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;
|
||
}
|