Skip to content

Commit

Permalink
banner test
Browse files Browse the repository at this point in the history
  • Loading branch information
courtlandleer committed Aug 23, 2024
2 parents ef2039d + 997f7de commit e97e1dc
Showing 1 changed file with 37 additions and 21 deletions.
58 changes: 37 additions & 21 deletions quartz/plugins/transformers/description.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
},
Expand Down

0 comments on commit e97e1dc

Please sign in to comment.