Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Treat lists with a single empty item as plain text, not Markdown. #6833

Merged
merged 6 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/Markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,30 @@ export default class Markdown {
isPlainText(): boolean {
const walker = this.parsed.walker();

let ev;
const emptyItemWithNoSiblings = (node: commonmark.Node) => {
return !node.prev && !node.next && !node.firstChild;
};

let ev: commonmark.NodeWalkingStep;
while ( (ev = walker.next()) ) {
const node = ev.node;
if (TEXT_NODES.indexOf(node.type) > -1) {
// definitely text
continue;
} else if (node.type == 'list' || node.type == 'item') {
// Special handling for inputs like `+`, `*`, `-` and `2021.` which
// would otherwise be treated as a list of a single empty item.
// See https://github.com/vector-im/element-web/issues/7631
if (node.type == 'list' && emptyItemWithNoSiblings(node.firstChild)) {
// A list with a single empty item is treated as plain text.
continue;
} else if (node.type == 'item' && emptyItemWithNoSiblings(node)) {
// An empty list item with no sibling items is treated as plain text.
continue;
} else {
// Everything else is actual lists and therefore not plaintext.
return false;
}
} else if (node.type == 'html_inline' || node.type == 'html_block') {
// if it's an allowed html tag, we need to render it and therefore
// we will need to use HTML. If it's not allowed, it's not HTML since
Expand All @@ -97,6 +115,7 @@ export default class Markdown {
return false;
}
}

return true;
}

Expand Down
21 changes: 21 additions & 0 deletions test/editor/serialize-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,27 @@ describe('editor/serialize', function() {
const html = htmlSerializeIfNeeded(model, {});
expect(html).toBe("<em>hello</em> world");
});
it('lists with a single empty item are not considered markdown', function() {
const pc = createPartCreator();

const model1 = new EditorModel([pc.plain("-")]);
const html1 = htmlSerializeIfNeeded(model1, {});
expect(html1).toBe(undefined);

const model2 = new EditorModel([pc.plain("* ")]);
const html2 = htmlSerializeIfNeeded(model2, {});
expect(html2).toBe(undefined);

const model3 = new EditorModel([pc.plain("2021.")]);
const html3 = htmlSerializeIfNeeded(model3, {});
expect(html3).toBe(undefined);
});
it('lists with a single non-empty item are still markdown', function() {
const pc = createPartCreator();
const model = new EditorModel([pc.plain("2021. foo")]);
const html = htmlSerializeIfNeeded(model, {});
expect(html).toBe("<ol start=\"2021\">\n<li>foo</li>\n</ol>\n");
});
it('displaynames ending in a backslash work', function() {
const pc = createPartCreator();
const model = new EditorModel([pc.userPill("Displayname\\", "@user:server")]);
Expand Down