|
|
|
@ -0,0 +1,32 @@ |
|
|
|
interface PageMeta { |
|
|
|
title?: string |
|
|
|
description?: string |
|
|
|
image?: string |
|
|
|
url?: string |
|
|
|
type?: string |
|
|
|
} |
|
|
|
|
|
|
|
export function setPageMeta(meta: PageMeta) { |
|
|
|
const siteName = 'Mach-CMS' |
|
|
|
document.title = meta.title ? `${meta.title} | ${siteName}` : siteName |
|
|
|
|
|
|
|
const setTag = (selector: string, attr: string, val?: string) => { |
|
|
|
if (!val) return |
|
|
|
let el = document.querySelector(selector) as HTMLMetaElement | null |
|
|
|
if (!el) { |
|
|
|
el = document.createElement('meta') |
|
|
|
const key = selector.includes('property=') ? 'property' : 'name' |
|
|
|
const keyVal = selector.match(/"([^"]+)"/)?.[1] || '' |
|
|
|
el.setAttribute(key, keyVal) |
|
|
|
document.head.appendChild(el) |
|
|
|
} |
|
|
|
el.setAttribute(attr, val) |
|
|
|
} |
|
|
|
|
|
|
|
setTag('meta[property="og:title"]', 'content', meta.title) |
|
|
|
setTag('meta[property="og:description"]', 'content', meta.description) |
|
|
|
setTag('meta[property="og:image"]', 'content', meta.image) |
|
|
|
setTag('meta[property="og:url"]', 'content', meta.url) |
|
|
|
setTag('meta[property="og:type"]', 'content', meta.type || 'article') |
|
|
|
setTag('meta[name="description"]', 'content', meta.description) |
|
|
|
} |