diff --git a/src/components/views/elements/MessageEditor.js b/src/components/views/elements/MessageEditor.js index b07eca22b6..cb5767b192 100644 --- a/src/components/views/elements/MessageEditor.js +++ b/src/components/views/elements/MessageEditor.js @@ -22,7 +22,7 @@ import dis from '../../../dispatcher'; import EditorModel from '../../../editor/model'; import {setCaretPosition} from '../../../editor/caret'; import {getCaretOffsetAndText} from '../../../editor/dom'; -import {htmlSerialize, textSerialize, requiresHtml} from '../../../editor/serialize'; +import {htmlSerializeIfNeeded, textSerialize} from '../../../editor/serialize'; import {parseEvent} from '../../../editor/deserialize'; import Autocomplete from '../rooms/Autocomplete'; import {PartCreator} from '../../../editor/parts'; @@ -128,9 +128,10 @@ export default class MessageEditor extends React.Component { msgtype: newContent.msgtype, body: ` * ${newContent.body}`, }; - if (requiresHtml(this.model)) { + const formattedBody = htmlSerializeIfNeeded(this.model); + if (formattedBody) { newContent.format = "org.matrix.custom.html"; - newContent.formatted_body = htmlSerialize(this.model); + newContent.formatted_body = formattedBody; contentBody.format = newContent.format; contentBody.formatted_body = ` * ${newContent.formatted_body}`; } diff --git a/src/editor/serialize.js b/src/editor/serialize.js index 1724e4a2b7..73fbbe5d01 100644 --- a/src/editor/serialize.js +++ b/src/editor/serialize.js @@ -15,21 +15,31 @@ See the License for the specific language governing permissions and limitations under the License. */ -export function htmlSerialize(model) { +import Markdown from '../Markdown'; + +export function mdSerialize(model) { return model.parts.reduce((html, part) => { switch (part.type) { case "newline": - return html + "
"; + return html + "\n"; case "plain": case "pill-candidate": return html + part.text; case "room-pill": case "user-pill": - return html + `${part.text}`; + return html + `[${part.text}](https://matrix.to/#/${part.resourceId})`; } }, ""); } +export function htmlSerializeIfNeeded(model) { + const md = mdSerialize(model); + const parser = new Markdown(md); + if (!parser.isPlainText()) { + return parser.toHTML(); + } +} + export function textSerialize(model) { return model.parts.reduce((text, part) => { switch (part.type) {