summaryrefslogtreecommitdiffstats
path: root/web/react/utils
diff options
context:
space:
mode:
authorhmhealey <harrisonmhealey@gmail.com>2015-09-19 10:31:48 -0400
committerhmhealey <harrisonmhealey@gmail.com>2015-09-19 10:39:51 -0400
commitefb3462a683d1f4d88c56327b14256adda11115d (patch)
tree7dcc006aaf25ed081f2cebf05ba164f4c511abea /web/react/utils
parentbb330b662a60c6581eda6dde0d94eb3a57579b15 (diff)
downloadchat-efb3462a683d1f4d88c56327b14256adda11115d.tar.gz
chat-efb3462a683d1f4d88c56327b14256adda11115d.tar.bz2
chat-efb3462a683d1f4d88c56327b14256adda11115d.zip
Prevented our url autolinking from breaking markdown links
Diffstat (limited to 'web/react/utils')
-rw-r--r--web/react/utils/text_formatting.jsx40
1 files changed, 34 insertions, 6 deletions
diff --git a/web/react/utils/text_formatting.jsx b/web/react/utils/text_formatting.jsx
index 62e6978a9..da0595326 100644
--- a/web/react/utils/text_formatting.jsx
+++ b/web/react/utils/text_formatting.jsx
@@ -17,11 +17,14 @@ const markdownRenderer = new marked.Renderer();
// - mentionHighlight - Specifies whether or not to highlight mentions of the current user. Defaults to true.
// - singleline - Specifies whether or not to remove newlines. Defaults to false.
export function formatText(text, options = {}) {
+ // TODO remove me
+ options.markdown = true;
+
let output = sanitizeHtml(text);
const tokens = new Map();
// replace important words and phrases with tokens
- output = autolinkUrls(output, tokens);
+ output = autolinkUrls(output, tokens, !!options.markdown);
output = autolinkAtMentions(output, tokens);
output = autolinkHashtags(output, tokens);
@@ -34,9 +37,11 @@ export function formatText(text, options = {}) {
}
// perform markdown parsing while we have an html-free input string
- console.log('output before marked ' + output);
- output = marked(output, {renderer: markdownRenderer});
- console.log('output after marked ' + output);
+ if (options.markdown) {
+ console.log('output before marked ' + output);
+ output = marked(output, {renderer: markdownRenderer});
+ console.log('output after marked ' + output);
+ }
// reinsert tokens with formatted versions of the important words and phrases
output = replaceTokens(output, tokens);
@@ -62,7 +67,7 @@ export function sanitizeHtml(text) {
return output;
}
-function autolinkUrls(text, tokens) {
+function autolinkUrls(text, tokens, markdown) {
function replaceUrlWithToken(autolinker, match) {
const linkText = match.getMatchedText();
let url = linkText;
@@ -92,7 +97,30 @@ function autolinkUrls(text, tokens) {
replaceFn: replaceUrlWithToken
});
- return autolinker.link(text);
+ let output = text;
+
+ // temporarily replace markdown links if markdown is enabled so that we don't accidentally parse them twice
+ const markdownLinkTokens = new Map();
+ if (markdown) {
+ function replaceMarkdownLinkWithToken(markdownLink) {
+ const index = markdownLinkTokens.size;
+ const alias = `MM_MARKDOWNLINK${index}`;
+
+ markdownLinkTokens.set(alias, {value: markdownLink});
+
+ return alias;
+ }
+
+ output = output.replace(/\]\([^\)]*\)/g, replaceMarkdownLinkWithToken);
+ }
+
+ output = autolinker.link(output);
+
+ if (markdown) {
+ output = replaceTokens(output, markdownLinkTokens);
+ }
+
+ return output;
}
function autolinkAtMentions(text, tokens) {