diff --git a/src/index.ts b/src/index.ts index 6e5c166..bdfb353 100644 --- a/src/index.ts +++ b/src/index.ts @@ -24,7 +24,29 @@ export async function markdownToBlocks( body: string, options: ParsingOptions = {} ): Promise { - const tokens = marked.lexer(body); + // Slack only wants &, <, and > escaped + // https://api.slack.com/reference/surfaces/formatting#escaping + const replacements: Record = { + '&': '&', + '<': '<', + '>': '>', + }; + + const lexer = new marked.Lexer(); + lexer.options.tokenizer = new marked.Tokenizer(); + lexer.options.tokenizer.inlineText = src => { + const text = src.replace(/[&<>]/g, char => { + return replacements[char]; + }); + + return { + type: 'text', + raw: src, + text: text, + }; + }; + + const tokens = lexer.lex(body); return parseBlocks(tokens, options); } diff --git a/test/integration.spec.ts b/test/integration.spec.ts index b9e36b4..83cd703 100644 --- a/test/integration.spec.ts +++ b/test/integration.spec.ts @@ -126,4 +126,10 @@ if (a === 'hi') { expect(actual).toStrictEqual(expected); }); }); + + it('should correctly escape text', async () => { + const actual = await markdownToBlocks('<>&\'""\'&><'); + const expected = [slack.section('<>&\'""\'&><')]; + expect(actual).toStrictEqual(expected); + }); });