From f1087fd270b142a8978b062d650b744b8b4157c6 Mon Sep 17 00:00:00 2001 From: Chris-Robin Ennen Date: Tue, 7 Feb 2023 00:03:16 +0100 Subject: [PATCH] fix: only count 23 characters per link when composing (#1660) --- components/publish/PublishWidget.vue | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/components/publish/PublishWidget.vue b/components/publish/PublishWidget.vue index e6b6ede7..cc4e653b 100644 --- a/components/publish/PublishWidget.vue +++ b/components/publish/PublishWidget.vue @@ -64,7 +64,19 @@ const { editor } = useTiptap({ }) const characterCount = $computed(() => { - let length = stringLength(htmlToText(editor.value?.getHTML() || '')) + const text = htmlToText(editor.value?.getHTML() || '') + + let length = stringLength(text) + + // taken from https://github.com/mastodon/mastodon/blob/07f8b4d1b19f734d04e69daeb4c3421ef9767aac/app/lib/text_formatter.rb + const linkRegex = /(https?:\/\/(www\.)?|xmpp:)\S+/g + + // maximum of 23 chars per link + // https://github.com/elk-zone/elk/issues/1651 + const maxLength = 23 + + for (const [fullMatch] of text.matchAll(linkRegex)) + length -= fullMatch.length - Math.min(maxLength, fullMatch.length) if (draft.mentions) { // + 1 is needed as mentions always need a space seperator at the end