added markdown support

This commit is contained in:
unknown 2021-08-09 14:44:06 +05:30
parent 0286a51525
commit 92ba6c9802
3 changed files with 38 additions and 2 deletions

26
package-lock.json generated
View file

@ -3359,6 +3359,24 @@
"integrity": "sha1-3dgA2gxmEnOTzKWVDqloo6rxJTs=", "integrity": "sha1-3dgA2gxmEnOTzKWVDqloo6rxJTs=",
"dev": true "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": { "component-emitter": {
"version": "1.3.0", "version": "1.3.0",
"resolved": "https://registry.npmjs.org/component-emitter/-/component-emitter-1.3.0.tgz", "resolved": "https://registry.npmjs.org/component-emitter/-/component-emitter-1.3.0.tgz",
@ -8276,8 +8294,7 @@
"minimist": { "minimist": {
"version": "1.2.5", "version": "1.2.5",
"resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.5.tgz", "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.5.tgz",
"integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==", "integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw=="
"dev": true
}, },
"minipass": { "minipass": {
"version": "3.1.3", "version": "3.1.3",
@ -12290,6 +12307,11 @@
"side-channel": "^1.0.4" "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": { "string.prototype.trimend": {
"version": "1.0.4", "version": "1.0.4",
"resolved": "https://registry.npmjs.org/string.prototype.trimend/-/string.prototype.trimend-1.0.4.tgz", "resolved": "https://registry.npmjs.org/string.prototype.trimend/-/string.prototype.trimend-1.0.4.tgz",

View file

@ -19,6 +19,7 @@
"@tippyjs/react": "^4.2.5", "@tippyjs/react": "^4.2.5",
"babel-polyfill": "^6.26.0", "babel-polyfill": "^6.26.0",
"browser-encrypt-attachment": "^0.3.0", "browser-encrypt-attachment": "^0.3.0",
"commonmark": "^0.30.0",
"dateformat": "^4.5.1", "dateformat": "^4.5.1",
"emojibase-data": "^6.2.0", "emojibase-data": "^6.2.0",
"flux": "^4.0.1", "flux": "^4.0.1",

View file

@ -1,6 +1,8 @@
import EventEmitter from 'events'; import EventEmitter from 'events';
import { Parser, HtmlRenderer } from 'commonmark';
import encrypt from 'browser-encrypt-attachment'; import encrypt from 'browser-encrypt-attachment';
import cons from './cons'; import cons from './cons';
import settings from './settings';
function getImageDimension(file) { function getImageDimension(file) {
return new Promise((resolve) => { 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 { class RoomsInput extends EventEmitter {
constructor(mx) { constructor(mx) {
super(); super();
@ -163,6 +172,10 @@ class RoomsInput extends EventEmitter {
body: input.message, body: input.message,
msgtype: 'm.text', msgtype: 'm.text',
}; };
if (settings.isMarkdown) {
content.format = 'org.matrix.custom.html';
content.formatted_body = getFormatedBody(input.message);
}
this.matrixClient.sendMessage(roomId, content); this.matrixClient.sendMessage(roomId, content);
} }