diff --git a/imports/Spectral/Panel/RoomPanel.qml b/imports/Spectral/Panel/RoomPanel.qml
index d7f7a26..32bcaf3 100644
--- a/imports/Spectral/Panel/RoomPanel.qml
+++ b/imports/Spectral/Panel/RoomPanel.qml
@@ -13,8 +13,6 @@ import Spectral 0.1
import Spectral.Setting 0.1
import SortFilterProxyModel 0.2
-import "qrc:/js/md.js" as Markdown
-
Item {
property var currentRoom: null
diff --git a/imports/Spectral/Panel/RoomPanelInput.qml b/imports/Spectral/Panel/RoomPanelInput.qml
index a172e7a..90a0b28 100644
--- a/imports/Spectral/Panel/RoomPanelInput.qml
+++ b/imports/Spectral/Panel/RoomPanelInput.qml
@@ -10,8 +10,6 @@ import Spectral.Setting 0.1
import Spectral 0.1
-import "qrc:/js/md.js" as Markdown
-
Control {
property alias isReply: replyItem.visible
property var replyUser
@@ -349,8 +347,7 @@ Control {
}
if (text.indexOf(PREFIX_MARKDOWN) === 0) {
text = text.substr(PREFIX_MARKDOWN.length)
- var parsedText = Markdown.markdown_parser(text)
- currentRoom.postHtmlMessage(text, parsedText, RoomMessageEvent.Text)
+ currentRoom.postMarkdownText(text)
return
}
diff --git a/js/md.js b/js/md.js
deleted file mode 100644
index ceae2ef..0000000
--- a/js/md.js
+++ /dev/null
@@ -1,90 +0,0 @@
-.pragma library
-
-var preg_replace=function(a,b,c,d){void 0===d&&(d=-1);var e=a.substr(a.lastIndexOf(a[0])+1),f=a.substr(1,a.lastIndexOf(a[0])-1),g=RegExp(f,e),i=[],j=0,k=0,l=c,m=[];if(-1===d){do m=g.exec(c),null!==m&&i.push(m);while(null!==m&&-1!==e.indexOf("g"))}else i.push(g.exec(c));for(j=i.length-1;j>-1;j--){for(m=b,k=i[j].length;k>-1;k--)m=m.replace("${"+k+"}",i[j][k]).replace("$"+k,i[j][k]).replace("\\"+k,i[j][k]);l=l.replace(i[j][0],m)}return l};
-
-var markdown_parser = function(str){
-
- var rules = [
- // headers
- ['/(#+)(.*)/g', function(chars, header){
- var level = chars.length;
- return '\\2'],
- // quote
- ['/\\:\\"(.*?)\\"\\:/g', '\\1
'],
- // unordered list
-// ['/\\n\\*(.*)/g', function(item){
-// return '
'+str.trim()+''; - }] - // paragraphs -// ['/\\n[^\\n]+\\n/g', function(line){ -// line = line.trim(); -// if(line[0] === '<'){ -// return line; -// } -// return '\n
'+line+'
\n'; -// }] - ], fixes = [ - ['/<\\/ul>\n/g', "\n"] - ]; - - var parse_line = function(str){ - str = "\n" + str.trim() + "\n"; - for(var i = 0, j = rules.length; i < j; i++){ - if(typeof rules[i][1] == 'function') { - var _flag = rules[i][0].substr(rules[i][0].lastIndexOf(rules[i][0][0])+1), - _pattern = rules[i][0].substr(1, rules[i][0].lastIndexOf(rules[i][0][0])-1), - reg = new RegExp(_pattern, _flag); - - var matches = reg.exec(str); - if(matches !== null){ - if(matches.length > 1){ - str = preg_replace(rules[i][0], rules[i][1](matches[1], matches[2]), str); - } - else - { - str = preg_replace(rules[i][0], rules[i][1](matches[0]), str); - } - } - } - else { - str = preg_replace(rules[i][0], rules[i][1], str); - } - } - return str.trim(); - }; - - str = str.split('\n'); - var rtn = []; - for(var i = 0, j = str.length; i < j; i++){ - rtn.push(parse_line(str[i])); - } - - rtn = rtn.join('\n'); - - for(i = 0, j = fixes.length; i < j; i++){ - rtn = preg_replace(fixes[i][0], fixes[i][1], rtn); - } - - return rtn; -}; diff --git a/res.qrc b/res.qrc index e5c3da8..d5d29dd 100644 --- a/res.qrc +++ b/res.qrc @@ -2,7 +2,6 @@qtquickcontrols2.conf qml/main.qml -js/md.js imports/Spectral/Component/Emoji/EmojiPicker.qml imports/Spectral/Component/Emoji/qmldir imports/Spectral/Component/Timeline/DownloadableContent.qml diff --git a/spectral.pro b/spectral.pro index 2620c0b..ae86e26 100644 --- a/spectral.pro +++ b/spectral.pro @@ -20,6 +20,9 @@ isEmpty(USE_SYSTEM_SORTFILTERPROXYMODEL) { isEmpty(USE_SYSTEM_QMATRIXCLIENT) { USE_SYSTEM_QMATRIXCLIENT = false } +isEmpty(USE_SYSTEM_CMARK) { + USE_SYSTEM_CMARK = false +} isEmpty(BUNDLE_FONT) { BUNDLE_FONT = false } @@ -36,6 +39,12 @@ $$USE_SYSTEM_SORTFILTERPROXYMODEL { message("Falling back to built-in SortFilterProxyModel.") include(include/SortFilterProxyModel/SortFilterProxyModel.pri) } +$$USE_SYSTEM_CMARK { + PKGCONFIG += libcmark +} else { + message("Falling back to built-in CMark.") + include(include/SortFilterProxyModel/SortFilterProxyModel.pri) +} # The following define makes your compiler emit warnings if you use # any feature of Qt which as been marked deprecated (the exact warnings diff --git a/src/spectralroom.cpp b/src/spectralroom.cpp index f9aded9..8c52651 100644 --- a/src/spectralroom.cpp +++ b/src/spectralroom.cpp @@ -14,6 +14,8 @@ #include#include +#include "cmark.h" + #include "utils.h" SpectralRoom::SpectralRoom(Connection* connection, QString roomId, @@ -233,3 +235,10 @@ QVariantList SpectralRoom::getUsers(const QString& prefix) { return matchedList; } + +QString SpectralRoom::postMarkdownText(const QString& markdown) { + QByteArray local = markdown.toLocal8Bit(); + const char* data = local.data(); + QString html = cmark_markdown_to_html(data, local.length(), 0); + return postHtmlText(markdown, html); +} diff --git a/src/spectralroom.h b/src/spectralroom.h index c4656fc..da2ab97 100644 --- a/src/spectralroom.h +++ b/src/spectralroom.h @@ -74,6 +74,8 @@ class SpectralRoom : public Room { Q_INVOKABLE QVariantList getUsers(const QString& prefix); + Q_INVOKABLE QString postMarkdownText(const QString& markdown); + private: QString m_cachedInput; QSet highlights;