2023-07-23 00:27:41 +00:00
|
|
|
import { Root as HTMLRoot } from "hast"
|
2023-05-30 15:02:20 +00:00
|
|
|
import { toString } from "hast-util-to-string"
|
|
|
|
import { QuartzTransformerPlugin } from "../types"
|
2023-09-07 04:47:59 +00:00
|
|
|
import { escapeHTML } from "../../util/escape"
|
2023-05-30 15:02:20 +00:00
|
|
|
|
|
|
|
export interface Options {
|
|
|
|
descriptionLength: number
|
2024-03-04 17:52:28 +00:00
|
|
|
replaceExternalLinks: boolean
|
2023-05-30 15:02:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const defaultOptions: Options = {
|
2023-07-23 00:27:41 +00:00
|
|
|
descriptionLength: 150,
|
2024-03-04 17:52:28 +00:00
|
|
|
replaceExternalLinks: true,
|
2023-05-30 15:02:20 +00:00
|
|
|
}
|
|
|
|
|
2024-03-04 17:52:28 +00:00
|
|
|
const urlRegex = new RegExp(
|
|
|
|
/(https?:\/\/)?(?<domain>([\da-z\.-]+)\.([a-z\.]{2,6})(:\d+)?)(?<path>[\/\w\.-]*)(\?[\/\w\.=&;-]*)?/,
|
|
|
|
"g",
|
|
|
|
)
|
|
|
|
|
2023-06-12 06:26:43 +00:00
|
|
|
export const Description: QuartzTransformerPlugin<Partial<Options> | undefined> = (userOpts) => {
|
|
|
|
const opts = { ...defaultOptions, ...userOpts }
|
|
|
|
return {
|
|
|
|
name: "Description",
|
|
|
|
htmlPlugins() {
|
|
|
|
return [
|
|
|
|
() => {
|
|
|
|
return async (tree: HTMLRoot, file) => {
|
2024-03-04 17:52:28 +00:00
|
|
|
let frontMatterDescription = file.data.frontmatter?.description
|
|
|
|
let text = escapeHTML(toString(tree))
|
|
|
|
|
|
|
|
if (opts.replaceExternalLinks) {
|
|
|
|
frontMatterDescription = frontMatterDescription?.replace(
|
|
|
|
urlRegex,
|
|
|
|
"$<domain>" + "$<path>",
|
|
|
|
)
|
|
|
|
text = text.replace(urlRegex, "$<domain>" + "$<path>")
|
|
|
|
}
|
2023-06-12 06:26:43 +00:00
|
|
|
|
|
|
|
const desc = frontMatterDescription ?? text
|
2024-03-04 17:52:28 +00:00
|
|
|
const sentences = desc.replace(/\s+/g, " ").split(/\.\s/)
|
2024-03-07 03:45:02 +00:00
|
|
|
const finalDesc: string[] = []
|
2023-06-12 06:26:43 +00:00
|
|
|
const len = opts.descriptionLength
|
2024-03-07 03:45:02 +00:00
|
|
|
let sentenceIdx = 0
|
2024-03-08 09:04:44 +00:00
|
|
|
let currentDescriptionLength = 0
|
2024-03-04 17:52:28 +00:00
|
|
|
|
|
|
|
if (sentences[0] !== undefined && sentences[0].length >= len) {
|
|
|
|
const firstSentence = sentences[0].split(" ")
|
2024-03-08 09:04:44 +00:00
|
|
|
while (currentDescriptionLength < len) {
|
2024-03-04 17:52:28 +00:00
|
|
|
const sentence = firstSentence[sentenceIdx]
|
|
|
|
if (!sentence) break
|
2024-03-07 03:45:02 +00:00
|
|
|
finalDesc.push(sentence)
|
2024-03-08 09:04:44 +00:00
|
|
|
currentDescriptionLength += sentence.length
|
2024-03-04 17:52:28 +00:00
|
|
|
sentenceIdx++
|
|
|
|
}
|
2024-03-07 03:45:02 +00:00
|
|
|
finalDesc.push("...")
|
2024-03-04 17:52:28 +00:00
|
|
|
} else {
|
2024-03-08 09:04:44 +00:00
|
|
|
while (currentDescriptionLength < len) {
|
2024-03-04 17:52:28 +00:00
|
|
|
const sentence = sentences[sentenceIdx]
|
|
|
|
if (!sentence) break
|
2024-03-08 09:04:44 +00:00
|
|
|
const currentSentence = sentence.endsWith(".") ? sentence : sentence + "."
|
|
|
|
finalDesc.push(currentSentence)
|
|
|
|
currentDescriptionLength += currentSentence.length
|
2024-03-04 17:52:28 +00:00
|
|
|
}
|
2023-06-12 06:26:43 +00:00
|
|
|
}
|
|
|
|
|
2024-03-07 03:45:02 +00:00
|
|
|
file.data.description = finalDesc.join(" ")
|
2023-06-12 06:26:43 +00:00
|
|
|
file.data.text = text
|
2023-05-30 15:02:20 +00:00
|
|
|
}
|
2023-07-23 00:27:41 +00:00
|
|
|
},
|
2023-06-12 06:26:43 +00:00
|
|
|
]
|
2023-07-23 00:27:41 +00:00
|
|
|
},
|
2023-05-30 15:02:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-23 00:27:41 +00:00
|
|
|
declare module "vfile" {
|
2023-05-30 15:02:20 +00:00
|
|
|
interface DataMap {
|
|
|
|
description: string
|
2023-06-08 05:27:32 +00:00
|
|
|
text: string
|
2023-05-30 15:02:20 +00:00
|
|
|
}
|
|
|
|
}
|