summaryrefslogtreecommitdiffstats
path: root/web/react/utils
diff options
context:
space:
mode:
authorhmhealey <harrisonmhealey@gmail.com>2016-02-22 17:58:20 -0500
committerhmhealey <harrisonmhealey@gmail.com>2016-02-23 14:36:04 -0500
commitd6c6f5eaeacd16b3615c855ab7b8aa51f9f46efe (patch)
tree208100488df57dc89b13494cf671e513906a092b /web/react/utils
parent0031c6fd1a4df621cbecfd2a2b3efe9a40cd49ce (diff)
downloadchat-d6c6f5eaeacd16b3615c855ab7b8aa51f9f46efe.tar.gz
chat-d6c6f5eaeacd16b3615c855ab7b8aa51f9f46efe.tar.bz2
chat-d6c6f5eaeacd16b3615c855ab7b8aa51f9f46efe.zip
Refactored how links are extracted to remove code blocks and markdown images
Diffstat (limited to 'web/react/utils')
-rw-r--r--web/react/utils/utils.jsx59
1 files changed, 19 insertions, 40 deletions
diff --git a/web/react/utils/utils.jsx b/web/react/utils/utils.jsx
index 82828e34b..fd250ad10 100644
--- a/web/react/utils/utils.jsx
+++ b/web/react/utils/utils.jsx
@@ -313,41 +313,17 @@ export function getTimestamp() {
// extracts links not styled by Markdown
export function extractLinks(text) {
const links = [];
- let replaceText = text;
-
- // pull out the Markdown code blocks
- const codeBlocks = [];
- const splitText = replaceText.split('`'); // also handles ```
- for (let i = 1; i < splitText.length; i += 2) {
- if (splitText[i].trim() !== '') {
- codeBlocks.push(splitText[i]);
- }
- }
+ let inText = text;
+
+ // strip out code blocks
+ inText = inText.replace(/`[^`]*`/g, '');
+
+ // strip out inline markdown images
+ inText = inText.replace(/!\[[^\]]*\]\([^\)]*\)/g, '');
function replaceFn(autolinker, match) {
let link = '';
const matchText = match.getMatchedText();
- const tempText = replaceText;
-
- const start = replaceText.indexOf(matchText);
- const end = start + matchText.length;
-
- replaceText = replaceText.substring(0, start) + replaceText.substring(end);
-
- // if it's a Markdown link, just skip it
- if (start > 1) {
- if (tempText.charAt(start - 2) === ']' && tempText.charAt(start - 1) === '(' && tempText.charAt(end) === ')') {
- return;
- }
- }
-
- // if it's in a Markdown code block, skip it
- for (const i in codeBlocks) {
- if (codeBlocks[i].indexOf(matchText) === 0) {
- codeBlocks[i] = codeBlocks[i].replace(matchText, '');
- return;
- }
- }
if (matchText.trim().indexOf('http') === 0) {
link = matchText;
@@ -358,16 +334,19 @@ export function extractLinks(text) {
links.push(link);
}
- Autolinker.link(text, {
- replaceFn,
- urls: {schemeMatches: true, wwwMatches: true, tldMatches: false},
- emails: false,
- twitter: false,
- phone: false,
- hashtag: false
- });
+ Autolinker.link(
+ inText,
+ {
+ replaceFn,
+ urls: {schemeMatches: true, wwwMatches: true, tldMatches: false},
+ emails: false,
+ twitter: false,
+ phone: false,
+ hashtag: false
+ }
+ );
- return {links, text};
+ return links;
}
export function escapeRegExp(string) {