This repository has been archived on 2025-03-28. You can view files and clone it, but cannot push or open issues or pull requests.
coryd.dev-eleventy/workers/dynamic-pages/utils/updaters.js

29 lines
No EOL
1.3 KiB
JavaScript

import { parseHTML } from 'linkedom'
export const updateDynamicContent = (html, metadata, mediaHtml) => {
const { document } = parseHTML(html)
const titleTag = document.querySelector('title[data-dynamic="title"]')
if (titleTag) titleTag['textContent'] = metadata['title']
const dynamicMetaSelectors = [
{ selector: 'meta[data-dynamic="description"]', attribute: 'content', value: metadata['description'] },
{ selector: 'meta[data-dynamic="og:title"]', attribute: 'content', value: metadata['og:title'] },
{ selector: 'meta[data-dynamic="og:description"]', attribute: 'content', value: metadata['og:description'] },
{ selector: 'meta[data-dynamic="og:image"]', attribute: 'content', value: metadata['og:image'] },
{ selector: 'meta[data-dynamic="og:url"]', attribute: 'content', value: metadata['canonical'] },
]
dynamicMetaSelectors.forEach(({ selector, attribute, value }) => {
const element = document.querySelector(selector)
if (element) element.setAttribute(attribute, value)
})
const canonicalLink = document.querySelector('link[rel="canonical"]')
if (canonicalLink) canonicalLink.setAttribute('href', metadata['canonical'])
const pageElement = document.querySelector('[data-dynamic="page"]')
if (pageElement) pageElement.innerHTML = mediaHtml
return document.toString()
}