From d6c6f5eaeacd16b3615c855ab7b8aa51f9f46efe Mon Sep 17 00:00:00 2001 From: hmhealey Date: Mon, 22 Feb 2016 17:58:20 -0500 Subject: Refactored how links are extracted to remove code blocks and markdown images --- web/react/components/post_body.jsx | 6 ++-- web/react/utils/utils.jsx | 59 ++++++++++++-------------------------- 2 files changed, 22 insertions(+), 43 deletions(-) diff --git a/web/react/components/post_body.jsx b/web/react/components/post_body.jsx index b187acba3..506b38ce6 100644 --- a/web/react/components/post_body.jsx +++ b/web/react/components/post_body.jsx @@ -41,7 +41,7 @@ class PostBody extends React.Component { const linkData = Utils.extractLinks(this.props.post.message); this.state = { - links: linkData.links, + links: linkData, post: this.props.post }; } @@ -86,10 +86,10 @@ class PostBody extends React.Component { componentWillReceiveProps(nextProps) { const linkData = Utils.extractLinks(nextProps.post.message); if (this.props.post.filenames.length === 0 && this.state.links && this.state.links.length > 0) { - this.embed = this.createEmbed(linkData.links[0]); + this.embed = this.createEmbed(linkData[0]); } this.setState({ - links: linkData.links + links: linkData }); } 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) { -- cgit v1.2.3-1-g7c22