From 08382a3e5f67e9c00262722c347c2c0ebe4a8401 Mon Sep 17 00:00:00 2001 From: Divyansh Singh <40380293+brc-dd@users.noreply.github.com> Date: Fri, 9 Sep 2022 10:18:10 +0530 Subject: [PATCH] cleanup [ci-skip] --- src/shared/shared.ts | 104 +++++++++++++++++++++---------------------- 1 file changed, 52 insertions(+), 52 deletions(-) diff --git a/src/shared/shared.ts b/src/shared/shared.ts index 4f2535827a6c..90d5a427ba4d 100644 --- a/src/shared/shared.ts +++ b/src/shared/shared.ts @@ -4,13 +4,13 @@ export type { SiteData, PageData, HeadConfig, + LocaleConfig, + LocaleSpecificConfig, Header, DefaultTheme, PageDataPayload, CleanUrlsMode, - Awaitable, - LocaleConfig, - LocaleSpecificConfig + Awaitable } from '../../types/shared.js' export const EXTERNAL_URL_RE = /^[a-z]+:/i @@ -29,55 +29,6 @@ export const notFoundPageData: PageData = { lastUpdated: 0 } -/** - * Create the page title string based on configs. - */ -export function createTitle(siteData: SiteData, pageData: PageData): string { - const title = pageData.title || siteData.title - const template = pageData.titleTemplate ?? siteData.titleTemplate - - if (typeof template === 'string' && template.includes(':title')) { - return template.replace(/:title/g, title) - } - - const templateString = createTitleTemplate(siteData.title, template) - - return `${title}${templateString}` -} - -function createTitleTemplate( - siteTitle: string, - template?: string | boolean -): string { - if (template === false) { - return '' - } - - if (template === true || template === undefined) { - return ` | ${siteTitle}` - } - - if (siteTitle === template) { - return '' - } - - return ` | ${template}` -} - -function hasTag(head: HeadConfig[], tag: HeadConfig) { - const [tagType, tagAttrs] = tag - if (tagType !== 'meta') return false - const keyAttr = Object.entries(tagAttrs)[0] // First key - if (keyAttr == null) return false - return head.some( - ([type, attrs]) => type === tagType && attrs[keyAttr[0]] === keyAttr[1] - ) -} - -export function mergeHead(prev: HeadConfig[], curr: HeadConfig[]) { - return [...prev.filter((tagAttrs) => !hasTag(curr, tagAttrs)), ...curr] -} - export function isActive( currentPath: string, matchPath?: string, @@ -144,3 +95,52 @@ export function resolveSiteDataByRoute( siteData.locales[localeIndex].themeConfig ?? siteData.themeConfig }) } + +/** + * Create the page title string based on configs. + */ +export function createTitle(siteData: SiteData, pageData: PageData): string { + const title = pageData.title || siteData.title + const template = pageData.titleTemplate ?? siteData.titleTemplate + + if (typeof template === 'string' && template.includes(':title')) { + return template.replace(/:title/g, title) + } + + const templateString = createTitleTemplate(siteData.title, template) + + return `${title}${templateString}` +} + +function createTitleTemplate( + siteTitle: string, + template?: string | boolean +): string { + if (template === false) { + return '' + } + + if (template === true || template === undefined) { + return ` | ${siteTitle}` + } + + if (siteTitle === template) { + return '' + } + + return ` | ${template}` +} + +function hasTag(head: HeadConfig[], tag: HeadConfig) { + const [tagType, tagAttrs] = tag + if (tagType !== 'meta') return false + const keyAttr = Object.entries(tagAttrs)[0] // First key + if (keyAttr == null) return false + return head.some( + ([type, attrs]) => type === tagType && attrs[keyAttr[0]] === keyAttr[1] + ) +} + +export function mergeHead(prev: HeadConfig[], curr: HeadConfig[]) { + return [...prev.filter((tagAttrs) => !hasTag(curr, tagAttrs)), ...curr] +}