Skip to content

Commit

Permalink
cleanup [ci-skip]
Browse files Browse the repository at this point in the history
  • Loading branch information
brc-dd committed Sep 9, 2022
1 parent 047101c commit 08382a3
Showing 1 changed file with 52 additions and 52 deletions.
104 changes: 52 additions & 52 deletions src/shared/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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]
}

0 comments on commit 08382a3

Please sign in to comment.