diff --git a/src/components/CommentItem.vue b/src/components/CommentItem.vue index be0ba20..dd2fbdf 100644 --- a/src/components/CommentItem.vue +++ b/src/components/CommentItem.vue @@ -99,6 +99,7 @@ import smartquotes from 'smartquotes-ts'; import type { HackerNewsItem } from '@/types'; import { apiItemUrl } from '@/utils/apiUrls'; import { absoluteTimestamp } from '@/utils/absoluteTimestamp'; +import { avoidShortWidows } from '@/utils/avoidShortWidows'; import BaseIcon from '@/components/BaseIcon.vue'; import CommentItem from '@/components/CommentItem.vue'; import { useRelativeTimestamp } from '@/composables/relativeTimestamp'; @@ -234,6 +235,7 @@ domPurifyInstance.addHook('afterSanitizeElements', (node) => { .replace(subsequentLinePattern, '\n'); } + node.textContent = avoidShortWidows(node.textContent); node.textContent = smartquotes(node.textContent); }); diff --git a/src/utils/avoidShortWidows.ts b/src/utils/avoidShortWidows.ts new file mode 100644 index 0000000..cef1690 --- /dev/null +++ b/src/utils/avoidShortWidows.ts @@ -0,0 +1,8 @@ +export function avoidShortWidows(text: string) { + const words = text.split(' '); + + const lastWord = words.pop(); + if (!lastWord) return text; + + return words.join(' ') + (words.length > 2 && lastWord.length <= 10 ? ' ' : ' ') + lastWord; +}