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 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 finalDesc: string[] = []
|
||||||
const len = opts.descriptionLength
|
const len = opts.descriptionLength
|
||||||
let sentenceIdx = 0
|
// let sentenceIdx = 0
|
||||||
let currentDescriptionLength = 0
|
let currentDescriptionLength = 0
|
||||||
|
|
||||||
if (sentences[0] !== undefined && sentences[0].length >= len) {
|
const endPunctuation = /[.!?]$/
|
||||||
const firstSentence = sentences[0].split(" ")
|
|
||||||
while (currentDescriptionLength < len) {
|
for (const sentence of sentences) {
|
||||||
const sentence = firstSentence[sentenceIdx]
|
if (currentDescriptionLength >= len) break
|
||||||
if (!sentence) break
|
|
||||||
finalDesc.push(sentence)
|
const currentSentence = endPunctuation.test(sentence) ? sentence : sentence + "."
|
||||||
currentDescriptionLength += sentence.length
|
finalDesc.push(currentSentence)
|
||||||
sentenceIdx++
|
currentDescriptionLength += currentSentence.length
|
||||||
}
|
|
||||||
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 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
|
file.data.text = text
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user