diff --git a/server/utils/turndown/index.ts b/server/utils/turndown/index.ts index 8efc8d9de..4d3596368 100644 --- a/server/utils/turndown/index.ts +++ b/server/utils/turndown/index.ts @@ -35,4 +35,35 @@ const service = new TurndownService({ .use(breaks) .use(emptyLists); +const escapes: [RegExp, string][] = [ + [/\\/g, "\\\\"], + [/\*/g, "\\*"], + [/^-/g, "\\-"], + [/^\+ /g, "\\+ "], + [/^(=+)/g, "\\$1"], + [/^(#{1,6}) /g, "\\$1 "], + [/`/g, "\\`"], + [/^~~~/g, "\\~~~"], + [/\[/g, "\\["], + [/\]/g, "\\]"], + [/\(/g, "\\("], // OLN-91 + [/\)/g, "\\)"], // OLN-91 + [/^>/g, "\\>"], + [/_/g, "\\_"], + [/^(\d+)\. /g, "$1\\. "], +]; + +/** + * Overrides the Markdown escaping, as documented here: + * https://github.com/mixmark-io/turndown/blob/4499b5c313d30a3189a58fdd74fc4ed4b2428afd/README.md#overriding-turndownserviceprototypeescape + * + * @param text The string to escape + * @returns A string with Markdown syntax escaped + */ +service.escape = function (text) { + return escapes.reduce(function (accumulator, escape) { + return accumulator.replace(escape[0], escape[1]); + }, text); +}; + export default service;