From a879b082422cd6b173da099646bc460a1e9ad046 Mon Sep 17 00:00:00 2001 From: PikachuEXE Date: Thu, 23 Mar 2023 11:01:37 +0800 Subject: [PATCH 1/3] ! Fix "mention" in comments was rendered as link with channel URL --- src/renderer/helpers/api/local.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/renderer/helpers/api/local.js b/src/renderer/helpers/api/local.js index 4916dab073874..bdb02eb5d80ed 100644 --- a/src/renderer/helpers/api/local.js +++ b/src/renderer/helpers/api/local.js @@ -459,8 +459,9 @@ function convertSearchFilters(filters) { /** * @param {(TextRun|EmojiRun)[]} runs * @param {number} emojiSize + * @param {{looseChannelNameDetection: boolean}} options */ -export function parseLocalTextRuns(runs, emojiSize = 16) { +export function parseLocalTextRuns(runs, emojiSize = 16, options = { looseChannelNameDetection: false }) { if (!Array.isArray(runs)) { throw new Error('not an array of text runs') } @@ -508,8 +509,10 @@ export function parseLocalTextRuns(runs, emojiSize = 16) { break case 'WEB_PAGE_TYPE_CHANNEL': { const trimmedText = text.trim() - if (CHANNEL_HANDLE_REGEX.test(trimmedText)) { - parsedRuns.push(`${trimmedText}`) + // In comments, mention can be `@Channel Name` (not handle, but name) + if (CHANNEL_HANDLE_REGEX.test(trimmedText) || (options.looseChannelNameDetection && /^@/.test(trimmedText))) { + // Extra space at the end due to YT tend to include one space afterward into "channel run" `text` for "mentions" + parsedRuns.push(`${trimmedText} `) } else { parsedRuns.push(`https://www.youtube.com${endpoint.metadata.url}`) } @@ -603,7 +606,7 @@ export function parseLocalComment(comment, commentThread = undefined) { isOwner: comment.author_is_channel_owner, isMember: comment.is_member, memberIconUrl: comment.is_member ? comment.sponsor_comment_badge.custom_badge[0].url : '', - text: Autolinker.link(parseLocalTextRuns(comment.content.runs, 16)), + text: Autolinker.link(parseLocalTextRuns(comment.content.runs, 16, { looseChannelNameDetection: true })), time: toLocalePublicationString({ publishText: comment.published.text.replace('(edited)', '').trim() }), likes: comment.vote_count, isHearted: comment.is_hearted, From 14dd6531ea1ce6bafe50cddf7ba41c17596102f5 Mon Sep 17 00:00:00 2001 From: PikachuEXE Date: Fri, 31 Mar 2023 10:59:19 +0800 Subject: [PATCH 2/3] ! Fix original space padding logic --- src/renderer/helpers/api/local.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/renderer/helpers/api/local.js b/src/renderer/helpers/api/local.js index bdb02eb5d80ed..01738ac8a02cb 100644 --- a/src/renderer/helpers/api/local.js +++ b/src/renderer/helpers/api/local.js @@ -511,8 +511,10 @@ export function parseLocalTextRuns(runs, emojiSize = 16, options = { looseChanne const trimmedText = text.trim() // In comments, mention can be `@Channel Name` (not handle, but name) if (CHANNEL_HANDLE_REGEX.test(trimmedText) || (options.looseChannelNameDetection && /^@/.test(trimmedText))) { - // Extra space at the end due to YT tend to include one space afterward into "channel run" `text` for "mentions" - parsedRuns.push(`${trimmedText} `) + // Note that in regex `\s` must be used since the text contain non-default space (the half-width space char when we press spacebar) + const spacesBefore = (/^\s+/.exec(text) || [''])[0] + const spacesAfter = (/\s+$/.exec(text) || [''])[0] + parsedRuns.push(`${spacesBefore}${trimmedText}${spacesAfter}`) } else { parsedRuns.push(`https://www.youtube.com${endpoint.metadata.url}`) } From a661dd3e4ebfbe8288469b748cde264dd39345d9 Mon Sep 17 00:00:00 2001 From: PikachuEXE Date: Sun, 2 Apr 2023 09:57:04 +0800 Subject: [PATCH 3/3] $ Refactor code as suggested --- src/renderer/helpers/api/local.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/renderer/helpers/api/local.js b/src/renderer/helpers/api/local.js index 01738ac8a02cb..efa49b6fa0a10 100644 --- a/src/renderer/helpers/api/local.js +++ b/src/renderer/helpers/api/local.js @@ -467,6 +467,8 @@ export function parseLocalTextRuns(runs, emojiSize = 16, options = { looseChanne } const timestampRegex = /^(?:\d+:){1,2}\d+$/ + const spacesBeforeRegex = /^\s+/ + const spacesAfterRegex = /\s+$/ const parsedRuns = [] for (const run of runs) { @@ -510,10 +512,10 @@ export function parseLocalTextRuns(runs, emojiSize = 16, options = { looseChanne case 'WEB_PAGE_TYPE_CHANNEL': { const trimmedText = text.trim() // In comments, mention can be `@Channel Name` (not handle, but name) - if (CHANNEL_HANDLE_REGEX.test(trimmedText) || (options.looseChannelNameDetection && /^@/.test(trimmedText))) { + if (CHANNEL_HANDLE_REGEX.test(trimmedText) || (options.looseChannelNameDetection && trimmedText.startsWith('@'))) { // Note that in regex `\s` must be used since the text contain non-default space (the half-width space char when we press spacebar) - const spacesBefore = (/^\s+/.exec(text) || [''])[0] - const spacesAfter = (/\s+$/.exec(text) || [''])[0] + const spacesBefore = (spacesBeforeRegex.exec(text) || [''])[0] + const spacesAfter = (spacesAfterRegex.exec(text) || [''])[0] parsedRuns.push(`${spacesBefore}${trimmedText}${spacesAfter}`) } else { parsedRuns.push(`https://www.youtube.com${endpoint.metadata.url}`)