Skip to content

Commit 5100561

Browse files
committed
escape non-printable chars
1 parent 8b7a677 commit 5100561

1 file changed

Lines changed: 13 additions & 1 deletion

File tree

packages/server/client/printer.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,24 @@ function toUtf8(code: Buffer) {
4444
return iconv.decode(code, info).toString();
4545
}
4646

47+
function escapeText(s: string) {
48+
let res = '';
49+
for (const c of Array.from(s)) {
50+
res += /^[\p{L}\p{M}\p{N}\p{P}\p{S}\r\n\t ]$/u.test(c)
51+
? c
52+
: (c.length === 1 && c.codePointAt(0) <= 0x7F)
53+
? c.charCodeAt(0).toString(16).padStart(2, '0')
54+
: `U+${c.codePointAt(0).toString(16).toUpperCase()}`;
55+
}
56+
return res;
57+
}
58+
4759
export async function ConvertCodeToPDF(code: Buffer, lang, filename, team, location, codeColor = false) {
4860
compiler ||= await createTypstCompiler();
4961
const fakeFilename = randomstring(8); // cubercsl: do not trust filename from user
5062
const typst = generateTypst(team, location, fakeFilename, filename, lang, codeColor);
5163
compiler.addSource('/main.typst', typst);
52-
compiler.addSource(`/${fakeFilename}`, toUtf8(code));
64+
compiler.addSource(`/${fakeFilename}`, escapeText(toUtf8(code)));
5365
logger.info(`Convert ${filename} to PDF`);
5466
try {
5567
return await compiler.compile({

0 commit comments

Comments
 (0)