summaryrefslogtreecommitdiffstats
path: root/web/react
diff options
context:
space:
mode:
authorCorey Hulen <corey@hulen.com>2016-02-23 14:00:02 -0800
committerCorey Hulen <corey@hulen.com>2016-02-23 14:00:02 -0800
commit52767d9dcdc84fca4cd7a5b5c7ece2650691b91d (patch)
treeb18c4d3de81a24a04147f1bb375fed9a390f7b2b /web/react
parent6e0ce387a35cccb0ecc849936acab62e75e90128 (diff)
parentd6c6f5eaeacd16b3615c855ab7b8aa51f9f46efe (diff)
downloadchat-52767d9dcdc84fca4cd7a5b5c7ece2650691b91d.tar.gz
chat-52767d9dcdc84fca4cd7a5b5c7ece2650691b91d.tar.bz2
chat-52767d9dcdc84fca4cd7a5b5c7ece2650691b91d.zip
Merge pull request #2215 from hmhealey/plt1678
PLT-1678 Refactored how links are extracted to remove code blocks and markdown images
Diffstat (limited to 'web/react')
-rw-r--r--web/react/components/post_body.jsx6
-rw-r--r--web/react/utils/utils.jsx59
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) {