From 92ba6c98023c520066cdf8d462e68e5ff45a6ed2 Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 9 Aug 2021 14:44:06 +0530 Subject: [PATCH] added markdown support --- package-lock.json | 26 ++++++++++++++++++++++++-- package.json | 1 + src/client/state/RoomsInput.js | 13 +++++++++++++ 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index 18dc493..517c11c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3359,6 +3359,24 @@ "integrity": "sha1-3dgA2gxmEnOTzKWVDqloo6rxJTs=", "dev": true }, + "commonmark": { + "version": "0.30.0", + "resolved": "https://registry.npmjs.org/commonmark/-/commonmark-0.30.0.tgz", + "integrity": "sha512-j1yoUo4gxPND1JWV9xj5ELih0yMv1iCWDG6eEQIPLSWLxzCXiFoyS7kvB+WwU+tZMf4snwJMMtaubV0laFpiBA==", + "requires": { + "entities": "~2.0", + "mdurl": "~1.0.1", + "minimist": ">=1.2.2", + "string.prototype.repeat": "^0.2.0" + }, + "dependencies": { + "entities": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/entities/-/entities-2.0.3.tgz", + "integrity": "sha512-MyoZ0jgnLvB2X3Lg5HqpFmn1kybDiIfEQmKzTb5apr51Rb+T3KdmMiqa70T+bhGnyv7bQ6WMj2QMHpGMmlrUYQ==" + } + } + }, "component-emitter": { "version": "1.3.0", "resolved": "https://registry.npmjs.org/component-emitter/-/component-emitter-1.3.0.tgz", @@ -8276,8 +8294,7 @@ "minimist": { "version": "1.2.5", "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.5.tgz", - "integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==", - "dev": true + "integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==" }, "minipass": { "version": "3.1.3", @@ -12290,6 +12307,11 @@ "side-channel": "^1.0.4" } }, + "string.prototype.repeat": { + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/string.prototype.repeat/-/string.prototype.repeat-0.2.0.tgz", + "integrity": "sha1-q6Nt4I3O5qWjN9SbLqHaGyj8Ds8=" + }, "string.prototype.trimend": { "version": "1.0.4", "resolved": "https://registry.npmjs.org/string.prototype.trimend/-/string.prototype.trimend-1.0.4.tgz", diff --git a/package.json b/package.json index bb288ed..d12a016 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ "@tippyjs/react": "^4.2.5", "babel-polyfill": "^6.26.0", "browser-encrypt-attachment": "^0.3.0", + "commonmark": "^0.30.0", "dateformat": "^4.5.1", "emojibase-data": "^6.2.0", "flux": "^4.0.1", diff --git a/src/client/state/RoomsInput.js b/src/client/state/RoomsInput.js index eb704a7..970bd62 100644 --- a/src/client/state/RoomsInput.js +++ b/src/client/state/RoomsInput.js @@ -1,6 +1,8 @@ import EventEmitter from 'events'; +import { Parser, HtmlRenderer } from 'commonmark'; import encrypt from 'browser-encrypt-attachment'; import cons from './cons'; +import settings from './settings'; function getImageDimension(file) { return new Promise((resolve) => { @@ -78,6 +80,13 @@ function getVideoThumbnail(video, width, height, mimeType) { }); } +function getFormatedBody(markdown) { + const reader = new Parser(); + const writer = new HtmlRenderer(); + const parsed = reader.parse(markdown); + return writer.render(parsed); +} + class RoomsInput extends EventEmitter { constructor(mx) { super(); @@ -163,6 +172,10 @@ class RoomsInput extends EventEmitter { body: input.message, msgtype: 'm.text', }; + if (settings.isMarkdown) { + content.format = 'org.matrix.custom.html'; + content.formatted_body = getFormatedBody(input.message); + } this.matrixClient.sendMessage(roomId, content); }