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

Commit 18c03da

Browse files
authored
Fix links being mangled by markdown processing (#9570)
1 parent 212233c commit 18c03da

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/Markdown.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,26 @@ const formattingChangesByNodeType = {
9999
'strong': '__',
100100
};
101101

102+
/**
103+
* Returns the literal of a node an all child nodes.
104+
*/
105+
const innerNodeLiteral = (node: commonmark.Node): string => {
106+
let literal = "";
107+
108+
const walker = node.walker();
109+
let step: commonmark.NodeWalkingStep;
110+
111+
while (step = walker.next()) {
112+
const currentNode = step.node;
113+
const currentNodeLiteral = currentNode.literal;
114+
if (step.entering && currentNode.type === "text" && currentNodeLiteral) {
115+
literal += currentNodeLiteral;
116+
}
117+
}
118+
119+
return literal;
120+
};
121+
102122
/**
103123
* Class that wraps commonmark, adding the ability to see whether
104124
* a given message actually uses any markdown syntax or whether
@@ -185,7 +205,7 @@ export default class Markdown {
185205
* but this solution seems to work well and is hopefully slightly easier to understand too
186206
*/
187207
const format = formattingChangesByNodeType[node.type];
188-
const nonEmphasizedText = `${format}${node.firstChild.literal}${format}`;
208+
const nonEmphasizedText = `${format}${innerNodeLiteral(node)}${format}`;
189209
const f = getTextUntilEndOrLinebreak(node);
190210
const newText = value + nonEmphasizedText + f;
191211
const newLinks = linkify.find(newText);

test/Markdown-test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,22 @@ describe("Markdown parser test", () => {
124124
const testString = [
125125
'http://domain.xyz/foo/bar-_stuff-like-this_-in-it.jpg' + " " + 'http://domain.xyz/foo/bar-_stuff-like-this_-in-it.jpg',
126126
'http://domain.xyz/foo/bar-_stuff-like-this_-in-it.jpg' + " " + 'http://domain.xyz/foo/bar-_stuff-like-this_-in-it.jpg',
127+
"https://example.com/_test_test2_-test3",
128+
"https://example.com/_test_test2_test3_",
129+
"https://example.com/_test__test2_test3_",
130+
"https://example.com/_test__test2__test3_",
131+
"https://example.com/_test__test2_test3__",
132+
"https://example.com/_test__test2",
127133
].join('\n');
128134
const expectedResult = [
129135
"http://domain.xyz/foo/bar-_stuff-like-this_-in-it.jpg http://domain.xyz/foo/bar-_stuff-like-this_-in-it.jpg",
130136
"http://domain.xyz/foo/bar-_stuff-like-this_-in-it.jpg http://domain.xyz/foo/bar-_stuff-like-this_-in-it.jpg",
137+
"https://example.com/_test_test2_-test3",
138+
"https://example.com/_test_test2_test3_",
139+
"https://example.com/_test__test2_test3_",
140+
"https://example.com/_test__test2__test3_",
141+
"https://example.com/_test__test2_test3__",
142+
"https://example.com/_test__test2",
131143
].join('<br />');
132144
/* eslint-enable max-len */
133145
const md = new Markdown(testString);

0 commit comments

Comments
 (0)