Skip to content

Commit f9b18d5

Browse files
committed
fix link markdown bugs
1 parent 7491de7 commit f9b18d5

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

src/utils.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,19 +101,31 @@ export const renderText = (text, mentioned_users) => {
101101
let newText = text;
102102
let markdownLinks = matchMarkdownLinks(newText);
103103
let codeBlocks = messageCodeBlocks(newText);
104+
const detectHttp = /(http(s?):\/\/)?(www\.)?/;
105+
104106
// extract all valid links/emails within text and replace it with proper markup
105107
linkify.find(newText).forEach(({ type, href, value }) => {
106108
const linkIsInBlock = codeBlocks.some((block) => block?.includes(value));
109+
107110
// check if message is already markdown
108111
const noParsingNeeded =
109112
markdownLinks &&
110-
markdownLinks.filter((text) => text?.indexOf(href) !== -1);
113+
markdownLinks.filter((text) => {
114+
const strippedHref = href?.replace(detectHttp, '');
115+
const strippedText = text?.replace(detectHttp, '');
116+
117+
if (!strippedHref || !strippedText) return false;
118+
119+
return (
120+
strippedHref.includes(strippedText) ||
121+
strippedText.includes(strippedHref)
122+
);
123+
});
124+
111125
if (noParsingNeeded.length > 0 || linkIsInBlock) return;
112126

113127
const displayLink =
114-
type === 'email'
115-
? value
116-
: truncate(value.replace(/(http(s?):\/\/)?(www\.)?/, ''), 20);
128+
type === 'email' ? value : truncate(value.replace(detectHttp, ''), 20);
117129
newText = newText.replace(value, `[${displayLink}](${encodeURI(href)})`);
118130
});
119131

0 commit comments

Comments
 (0)