re-apply markdown when saving a message

This commit is contained in:
Bruno Windels 2019-05-21 17:59:54 +02:00
parent 5373007301
commit 53b6586986
2 changed files with 17 additions and 6 deletions
src
components/views/elements
editor

View file

@ -22,7 +22,7 @@ import dis from '../../../dispatcher';
import EditorModel from '../../../editor/model'; import EditorModel from '../../../editor/model';
import {setCaretPosition} from '../../../editor/caret'; import {setCaretPosition} from '../../../editor/caret';
import {getCaretOffsetAndText} from '../../../editor/dom'; import {getCaretOffsetAndText} from '../../../editor/dom';
import {htmlSerialize, textSerialize, requiresHtml} from '../../../editor/serialize'; import {htmlSerializeIfNeeded, textSerialize} from '../../../editor/serialize';
import {parseEvent} from '../../../editor/deserialize'; import {parseEvent} from '../../../editor/deserialize';
import Autocomplete from '../rooms/Autocomplete'; import Autocomplete from '../rooms/Autocomplete';
import {PartCreator} from '../../../editor/parts'; import {PartCreator} from '../../../editor/parts';
@ -128,9 +128,10 @@ export default class MessageEditor extends React.Component {
msgtype: newContent.msgtype, msgtype: newContent.msgtype,
body: ` * ${newContent.body}`, body: ` * ${newContent.body}`,
}; };
if (requiresHtml(this.model)) { const formattedBody = htmlSerializeIfNeeded(this.model);
if (formattedBody) {
newContent.format = "org.matrix.custom.html"; newContent.format = "org.matrix.custom.html";
newContent.formatted_body = htmlSerialize(this.model); newContent.formatted_body = formattedBody;
contentBody.format = newContent.format; contentBody.format = newContent.format;
contentBody.formatted_body = ` * ${newContent.formatted_body}`; contentBody.formatted_body = ` * ${newContent.formatted_body}`;
} }

View file

@ -15,21 +15,31 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
export function htmlSerialize(model) { import Markdown from '../Markdown';
export function mdSerialize(model) {
return model.parts.reduce((html, part) => { return model.parts.reduce((html, part) => {
switch (part.type) { switch (part.type) {
case "newline": case "newline":
return html + "<br />"; return html + "\n";
case "plain": case "plain":
case "pill-candidate": case "pill-candidate":
return html + part.text; return html + part.text;
case "room-pill": case "room-pill":
case "user-pill": case "user-pill":
return html + `<a href="https://matrix.to/#/${part.resourceId}">${part.text}</a>`; 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) { export function textSerialize(model) {
return model.parts.reduce((text, part) => { return model.parts.reduce((text, part) => {
switch (part.type) { switch (part.type) {