From 49bf934da336aae5bcd950d432fc0ad007b6a94f Mon Sep 17 00:00:00 2001 From: Shahed Nasser Date: Wed, 12 Feb 2025 19:08:37 +0200 Subject: [PATCH] docs-util: fix inline code escape in generated references (#11428) --- www/utils/packages/utils/src/str-utils.ts | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/www/utils/packages/utils/src/str-utils.ts b/www/utils/packages/utils/src/str-utils.ts index 4a79b2ec2a..0e0a39616b 100644 --- a/www/utils/packages/utils/src/str-utils.ts +++ b/www/utils/packages/utils/src/str-utils.ts @@ -7,8 +7,26 @@ export function getHTMLChar(str: string) { } export function escapeChars(str: string, escapeBackticks = true) { - const result = getHTMLChar(str).replace(/_/g, "\\_").replace(/\|/g, "\\|") - return escapeBackticks ? result.replace(/`/g, "\\`") : result + const codeRegex = /(`[^`]*`)/g + let result = "" + let lastIndex = 0 + + str.replace(codeRegex, (match, p1, offset) => { + result += getHTMLChar(str.slice(lastIndex, offset)) + .replace(/\|/g, "\\|") + .replace(/`/g, escapeBackticks ? "\\`" : "`") + .replace(/_/g, "\\_") + result += p1 // keep the inline code block as is + lastIndex = offset + match.length + return match + }) + + result += getHTMLChar(str.slice(lastIndex)) + .replace(/\|/g, "\\|") + .replace(/`/g, escapeBackticks ? "\\`" : "`") + .replace(/_/g, "\\_") + + return result } export function stripLineBreaks(str: string) {