From 997f7deed8d1567d3b73959686276c7fe86bccc5 Mon Sep 17 00:00:00 2001 From: Vineeth Voruganti <13438633+VVoruganti@users.noreply.github.com> Date: Fri, 23 Aug 2024 12:42:00 -0400 Subject: [PATCH] Repetitive description fix --- quartz/plugins/transformers/description.ts | 58 ++++++++++++++-------- 1 file changed, 37 insertions(+), 21 deletions(-) diff --git a/quartz/plugins/transformers/description.ts b/quartz/plugins/transformers/description.ts index 2ec88804a0617..37ddfd62808de 100644 --- a/quartz/plugins/transformers/description.ts +++ b/quartz/plugins/transformers/description.ts @@ -38,33 +38,49 @@ export const Description: QuartzTransformerPlugin | 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 } },