Add markdown support (enabled by default)

This commit is contained in:
Kegan Dougal 2015-11-20 16:08:57 +00:00
parent 4e01d27f7b
commit b12fc67a63
2 changed files with 65 additions and 12 deletions

View file

@ -24,6 +24,7 @@
"flux": "^2.0.3", "flux": "^2.0.3",
"glob": "^5.0.14", "glob": "^5.0.14",
"linkifyjs": "^2.0.0-beta.4", "linkifyjs": "^2.0.0-beta.4",
"marked": "^0.3.5",
"matrix-js-sdk": "https://github.com/matrix-org/matrix-js-sdk.git#develop", "matrix-js-sdk": "https://github.com/matrix-org/matrix-js-sdk.git#develop",
"optimist": "^0.6.1", "optimist": "^0.6.1",
"q": "^1.4.1", "q": "^1.4.1",

View file

@ -14,6 +14,17 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
var marked = require("marked");
marked.setOptions({
renderer: new marked.Renderer(),
gfm: true,
tables: true,
breaks: true,
pedantic: false,
sanitize: true,
smartLists: true,
smartypants: false
});
var MatrixClientPeg = require("../../MatrixClientPeg"); var MatrixClientPeg = require("../../MatrixClientPeg");
var SlashCommands = require("../../SlashCommands"); var SlashCommands = require("../../SlashCommands");
var Modal = require("../../Modal"); var Modal = require("../../Modal");
@ -32,11 +43,26 @@ var KeyCode = {
var TYPING_USER_TIMEOUT = 10000; var TYPING_USER_TIMEOUT = 10000;
var TYPING_SERVER_TIMEOUT = 30000; var TYPING_SERVER_TIMEOUT = 30000;
var MARKDOWN_ENABLED = true;
function mdownToHtml(mdown) {
var html = marked(mdown) || "";
html = html.trim();
// strip start and end <p> tags else you get 'orrible spacing
if (html.indexOf("<p>") === 0) {
html = html.substring("<p>".length);
}
if (html.lastIndexOf("</p>") === (html.length - "</p>".length)) {
html = html.substring(0, html.length - "</p>".length);
}
return html;
}
module.exports = { module.exports = {
oldScrollHeight: 0, oldScrollHeight: 0,
componentWillMount: function() { componentWillMount: function() {
this.markdownEnabled = MARKDOWN_ENABLED;
this.tabStruct = { this.tabStruct = {
completing: false, completing: false,
original: null, original: null,
@ -228,6 +254,27 @@ module.exports = {
onEnter: function(ev) { onEnter: function(ev) {
var contentText = this.refs.textarea.value; var contentText = this.refs.textarea.value;
// bodge for now to set markdown state on/off. We probably want a separate
// area for "local" commands which don't hit out to the server.
if (contentText.indexOf("/markdown") === 0) {
ev.preventDefault();
this.refs.textarea.value = '';
if (contentText.indexOf("/markdown on") === 0) {
this.markdownEnabled = true;
}
else if (contentText.indexOf("/markdown off") === 0) {
this.markdownEnabled = false;
}
else {
var ErrorDialog = sdk.getComponent("organisms.ErrorDialog");
Modal.createDialog(ErrorDialog, {
title: "Unknown command",
description: "Usage: /markdown on|off"
});
}
return;
}
var cmd = SlashCommands.processInput(this.props.room.roomId, contentText); var cmd = SlashCommands.processInput(this.props.room.roomId, contentText);
if (cmd) { if (cmd) {
ev.preventDefault(); ev.preventDefault();
@ -257,20 +304,25 @@ module.exports = {
return; return;
} }
var content = null; var isEmote = /^\/me /i.test(contentText);
if (/^\/me /i.test(contentText)) { var sendMessagePromise;
content = { if (isEmote) {
msgtype: 'm.emote', sendMessagePromise = MatrixClientPeg.get().sendEmoteMessage(
body: contentText.substring(4) this.props.room.roomId, contentText.substring(4)
}; );
} else { }
content = { else if (this.markdownEnabled) {
msgtype: 'm.text', sendMessagePromise = MatrixClientPeg.get().sendHtmlMessage(
body: contentText this.props.room.roomId, contentText, mdownToHtml(contentText)
}; );
}
else {
sendMessagePromise = MatrixClientPeg.get().sendTextMessage(
this.props.room.roomId, contentText
);
} }
MatrixClientPeg.get().sendMessage(this.props.room.roomId, content).then(function() { sendMessagePromise.then(function() {
dis.dispatch({ dis.dispatch({
action: 'message_sent' action: 'message_sent'
}); });