Tighten social metadata generation
This commit is contained in:
parent
bbdc45eca3
commit
bc3cf9691f
5 changed files with 57 additions and 8 deletions
|
|
@ -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