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

Commit fda0c2c

Browse files
authored
Fix markdown formatting for bold (#7257)
* Fix markdown formatting for bold Fix element-hq/element-web#4674 * I hate you too eslint
1 parent ab750ae commit fda0c2c

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

src/Markdown.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ function getTextUntilEndOrLinebreak(node: commonmark.Node) {
9393
return text;
9494
}
9595

96+
const formattingChangesByNodeType = {
97+
'emph': '_',
98+
'strong': '__',
99+
};
100+
96101
/**
97102
* Class that wraps commonmark, adding the ability to see whether
98103
* a given message actually uses any markdown syntax or whether
@@ -128,7 +133,7 @@ export default class Markdown {
128133
let text = '';
129134
let isInPara = false;
130135
let previousNode: commonmark.Node | null = null;
131-
let shouldUnlinkEmphasisNode = false;
136+
let shouldUnlinkFormattingNode = false;
132137
while ((event = walker.next())) {
133138
const { node } = event;
134139
if (node.type === 'paragraph') {
@@ -152,7 +157,7 @@ export default class Markdown {
152157
text += node.literal;
153158
}
154159
// We should not do this if previous node was not a textnode, as we can't combine it then.
155-
if (node.type === 'emph' && previousNode.type === 'text') {
160+
if ((node.type === 'emph' || node.type === 'strong') && previousNode.type === 'text') {
156161
if (event.entering) {
157162
const foundLinks = linkify.find(text);
158163
for (const { value } of foundLinks) {
@@ -161,7 +166,8 @@ export default class Markdown {
161166
* NOTE: This technically should unlink the emph node and create LINK nodes instead, adding all the next elements as siblings
162167
* but this solution seems to work well and is hopefully slightly easier to understand too
163168
*/
164-
const nonEmphasizedText = `_${node.firstChild.literal}_`;
169+
const format = formattingChangesByNodeType[node.type];
170+
const nonEmphasizedText = `${format}${node.firstChild.literal}${format}`;
165171
const f = getTextUntilEndOrLinebreak(node);
166172
const newText = value + nonEmphasizedText + f;
167173
const newLinks = linkify.find(newText);
@@ -175,7 +181,7 @@ export default class Markdown {
175181
// Remove `em` opening and closing nodes
176182
node.unlink();
177183
previousNode.insertAfter(event.node);
178-
shouldUnlinkEmphasisNode = true;
184+
shouldUnlinkFormattingNode = true;
179185
} else {
180186
logger.error(
181187
"Markdown links escaping found too many links for following text: ",
@@ -189,9 +195,9 @@ export default class Markdown {
189195
}
190196
}
191197
} else {
192-
if (shouldUnlinkEmphasisNode) {
198+
if (shouldUnlinkFormattingNode) {
193199
node.unlink();
194-
shouldUnlinkEmphasisNode = false;
200+
shouldUnlinkFormattingNode = false;
195201
}
196202
}
197203
}

test/Markdown-test.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ describe("Markdown parser test", () => {
4646
"https://riot.im/app/#/room/#_foonetic_xkcd:matrix.org",
4747
].join("\n");
4848

49-
it('tests that links are getting properly HTML formatted', () => {
49+
it('tests that links with markdown empasis in them are getting properly HTML formatted', () => {
5050
/* eslint-disable max-len */
5151
const expectedResult = [
5252
"<p>Test1:<br />#_foonetic_xkcd:matrix.org<br />http://google.com/_thing_<br />https://matrix.org/_matrix/client/foo/123_<br />#_foonetic_xkcd:matrix.org</p>",
@@ -125,7 +125,7 @@ describe("Markdown parser test", () => {
125125
expect(md.toHTML()).toEqual(expectedResult);
126126
});
127127

128-
it('expects that links in one line will be "escaped" properly', () => {
128+
it('expects that links with emphasis are "escaped" correctly', () => {
129129
/* eslint-disable max-len */
130130
const testString = [
131131
'http://domain.xyz/foo/bar-_stuff-like-this_-in-it.jpg' + " " + 'http://domain.xyz/foo/bar-_stuff-like-this_-in-it.jpg',
@@ -139,5 +139,29 @@ describe("Markdown parser test", () => {
139139
const md = new Markdown(testString);
140140
expect(md.toHTML()).toEqual(expectedResult);
141141
});
142+
143+
it('expects that the link part will not be accidentally added to <strong>', () => {
144+
/* eslint-disable max-len */
145+
const testString = `https://github.com/matrix-org/synapse/blob/develop/synapse/module_api/__init__.py`;
146+
const expectedResult = 'https://github.com/matrix-org/synapse/blob/develop/synapse/module_api/__init__.py';
147+
/* eslint-enable max-len */
148+
const md = new Markdown(testString);
149+
expect(md.toHTML()).toEqual(expectedResult);
150+
});
151+
152+
it('expects that the link part will not be accidentally added to <strong> for multiline links', () => {
153+
/* eslint-disable max-len */
154+
const testString = [
155+
'https://github.com/matrix-org/synapse/blob/develop/synapse/module_api/__init__.py' + " " + 'https://github.com/matrix-org/synapse/blob/develop/synapse/module_api/__init__.py',
156+
'https://github.com/matrix-org/synapse/blob/develop/synapse/module_api/__init__.py' + " " + 'https://github.com/matrix-org/synapse/blob/develop/synapse/module_api/__init__.py',
157+
].join('\n');
158+
const expectedResult = [
159+
'https://github.com/matrix-org/synapse/blob/develop/synapse/module_api/__init__.py' + " " + 'https://github.com/matrix-org/synapse/blob/develop/synapse/module_api/__init__.py',
160+
'https://github.com/matrix-org/synapse/blob/develop/synapse/module_api/__init__.py' + " " + 'https://github.com/matrix-org/synapse/blob/develop/synapse/module_api/__init__.py',
161+
].join('<br />');
162+
/* eslint-enable max-len */
163+
const md = new Markdown(testString);
164+
expect(md.toHTML()).toEqual(expectedResult);
165+
});
142166
});
143167
});

0 commit comments

Comments
 (0)