Init Markdown support.

This commit is contained in:
Black Hat 2018-07-10 21:08:17 +08:00
parent 0b2f252c42
commit 65ff905a7c
4 changed files with 40 additions and 19 deletions

View File

@ -48,7 +48,7 @@ var markdown_parser = function(str){
return '<h'+level+'>'+header.trim()+'</h'+level+'>'; return '<h'+level+'>'+header.trim()+'</h'+level+'>';
}], }],
// images // images
['/\\!\\[([^\\[]+)\\]\\(([^\\(]+)\\)/g', '<img src=\"\\2\" alt=\"\\1\" />'], // ['/\\!\\[([^\\[]+)\\]\\(([^\\(]+)\\)/g', '<img src=\"\\2\" alt=\"\\1\" />'],
// link // link
['/\\[([^\\[]+)\\]\\(([^\\(]+)\\)/g', '<a href=\"\\2\">\\1</a>'], ['/\\[([^\\[]+)\\]\\(([^\\(]+)\\)/g', '<a href=\"\\2\">\\1</a>'],
// bold // bold
@ -60,25 +60,25 @@ var markdown_parser = function(str){
// quote // quote
['/\\:\\"(.*?)\\"\\:/g', '<q>\\1</q>'], ['/\\:\\"(.*?)\\"\\:/g', '<q>\\1</q>'],
// unordered list // unordered list
['/\\n\\*(.*)/g', function(item){ // ['/\\n\\*(.*)/g', function(item){
return '<ul>\n<li>'+item.trim()+'</li>\n</ul>'; // return '<ul>\n<li>'+item.trim()+'</li>\n</ul>';
}], // }],
// ordered list // ordered list
['/\\n[0-9]+\\.(.*)/g', function(item){ // ['/\\n[0-9]+\\.(.*)/g', function(item){
return '<ol>\n<li>'+item.trim()+'</li>\n</ol>'; // return '<ol>\n<li>'+item.trim()+'</li>\n</ol>';
}], // }],
// blockquote // blockquote
['/\\n\\>(.*)/g', function(str){ ['/\\n\\>(.*)/g', function(str){
return '<blockquote>'+str.trim()+'</blockquote>'; return '<blockquote>'+str.trim()+'</blockquote>';
}],
// paragraphs
['/\\n[^\\n]+\\n/g', function(line){
line = line.trim();
if(line[0] === '<'){
return line;
}
return '\n<p>'+line+'</p>\n';
}] }]
// paragraphs
// ['/\\n[^\\n]+\\n/g', function(line){
// line = line.trim();
// if(line[0] === '<'){
// return line;
// }
// return '\n<p>'+line+'</p>\n';
// }]
], fixes = [ ], fixes = [
['/<\\/ul>\n<ul>/g', '\n'], ['/<\\/ul>\n<ul>/g', '\n'],
['/<\\/ol>\n<ol>/g', '\n'], ['/<\\/ol>\n<ol>/g', '\n'],

@ -1 +1 @@
Subproject commit fe4bedeb349ed867feba7cb3c996a97f726d2083 Subproject commit e66cae5fd3e74c5839804e560332e5690709931a

View File

@ -25,7 +25,7 @@ AvatarContainer {
anchors.margins: 12 anchors.margins: 12
wrapMode: Label.Wrap wrapMode: Label.Wrap
linkColor: isNotice ? Material.accent : sentByMe ? Material.accent : "white" linkColor: isNotice ? Material.accent : sentByMe ? Material.accent : "white"
textFormat: Text.StyledText textFormat: contentType === "text/html" ? Text.RichText : Text.StyledText
} }
} }
} }

View File

@ -195,13 +195,34 @@ Item {
var type = "m.text" var type = "m.text"
var PREFIX_ME = '/me ' var PREFIX_ME = '/me '
var PREFIX_RAINBOW = '/rainbow'
if (text.indexOf(PREFIX_ME) === 0) { if (text.indexOf(PREFIX_ME) === 0) {
text = text.substr(PREFIX_ME.length) text = text.substr(PREFIX_ME.length)
type = "m.emote" type = "m.emote"
} }
if (text.indexOf(PREFIX_RAINBOW) === 0) {
text = text.substr(PREFIX_RAINBOW.length)
}
// var parsedText = Markdown.markdown_parser(text) var result = parse(text)
currentRoom.postMessage(type, text) var parsedText = result[0]
var isMarkdown = result [1]
if (isMarkdown) currentRoom.postHtmlMessage(text, parsedText, type)
else currentRoom.postMessage(type, text)
}
function parse(text) {
if (!testHTML(text)) {
var parsedText = Markdown.markdown_parser(text)
if (testHTML(parsedText)) return [parsedText, true]
}
return [text, false]
}
function testHTML(text) {
var re = new RegExp("(<([^>]+)>)")
return re.test(text)
} }
} }