-
Notifications
You must be signed in to change notification settings - Fork 2
/
DescriptionReplaceCommand.ts
44 lines (41 loc) · 1.77 KB
/
DescriptionReplaceCommand.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { HtmlRR0SsgContext } from "./RR0SsgContext.js"
import { ReplaceCommand } from "ssg-api/dist/src/step/content/replace/ReplaceCommand.js"
/**
* Adds an "abstract" HTML paragraph from a <meta name="description">, if any.
*/
export class DescriptionReplaceCommand implements ReplaceCommand<HtmlRR0SsgContext> {
constructor(protected readonly defaultDescription: string, protected readonly abstractClass = "abstract") {
}
async execute(context: HtmlRR0SsgContext): Promise<void> {
const file = context.file
const inDescription = file.meta.description
const outDoc = file.document
let descriptionMeta = outDoc.head.querySelector("meta[name='description']")
if (!descriptionMeta) {
const existingAbstract = outDoc.querySelector(`.${this.abstractClass}`)
let outDescription: string
if (existingAbstract) {
outDescription = inDescription || existingAbstract.textContent.toString()
} else if (inDescription) {
const abstractFromDescription = outDoc.createElement("p")
abstractFromDescription.className = this.abstractClass
abstractFromDescription.textContent = inDescription
const body = outDoc.body
body.insertBefore(abstractFromDescription, body.firstChild)
outDescription = inDescription
} else {
outDescription = this.defaultDescription
}
outDescription = outDescription.replace(/\s{2,}/g, " ")
file.meta.description = outDescription
const descriptionMeta = outDoc.createElement("meta") as HTMLMetaElement
descriptionMeta.name = "description"
descriptionMeta.content = file.meta.description
outDoc.head.append(descriptionMeta)
context.file.contents = outDoc.documentElement.outerHTML
}
}
async contentStepEnd() {
// NOP
}
}