From ac24c9e94ec2ae438aed82af72d6acc3528d40ee Mon Sep 17 00:00:00 2001 From: prototypa Date: Fri, 6 Jan 2023 12:47:00 -0500 Subject: [PATCH] Replace getRelativeLink with getPermalink --- src/components/common/ExtraMetaTags.astro | 8 +++---- src/components/common/Pagination.astro | 6 ++--- src/components/widgets/Footer.astro | 4 ++-- src/components/widgets/Header.astro | 4 ++-- src/utils/permalinks.ts | 28 +++++++++-------------- 5 files changed, 22 insertions(+), 28 deletions(-) diff --git a/src/components/common/ExtraMetaTags.astro b/src/components/common/ExtraMetaTags.astro index 8915004..878ed1f 100644 --- a/src/components/common/ExtraMetaTags.astro +++ b/src/components/common/ExtraMetaTags.astro @@ -1,7 +1,7 @@ --- -import { getRelativeLink } from '~/utils/permalinks'; +import { getPermalink } from '~/utils/permalinks'; --- - - - + + + diff --git a/src/components/common/Pagination.astro b/src/components/common/Pagination.astro index 5e72cac..c63b282 100644 --- a/src/components/common/Pagination.astro +++ b/src/components/common/Pagination.astro @@ -1,6 +1,6 @@ --- import { Icon } from 'astro-icon'; -import { getRelativeLink } from '~/utils/permalinks'; +import { getPermalink } from '~/utils/permalinks'; export interface Props { prevUrl: string; @@ -16,13 +16,13 @@ const { prevUrl, nextUrl, prevText = 'Newer posts', nextText = 'Older posts' } = (prevUrl || nextUrl) && (
- +

{prevText}

- +
{nextText} diff --git a/src/components/widgets/Footer.astro b/src/components/widgets/Footer.astro index fbde59e..1b63cf8 100644 --- a/src/components/widgets/Footer.astro +++ b/src/components/widgets/Footer.astro @@ -1,6 +1,6 @@ --- import { Icon } from 'astro-icon'; -import { getHomePermalink, getRelativeLink } from '~/utils/permalinks'; +import { getHomePermalink, getPermalink } from '~/utils/permalinks'; const links = [ { @@ -53,7 +53,7 @@ const social = [ { label: 'Twitter', icon: 'tabler:brand-twitter', href: '#' }, { label: 'Instagram', icon: 'tabler:brand-instagram', href: '#' }, { label: 'Facebook', icon: 'tabler:brand-facebook', href: '#' }, - { label: 'RSS', icon: 'tabler:rss', href: getRelativeLink('/rss.xml') }, + { label: 'RSS', icon: 'tabler:rss', href: getPermalink('/rss.xml') }, { label: 'Github', icon: 'tabler:brand-github', href: 'https://github.com/onwidget/astrowind' }, ]; --- diff --git a/src/components/widgets/Header.astro b/src/components/widgets/Header.astro index f0f71e8..f7c8d90 100644 --- a/src/components/widgets/Header.astro +++ b/src/components/widgets/Header.astro @@ -4,7 +4,7 @@ import Logo from '~/components/common/Logo.astro'; import ToggleTheme from '~/components/common/ToggleTheme.astro'; import ToggleMenu from '~/components/common/ToggleMenu.astro'; -import { getHomePermalink, getBlogPermalink, getPermalink, getRelativeLink } from '~/utils/permalinks'; +import { getHomePermalink, getBlogPermalink, getPermalink, getPermalink } from '~/utils/permalinks'; ---
diff --git a/src/utils/permalinks.ts b/src/utils/permalinks.ts index 65dba84..466a973 100644 --- a/src/utils/permalinks.ts +++ b/src/utils/permalinks.ts @@ -11,7 +11,6 @@ const trim = (str = '', ch?: string) => { }; const trimSlash = (s: string) => trim(trim(s, '/')); - const createPath = (...params: string[]) => { const paths = params.filter((el) => !!el).join('/'); return '/' + paths + (SITE.trailingSlash && paths ? '/' : ''); @@ -19,7 +18,11 @@ const createPath = (...params: string[]) => { const basePathname = trimSlash(SITE.basePathname); -export const cleanSlug = (text: string) => slugify(trimSlash(text)); +export const cleanSlug = (text: string) => + trimSlash(text) + .split('/') + .map((slug) => slugify(slug)) + .join('/'); export const BLOG_BASE = cleanSlug(BLOG?.blog?.pathname); export const POST_BASE = cleanSlug(BLOG?.post?.pathname); @@ -31,33 +34,24 @@ export const getCanonical = (path = ''): string | URL => new URL(path, SITE.orig /** */ export const getPermalink = (slug = '', type = 'page'): string => { - const _slug = cleanSlug(slug); - switch (type) { case 'category': - return createPath(basePathname, CATEGORY_BASE, _slug); + return createPath(basePathname, CATEGORY_BASE, cleanSlug(slug)); case 'tag': - return createPath(basePathname, TAG_BASE, _slug); + return createPath(basePathname, TAG_BASE, cleanSlug(slug)); case 'post': - return createPath(basePathname, POST_BASE, _slug); - - case 'page': - default: - return createPath(basePathname, _slug); + return createPath(basePathname, POST_BASE, cleanSlug(slug)); } + + return createPath(basePathname, trimSlash(slug)); }; /** */ export const getHomePermalink = (): string => { const permalink = getPermalink(); - return permalink !== '/' ? permalink + '/' : permalink; -}; - -/** */ -export const getRelativeLink = (link = ''): string => { - return createPath(basePathname, trimSlash(link)); + return !permalink.startsWith('/') ? '/' + permalink : permalink; }; /** */