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