Skip to content
This repository was archived by the owner on Jul 17, 2020. It is now read-only.

Commit 7c5498b

Browse files
author
Zirak
committed
incorporating Markdown decoding, fixes #150
1 parent 95e5b5c commit 7c5498b

File tree

4 files changed

+97
-7
lines changed

4 files changed

+97
-7
lines changed

master.js

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,49 @@ var IO = window.IO = {
102102
}
103103
};
104104

105-
IO.decodehtmlEntities = (function (){
105+
//turns some html tags into markdown. a major assumption is that the input is
106+
// properly sanitised - that is, all <, &, etc entered by the user got turned
107+
// into html entities.
108+
IO.htmlToMarkdown = (function () {
109+
110+
// A string value is the delimiter (what replaces the tag)
111+
var markdown = {
112+
i : '*',
113+
b : '**',
114+
strike : '---',
115+
code : '`',
116+
117+
a : function ( $0, $1, text ) {
118+
var href = /href="([^"]+?)"/.exec( $0 );
119+
120+
if ( !href ) {
121+
return $0;
122+
}
123+
return '[' + text + '](' + href[1] + ')';
124+
},
125+
};
126+
var htmlRe = /<(\S+)[^\>]*>([^<]+)<\/\1>/g;
127+
128+
return function ( html ) {
129+
var delim;
130+
131+
return html.replace( htmlRe, decodeHtml );
132+
133+
function decodeHtml ( $0, tag, text ) {
134+
if ( !markdown.hasOwnProperty(tag) ) {
135+
return $0;
136+
}
137+
138+
delim = markdown[ tag ];
139+
140+
return delim.apply ?
141+
markdown[ tag ].apply( markdown, arguments ) :
142+
delim + text + delim;
143+
}
144+
};
145+
}());
146+
147+
IO.decodehtmlEntities = (function () {
106148
var entities; //will be filled in the following line
107149
entities = {"quot":"\"","amp":"&","apos":"'","lt":"<","gt":">","nbsp":" ","iexcl":"¡","cent":"¢","pound":"£","curren":"¤","yen":"¥","brvbar":"¦","sect":"§","uml":"¨","copy":"©","ordf":"ª","laquo":"«","not":"¬","reg":"®","macr":"¯","deg":"°","plusmn":"±","sup2":"²","sup3":"³","acute":"´","micro":"µ","para":"¶","middot":"·","cedil":"¸","sup1":"¹","ordm":"º","raquo":"»","frac14":"¼","frac12":"½","frac34":"¾","iquest":"¿","Agrave":"À","Aacute":"Á","Acirc":"Â","Atilde":"Ã","Auml":"Ä","Aring":"Å","AElig":"Æ","Ccedil":"Ç","Egrave":"È","Eacute":"É","Ecirc":"Ê","Euml":"Ë","Igrave":"Ì","Iacute":"Í","Icirc":"Î","Iuml":"Ï","ETH":"Ð","Ntilde":"Ñ","Ograve":"Ò","Oacute":"Ó","Ocirc":"Ô","Otilde":"Õ","Ouml":"Ö","times":"×","Oslash":"Ø","Ugrave":"Ù","Uacute":"Ú","Ucirc":"Û","Uuml":"Ü","Yacute":"Ý","THORN":"Þ","szlig":"ß","agrave":"à","aacute":"á","acirc":"â","atilde":"ã","auml":"ä","aring":"å","aelig":"æ","ccedil":"ç","egrave":"è","eacute":"é","ecirc":"ê","euml":"ë","igrave":"ì","iacute":"í","icirc":"î","iuml":"ï","eth":"ð","ntilde":"ñ","ograve":"ò","oacute":"ó","ocirc":"ô","otilde":"õ","ouml":"ö","divide":"÷","oslash":"ø","ugrave":"ù","uacute":"ú","ucirc":"û","uuml":"ü","yacute":"ý","thorn":"þ","yuml":"ÿ","OElig":"Œ","oelig":"œ","Scaron":"Š","scaron":"š","Yuml":"Ÿ","fnof":"ƒ","circ":"ˆ","tilde":"˜","Alpha":"Α","Beta":"Β","Gamma":"Γ","Delta":"Δ","Epsilon":"Ε","Zeta":"Ζ","Eta":"Η","Theta":"Θ","Iota":"Ι","Kappa":"Κ","Lambda":"Λ","Mu":"Μ","Nu":"Ν","Xi":"Ξ","Omicron":"Ο","Pi":"Π","Rho":"Ρ","Sigma":"Σ","Tau":"Τ","Upsilon":"Υ","Phi":"Φ","Chi":"Χ","Psi":"Ψ","Omega":"Ω","alpha":"α","beta":"β","gamma":"γ","delta":"δ","epsilon":"ε","zeta":"ζ","eta":"η","theta":"θ","iota":"ι","kappa":"κ","lambda":"λ","mu":"μ","nu":"ν","xi":"ξ","omicron":"ο","pi":"π","rho":"ρ","sigmaf":"ς","sigma":"σ","tau":"τ","upsilon":"υ","phi":"φ","chi":"χ","psi":"ψ","omega":"ω","thetasym":"ϑ","upsih":"ϒ","piv":"ϖ","ensp":" ","emsp":" ","thinsp":" ","ndash":"–","mdash":"—","lsquo":"‘","rsquo":"’","sbquo":"‚","ldquo":"“","rdquo":"”","bdquo":"„","dagger":"†","Dagger":"‡","bull":"•","hellip":"…","permil":"‰","prime":"′","Prime":"″","lsaquo":"‹","rsaquo":"›","oline":"‾","frasl":"⁄","euro":"€","image":"ℑ","weierp":"℘","real":"ℜ","trade":"™","alefsym":"ℵ","larr":"←","uarr":"↑","rarr":"→","darr":"↓","harr":"↔","crarr":"↵","lArr":"⇐","uArr":"⇑","rArr":"⇒","dArr":"⇓","hArr":"⇔","forall":"∀","part":"∂","exist":"∃","empty":"∅","nabla":"∇","isin":"∈","notin":"∉","ni":"∋","prod":"∏","sum":"∑","minus":"−","lowast":"∗","radic":"√","prop":"∝","infin":"∞","ang":"∠","and":"∧","or":"∨","cap":"∩","cup":"∪","int":"∫","there4":"∴","sim":"∼","cong":"≅","asymp":"≈","ne":"≠","equiv":"≡","le":"≤","ge":"≥","sub":"⊂","sup":"⊃","nsub":"⊄","sube":"⊆","supe":"⊇","oplus":"⊕","otimes":"⊗","perp":"⊥","sdot":"⋅","lceil":"⌈","rceil":"⌉","lfloor":"⌊","rfloor":"⌋","lang":"〈","rang":"〉","loz":"◊","spades":"♠","clubs":"♣","hearts":"♥","diams":"♦", "zwnj":"", "zwsp":""};
108150

@@ -791,7 +833,10 @@ var bot = window.bot = {
791833
prepareMessage : function ( msgObj ) {
792834
msgObj = this.adapter.transform( msgObj );
793835

794-
var msg = IO.decodehtmlEntities( msgObj.content );
836+
//decode markdown and html entities.
837+
var msg = IO.htmlToMarkdown( msgObj.content ); //#150
838+
msg = IO.decodehtmlEntities( msg );
839+
795840
//fixes issues #87 and #90 globally
796841
msg = msg.replace( /\u200b|\u200c/g, '' );
797842

master.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

source/IO.js

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,49 @@ var IO = window.IO = {
102102
}
103103
};
104104

105-
IO.decodehtmlEntities = (function (){
105+
//turns some html tags into markdown. a major assumption is that the input is
106+
// properly sanitised - that is, all <, &, etc entered by the user got turned
107+
// into html entities.
108+
IO.htmlToMarkdown = (function () {
109+
110+
// A string value is the delimiter (what replaces the tag)
111+
var markdown = {
112+
i : '*',
113+
b : '**',
114+
strike : '---',
115+
code : '`',
116+
117+
a : function ( $0, $1, text ) {
118+
var href = /href="([^"]+?)"/.exec( $0 );
119+
120+
if ( !href ) {
121+
return $0;
122+
}
123+
return '[' + text + '](' + href[1] + ')';
124+
},
125+
};
126+
var htmlRe = /<(\S+)[^\>]*>([^<]+)<\/\1>/g;
127+
128+
return function ( html ) {
129+
var delim;
130+
131+
return html.replace( htmlRe, decodeHtml );
132+
133+
function decodeHtml ( $0, tag, text ) {
134+
if ( !markdown.hasOwnProperty(tag) ) {
135+
return $0;
136+
}
137+
138+
delim = markdown[ tag ];
139+
140+
return delim.apply ?
141+
markdown[ tag ].apply( markdown, arguments ) :
142+
delim + text + delim;
143+
}
144+
};
145+
}());
146+
147+
IO.decodehtmlEntities = (function () {
106148
var entities; //will be filled in the following line
107149
//#build static/htmlEntities.js
108150

source/bot.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,10 @@ var bot = window.bot = {
135135
prepareMessage : function ( msgObj ) {
136136
msgObj = this.adapter.transform( msgObj );
137137

138-
var msg = IO.decodehtmlEntities( msgObj.content );
138+
//decode markdown and html entities.
139+
var msg = IO.htmlToMarkdown( msgObj.content ); //#150
140+
msg = IO.decodehtmlEntities( msg );
141+
139142
//fixes issues #87 and #90 globally
140143
msg = msg.replace( /\u200b|\u200c/g, '' );
141144

0 commit comments

Comments
 (0)