From 1e4510be3bb13c00583e14dbbcf99ade7a76c9bd Mon Sep 17 00:00:00 2001
From: Florian Orben <florian.orben@gmail.com>
Date: Sat, 24 Oct 2015 03:25:26 +0200
Subject: highlight code in markdown blocks

---
 web/react/utils/markdown.jsx | 61 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 61 insertions(+)

(limited to 'web/react/utils/markdown.jsx')

diff --git a/web/react/utils/markdown.jsx b/web/react/utils/markdown.jsx
index e34f3d00a..5be01d055 100644
--- a/web/react/utils/markdown.jsx
+++ b/web/react/utils/markdown.jsx
@@ -6,6 +6,32 @@ const Utils = require('./utils.jsx');
 
 const marked = require('marked');
 
+const highlightJs = require('highlight.js/lib/highlight.js');
+const highlightJsDiff = require('highlight.js/lib/languages/diff.js');
+const highlightJsApache = require('highlight.js/lib/languages/apache.js');
+const highlightJsMakefile = require('highlight.js/lib/languages/makefile.js');
+const highlightJsHttp = require('highlight.js/lib/languages/http.js');
+const highlightJsJson = require('highlight.js/lib/languages/json.js');
+const highlightJsMarkdown = require('highlight.js/lib/languages/markdown.js');
+const highlightJsJavascript = require('highlight.js/lib/languages/javascript.js');
+const highlightJsCss = require('highlight.js/lib/languages/css.js');
+const highlightJsNginx = require('highlight.js/lib/languages/nginx.js');
+const highlightJsObjectivec = require('highlight.js/lib/languages/objectivec.js');
+const highlightJsPython = require('highlight.js/lib/languages/python.js');
+const highlightJsXml = require('highlight.js/lib/languages/xml.js');
+const highlightJsPerl = require('highlight.js/lib/languages/perl.js');
+const highlightJsBash = require('highlight.js/lib/languages/bash.js');
+const highlightJsPhp = require('highlight.js/lib/languages/php.js');
+const highlightJsCoffeescript = require('highlight.js/lib/languages/coffeescript.js');
+const highlightJsCs = require('highlight.js/lib/languages/cs.js');
+const highlightJsCpp = require('highlight.js/lib/languages/cpp.js');
+const highlightJsSql = require('highlight.js/lib/languages/sql.js');
+const highlightJsGo = require('highlight.js/lib/languages/go.js');
+const highlightJsRuby = require('highlight.js/lib/languages/ruby.js');
+
+const Constants = require('../utils/constants.jsx');
+const HighlightedLanguages = Constants.HighlightedLanguages;
+
 class MattermostInlineLexer extends marked.InlineLexer {
     constructor(links, options) {
         super(links, options);
@@ -51,6 +77,41 @@ class MattermostMarkdownRenderer extends marked.Renderer {
         this.text = this.text.bind(this);
 
         this.formattingOptions = formattingOptions;
+
+        highlightJs.registerLanguage('diff', highlightJsDiff);
+        highlightJs.registerLanguage('apache', highlightJsApache);
+        highlightJs.registerLanguage('makefile', highlightJsMakefile);
+        highlightJs.registerLanguage('http', highlightJsHttp);
+        highlightJs.registerLanguage('json', highlightJsJson);
+        highlightJs.registerLanguage('markdown', highlightJsMarkdown);
+        highlightJs.registerLanguage('javascript', highlightJsJavascript);
+        highlightJs.registerLanguage('css', highlightJsCss);
+        highlightJs.registerLanguage('nginx', highlightJsNginx);
+        highlightJs.registerLanguage('objectivec', highlightJsObjectivec);
+        highlightJs.registerLanguage('python', highlightJsPython);
+        highlightJs.registerLanguage('xml', highlightJsXml);
+        highlightJs.registerLanguage('perl', highlightJsPerl);
+        highlightJs.registerLanguage('bash', highlightJsBash);
+        highlightJs.registerLanguage('php', highlightJsPhp);
+        highlightJs.registerLanguage('coffeescript', highlightJsCoffeescript);
+        highlightJs.registerLanguage('cs', highlightJsCs);
+        highlightJs.registerLanguage('cpp', highlightJsCpp);
+        highlightJs.registerLanguage('sql', highlightJsSql);
+        highlightJs.registerLanguage('go', highlightJsGo);
+        highlightJs.registerLanguage('ruby', highlightJsRuby);
+    }
+
+    code(code, language) {
+        if (!language || highlightJs.listLanguages().indexOf(language) < 0) {
+            let parsed = super.code(code, language);
+            return '<code class="hljs">' + parsed.substr(11, parsed.length - 17);
+        }
+
+        let parsed = highlightJs.highlight(language, code);
+        return '<div class="post-body--code">' +
+            '<span class="post-body--code__language">' + HighlightedLanguages[language] + '</span>' +
+            '<code style="white-space: pre;" class="hljs">' + parsed.value + '</code>' +
+            '</div>';
     }
 
     br() {
-- 
cgit v1.2.3-1-g7c22


From aeb0a08d077d35e46451142be1e06a4718a2d6bb Mon Sep 17 00:00:00 2001
From: Florian Orben <florian.orben@gmail.com>
Date: Sat, 24 Oct 2015 16:03:12 +0200
Subject: fix markup if code is of unknown language

---
 web/react/utils/markdown.jsx | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

(limited to 'web/react/utils/markdown.jsx')

diff --git a/web/react/utils/markdown.jsx b/web/react/utils/markdown.jsx
index 5be01d055..ca26f7701 100644
--- a/web/react/utils/markdown.jsx
+++ b/web/react/utils/markdown.jsx
@@ -104,7 +104,7 @@ class MattermostMarkdownRenderer extends marked.Renderer {
     code(code, language) {
         if (!language || highlightJs.listLanguages().indexOf(language) < 0) {
             let parsed = super.code(code, language);
-            return '<code class="hljs">' + parsed.substr(11, parsed.length - 17);
+            return '<code class="hljs">' + $(parsed).text() + '</code>';
         }
 
         let parsed = highlightJs.highlight(language, code);
-- 
cgit v1.2.3-1-g7c22


From 742424228414793e6aaa06ce8a9de182cdfb2957 Mon Sep 17 00:00:00 2001
From: Florian Orben <florian.orben@gmail.com>
Date: Sat, 24 Oct 2015 21:55:16 +0200
Subject: Add java and ini language

Forgot to add those altough they are quite common
---
 web/react/utils/markdown.jsx | 4 ++++
 1 file changed, 4 insertions(+)

(limited to 'web/react/utils/markdown.jsx')

diff --git a/web/react/utils/markdown.jsx b/web/react/utils/markdown.jsx
index ca26f7701..b5d239eb5 100644
--- a/web/react/utils/markdown.jsx
+++ b/web/react/utils/markdown.jsx
@@ -28,6 +28,8 @@ const highlightJsCpp = require('highlight.js/lib/languages/cpp.js');
 const highlightJsSql = require('highlight.js/lib/languages/sql.js');
 const highlightJsGo = require('highlight.js/lib/languages/go.js');
 const highlightJsRuby = require('highlight.js/lib/languages/ruby.js');
+const highlightJsJava = require('highlight.js/lib/languages/java.js');
+const highlightJsIni = require('highlight.js/lib/languages/ini.js');
 
 const Constants = require('../utils/constants.jsx');
 const HighlightedLanguages = Constants.HighlightedLanguages;
@@ -99,6 +101,8 @@ class MattermostMarkdownRenderer extends marked.Renderer {
         highlightJs.registerLanguage('sql', highlightJsSql);
         highlightJs.registerLanguage('go', highlightJsGo);
         highlightJs.registerLanguage('ruby', highlightJsRuby);
+        highlightJs.registerLanguage('java', highlightJsJava);
+        highlightJs.registerLanguage('ini', highlightJsIni);
     }
 
     code(code, language) {
-- 
cgit v1.2.3-1-g7c22


From bad01d40a2c9354573bfe1c4b9d33a05ffbe9b0f Mon Sep 17 00:00:00 2001
From: Florian Orben <florian.orben@gmail.com>
Date: Wed, 28 Oct 2015 19:36:34 +0100
Subject: escape user input

---
 web/react/utils/markdown.jsx | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

(limited to 'web/react/utils/markdown.jsx')

diff --git a/web/react/utils/markdown.jsx b/web/react/utils/markdown.jsx
index b5d239eb5..84690150a 100644
--- a/web/react/utils/markdown.jsx
+++ b/web/react/utils/markdown.jsx
@@ -108,13 +108,13 @@ class MattermostMarkdownRenderer extends marked.Renderer {
     code(code, language) {
         if (!language || highlightJs.listLanguages().indexOf(language) < 0) {
             let parsed = super.code(code, language);
-            return '<code class="hljs">' + $(parsed).text() + '</code>';
+            return '<div class="post-body--code"><code class="hljs">' + TextFormatting.sanitizeHtml($(parsed).text()) + '</code></div>';
         }
 
         let parsed = highlightJs.highlight(language, code);
         return '<div class="post-body--code">' +
             '<span class="post-body--code__language">' + HighlightedLanguages[language] + '</span>' +
-            '<code style="white-space: pre;" class="hljs">' + parsed.value + '</code>' +
+            '<code class="hljs">' + parsed.value + '</code>' +
             '</div>';
     }
 
-- 
cgit v1.2.3-1-g7c22


From 3c8fd9942557aee8a1bc06b2b973bb1e3f9519d0 Mon Sep 17 00:00:00 2001
From: Florian Orben <florian.orben@gmail.com>
Date: Wed, 28 Oct 2015 19:39:10 +0100
Subject: use XML syntax highlighting if provided language is html

---
 web/react/utils/markdown.jsx | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

(limited to 'web/react/utils/markdown.jsx')

diff --git a/web/react/utils/markdown.jsx b/web/react/utils/markdown.jsx
index 84690150a..179416ea0 100644
--- a/web/react/utils/markdown.jsx
+++ b/web/react/utils/markdown.jsx
@@ -106,14 +106,20 @@ class MattermostMarkdownRenderer extends marked.Renderer {
     }
 
     code(code, language) {
-        if (!language || highlightJs.listLanguages().indexOf(language) < 0) {
-            let parsed = super.code(code, language);
+        let usedLanguage = language;
+
+        if (String(usedLanguage).toLocaleLowerCase() === 'html') {
+            usedLanguage = 'xml';
+        }
+
+        if (!usedLanguage || highlightJs.listLanguages().indexOf(usedLanguage) < 0) {
+            let parsed = super.code(code, usedLanguage);
             return '<div class="post-body--code"><code class="hljs">' + TextFormatting.sanitizeHtml($(parsed).text()) + '</code></div>';
         }
 
-        let parsed = highlightJs.highlight(language, code);
+        let parsed = highlightJs.highlight(usedLanguage, code);
         return '<div class="post-body--code">' +
-            '<span class="post-body--code__language">' + HighlightedLanguages[language] + '</span>' +
+            '<span class="post-body--code__language">' + HighlightedLanguages[usedLanguage] + '</span>' +
             '<code class="hljs">' + parsed.value + '</code>' +
             '</div>';
     }
-- 
cgit v1.2.3-1-g7c22

