mirror of
https://github.com/jackyzha0/quartz.git
synced 2025-12-19 19:04:06 -06:00
Repetitive description fix
This commit is contained in:
parent
b67da534ef
commit
997f7deed8
@ -38,33 +38,49 @@ export const Description: QuartzTransformerPlugin<Partial<Options> | undefined>
|
||||
}
|
||||
|
||||
const desc = frontMatterDescription ?? text
|
||||
const sentences = desc.replace(/\s+/g, " ").split(/\.\s/)
|
||||
// const sentences = desc.replace(/\s+/g, " ").split(/\.\s/)
|
||||
const sentences = desc.replace(/\s+/g, " ").split(/[.!?]\s+/)
|
||||
const finalDesc: string[] = []
|
||||
const len = opts.descriptionLength
|
||||
let sentenceIdx = 0
|
||||
// let sentenceIdx = 0
|
||||
let currentDescriptionLength = 0
|
||||
|
||||
if (sentences[0] !== undefined && sentences[0].length >= len) {
|
||||
const firstSentence = sentences[0].split(" ")
|
||||
while (currentDescriptionLength < len) {
|
||||
const sentence = firstSentence[sentenceIdx]
|
||||
if (!sentence) break
|
||||
finalDesc.push(sentence)
|
||||
currentDescriptionLength += sentence.length
|
||||
sentenceIdx++
|
||||
}
|
||||
finalDesc.push("...")
|
||||
} else {
|
||||
while (currentDescriptionLength < len) {
|
||||
const sentence = sentences[sentenceIdx]
|
||||
if (!sentence) break
|
||||
const currentSentence = sentence.endsWith(".") ? sentence : sentence + "."
|
||||
finalDesc.push(currentSentence)
|
||||
currentDescriptionLength += currentSentence.length
|
||||
}
|
||||
const endPunctuation = /[.!?]$/
|
||||
|
||||
for (const sentence of sentences) {
|
||||
if (currentDescriptionLength >= len) break
|
||||
|
||||
const currentSentence = endPunctuation.test(sentence) ? sentence : sentence + "."
|
||||
finalDesc.push(currentSentence)
|
||||
currentDescriptionLength += currentSentence.length
|
||||
}
|
||||
|
||||
file.data.description = finalDesc.join(" ")
|
||||
// Trim the last sentence if it exceeds the length limit
|
||||
if (currentDescriptionLength > len && finalDesc.length > 1) {
|
||||
finalDesc.pop()
|
||||
}
|
||||
|
||||
// if (sentences[0] !== undefined && sentences[0].length >= len) {
|
||||
// const firstSentence = sentences[0].split(" ")
|
||||
// while (currentDescriptionLength < len) {
|
||||
// const sentence = firstSentence[sentenceIdx]
|
||||
// if (!sentence) break
|
||||
// finalDesc.push(sentence)
|
||||
// currentDescriptionLength += sentence.length
|
||||
// sentenceIdx++
|
||||
// }
|
||||
// finalDesc.push("...")
|
||||
// } else {
|
||||
// while (currentDescriptionLength < len) {
|
||||
// const sentence = sentences[sentenceIdx]
|
||||
// if (!sentence) break
|
||||
// const currentSentence = sentence.endsWith(".") ? sentence : sentence + "."
|
||||
// finalDesc.push(currentSentence)
|
||||
// currentDescriptionLength += currentSentence.length
|
||||
// }
|
||||
// }
|
||||
|
||||
file.data.description = finalDesc.join(" ").trim()
|
||||
file.data.text = text
|
||||
}
|
||||
},
|
||||
|
||||
Loading…
Reference in New Issue
Block a user