Skip to content

Commit

Permalink
Add short widow avoidance in comment text
Browse files Browse the repository at this point in the history
  • Loading branch information
jonamil committed Jul 9, 2024
1 parent f58800d commit 63f2198
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/components/CommentItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -234,6 +235,7 @@ domPurifyInstance.addHook('afterSanitizeElements', (node) => {
.replace(subsequentLinePattern, '\n');
}
node.textContent = avoidShortWidows(node.textContent);
node.textContent = smartquotes(node.textContent);
});
Expand Down
8 changes: 8 additions & 0 deletions src/utils/avoidShortWidows.ts
Original file line number Diff line number Diff line change
@@ -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;
}

1 comment on commit 63f2198

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deploy preview for hackernews-dot-cool ready!

✅ Preview
https://hackernews-dot-cool-owhnhi2w4-jonamil-56ba8df7.vercel.app

Built with commit 63f2198.
This pull request is being automatically deployed with vercel-action

Please sign in to comment.