Tighten social metadata generation
This commit is contained in:
parent
bbdc45eca3
commit
bc3cf9691f
5 changed files with 57 additions and 8 deletions
|
|
@ -12,6 +12,7 @@ import { BuildCtx } from "../../util/ctx"
|
|||
import { QuartzPluginData } from "../vfile"
|
||||
import fs from "node:fs/promises"
|
||||
import { styleText } from "util"
|
||||
import { normalizeSocialDescription } from "../../util/social"
|
||||
|
||||
const defaultOptions: SocialImageOptions = {
|
||||
colorScheme: "lightMode",
|
||||
|
|
@ -76,10 +77,11 @@ async function processOgImage(
|
|||
const titleSuffix = cfg.pageTitleSuffix ?? ""
|
||||
const title =
|
||||
(fileData.frontmatter?.title ?? i18n(cfg.locale).propertyDefaults.title) + titleSuffix
|
||||
const description =
|
||||
const description = normalizeSocialDescription(
|
||||
fileData.frontmatter?.socialDescription ??
|
||||
fileData.frontmatter?.description ??
|
||||
unescapeHTML(fileData.description?.trim() ?? i18n(cfg.locale).propertyDefaults.description)
|
||||
fileData.frontmatter?.description ??
|
||||
unescapeHTML(fileData.description?.trim() ?? i18n(cfg.locale).propertyDefaults.description),
|
||||
)
|
||||
|
||||
const stream = await generateSocialImage(
|
||||
{
|
||||
|
|
|
|||
|
|
@ -53,6 +53,17 @@ function isStandalonePreviewTagline(node: RootContent): boolean {
|
|||
return isElementNode(onlyChild) && ["em", "strong"].includes(onlyChild.tagName)
|
||||
}
|
||||
|
||||
function isStructuredMetadataParagraph(node: RootContent): boolean {
|
||||
if (!isElementNode(node) || node.tagName !== "p") {
|
||||
return false
|
||||
}
|
||||
|
||||
const text = normalizeWhitespace(toString(node)).toLowerCase()
|
||||
const labels = ["date:", "location:", "primary actors:", "authors:"]
|
||||
const labelMatches = labels.filter((label) => text.includes(label)).length
|
||||
return labelMatches >= 2
|
||||
}
|
||||
|
||||
export function extractDescriptionText(tree: HTMLRoot, pageTitle?: string): string {
|
||||
const children = [...tree.children]
|
||||
let startIdx = 0
|
||||
|
|
@ -77,6 +88,14 @@ export function extractDescriptionText(tree: HTMLRoot, pageTitle?: string): stri
|
|||
startIdx++
|
||||
}
|
||||
|
||||
if (startIdx < children.length && isStructuredMetadataParagraph(children[startIdx])) {
|
||||
startIdx++
|
||||
}
|
||||
|
||||
while (startIdx < children.length && !isMeaningfulNode(children[startIdx])) {
|
||||
startIdx++
|
||||
}
|
||||
|
||||
return escapeHTML(toString({ ...tree, children: children.slice(startIdx) }))
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -63,7 +63,11 @@ function getFirstMarkdownHeading(root: MdRoot): string | undefined {
|
|||
return toString(node).trim().length > 0
|
||||
})
|
||||
|
||||
if (!firstMeaningfulNode || firstMeaningfulNode.type !== "heading") {
|
||||
if (
|
||||
!firstMeaningfulNode ||
|
||||
firstMeaningfulNode.type !== "heading" ||
|
||||
firstMeaningfulNode.depth !== 1
|
||||
) {
|
||||
return undefined
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue