summaryrefslogtreecommitdiff
path: root/packages/integrations/sitemap/src/utils/parse-url.ts
diff options
context:
space:
mode:
authorGravatar Oleksii Tymoshenko <alextim@users.noreply.github.com> 2022-06-16 22:06:48 +0300
committerGravatar GitHub <noreply@github.com> 2022-06-16 19:06:48 +0000
commit1031c06f9c6794d9ee6fb18c145ca5614e6f0583 (patch)
tree79e555ae82aef24f7838bc0e747ef6340941d68a /packages/integrations/sitemap/src/utils/parse-url.ts
parent44ba4e1ed9926f66795b8950c1464488b8756b2f (diff)
downloadastro-1031c06f9c6794d9ee6fb18c145ca5614e6f0583.tar.gz
astro-1031c06f9c6794d9ee6fb18c145ca5614e6f0583.tar.zst
astro-1031c06f9c6794d9ee6fb18c145ca5614e6f0583.zip
feat: improved sitemap (#3579)
* feat: extended sitemap functionality * docs: del samples * docs: readme * feat: new sitemap * feat: createLinkInHead removed * docs: updated changeset text * refactor: 'zod' function() instead of self made refine() * Revert "refactor: 'zod' function() instead of self made refine()" This reverts commit 036bac730d235b96b218e4d8c97527fa922b044f. undo function()
Diffstat (limited to 'packages/integrations/sitemap/src/utils/parse-url.ts')
-rw-r--r--packages/integrations/sitemap/src/utils/parse-url.ts39
1 files changed, 39 insertions, 0 deletions
diff --git a/packages/integrations/sitemap/src/utils/parse-url.ts b/packages/integrations/sitemap/src/utils/parse-url.ts
new file mode 100644
index 000000000..f9189cf7d
--- /dev/null
+++ b/packages/integrations/sitemap/src/utils/parse-url.ts
@@ -0,0 +1,39 @@
+export const parseUrl = (
+ url: string,
+ defaultLocale: string,
+ localeCodes: string[],
+ base: string
+) => {
+ if (
+ !url ||
+ !defaultLocale ||
+ localeCodes.length === 0 ||
+ localeCodes.some((key) => !key) ||
+ !base
+ ) {
+ throw new Error('parseUrl: some parameters are empty');
+ }
+ if (url.indexOf(base) !== 0) {
+ return undefined;
+ }
+ let s = url.replace(base, '');
+ if (!s || s === '/') {
+ return { locale: defaultLocale, path: '/' };
+ }
+ if (!s.startsWith('/')) {
+ s = '/' + s;
+ }
+ const a = s.split('/');
+ const locale = a[1];
+ if (localeCodes.some((key) => key === locale)) {
+ let path = a.slice(2).join('/');
+ if (path === '//') {
+ path = '/';
+ }
+ if (path !== '/' && !path.startsWith('/')) {
+ path = '/' + path;
+ }
+ return { locale, path };
+ }
+ return { locale: defaultLocale, path: s };
+};