Skip to content

Commit 58c602d

Browse files
authored
Merge pull request #111 from Gerrit0/master
fix: No codeblock escapes
2 parents f70cdd1 + 5db2a93 commit 58c602d

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/modules/twoslash.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ import { sendWithMessageOwnership } from '../util/send';
66

77
const CODEBLOCK = '```';
88

9+
// Custom escape function instead of using discord.js Util.escapeCodeBlock because this
10+
// produces better results with template literal types. Discord's markdown handling is pretty
11+
// bad. It doesn't properly handle escaping back ticks, so we instead insert zero width spaces
12+
// so that users cannot escape our code block.
13+
function escapeCode(code: string) {
14+
return code.replace(/```/g, '`\u200B`\u200B`');
15+
}
16+
917
export class TwoslashModule extends Module {
1018
@command({
1119
single: true,
@@ -47,7 +55,7 @@ export class TwoslashModule extends Module {
4755

4856
await sendWithMessageOwnership(
4957
msg,
50-
`${CODEBLOCK}typescript\n${value.text}${CODEBLOCK}`,
58+
`${CODEBLOCK}typescript\n${escapeCode(value.text)}${CODEBLOCK}`,
5159
);
5260
}
5361

@@ -138,7 +146,7 @@ export class TwoslashModule extends Module {
138146
const output = resultLines.join('\n');
139147
return sendWithMessageOwnership(
140148
msg,
141-
`${CODEBLOCK}ts\n${output}${CODEBLOCK}\n`,
149+
`${CODEBLOCK}ts\n${escapeCode(output)}${CODEBLOCK}\n`,
142150
);
143151
}
144152
}

0 commit comments

Comments
 (0)