From f9c2aa2195b99410cb88b69cf214fbb6a76e5653 Mon Sep 17 00:00:00 2001 From: ComputerGuy <63362464+Ocean-OS@users.noreply.github.com> Date: Sun, 27 Apr 2025 19:28:42 -0700 Subject: [PATCH 1/3] rewrite smart quotes logic --- packages/site-kit/src/lib/markdown/utils.ts | 40 +++++++++++++-------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/packages/site-kit/src/lib/markdown/utils.ts b/packages/site-kit/src/lib/markdown/utils.ts index 559d507456..3c9356fad5 100644 --- a/packages/site-kit/src/lib/markdown/utils.ts +++ b/packages/site-kit/src/lib/markdown/utils.ts @@ -50,23 +50,33 @@ export function smart_quotes( // wouldn't correctly handle `That '70s show` or `My country 'tis of thee` // but a) it's very unlikely they'll occur in our docs, and // b) they can be dealt with manually - return str.replace( - html ? /(.|^)('|")(.|$)/g : /(.|^)('|")(.|$)/g, - (m, before, quote, after) => { - const left = (first && before === '') || [' ', '\n', '('].includes(before); - let replacement = ''; - - if (html) { - const double = quote === '"'; - replacement = `&${left ? 'l' : 'r'}${double ? 'd' : 's'}quo;`; - } else { - const double = quote === '"'; - replacement = double ? (left ? '“' : '”') : left ? '‘' : '’'; + let open_quote = false; + let res = ''; + const len = str.length; + for (let index = 0; index < len; index++) { + let char = str.charAt(index); + if (html && char === '&') { + if (str.slice(index, index + 5) === ''') { + let left: boolean = first && !open_quote; + open_quote = left; + res += `&${left ? 'l' : 'r'}squo;`; + index += 4; + } else if (str.slice(index, index + 6) === '"') { + let left: boolean = first && !open_quote; + open_quote = left; + res += `&${left ? 'l' : 'r'}dquo`; + index += 5; } - - return (before ?? '') + replacement + (after ?? ''); + } else if (!html && (char === '"' || char === "'")) { + let left: boolean = first && !open_quote; + open_quote = left; + let double = char === '"'; + res += double ? (left ? '“' : '”') : left ? '‘' : '’'; + } else { + res += char; } - ); + } + return res; } const tokenizer: TokenizerObject = { From c62147a3d717e78c28c9e270d93a3c254a029a5c Mon Sep 17 00:00:00 2001 From: ComputerGuy <63362464+Ocean-OS@users.noreply.github.com> Date: Fri, 1 Aug 2025 02:05:34 -0700 Subject: [PATCH 2/3] fix --- packages/site-kit/src/lib/markdown/utils.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/site-kit/src/lib/markdown/utils.ts b/packages/site-kit/src/lib/markdown/utils.ts index 3c9356fad5..33632e3bc3 100644 --- a/packages/site-kit/src/lib/markdown/utils.ts +++ b/packages/site-kit/src/lib/markdown/utils.ts @@ -66,6 +66,8 @@ export function smart_quotes( open_quote = left; res += `&${left ? 'l' : 'r'}dquo`; index += 5; + } else { + res += '&'; } } else if (!html && (char === '"' || char === "'")) { let left: boolean = first && !open_quote; From c735af75bec91bf65a011a9c9e5b6cdf35b51844 Mon Sep 17 00:00:00 2001 From: ComputerGuy <63362464+Ocean-OS@users.noreply.github.com> Date: Fri, 1 Aug 2025 02:23:20 -0700 Subject: [PATCH 3/3] apply suggestion from review --- packages/site-kit/src/lib/markdown/utils.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/site-kit/src/lib/markdown/utils.ts b/packages/site-kit/src/lib/markdown/utils.ts index 33632e3bc3..41fc0dc42e 100644 --- a/packages/site-kit/src/lib/markdown/utils.ts +++ b/packages/site-kit/src/lib/markdown/utils.ts @@ -57,12 +57,12 @@ export function smart_quotes( let char = str.charAt(index); if (html && char === '&') { if (str.slice(index, index + 5) === ''') { - let left: boolean = first && !open_quote; + let left: boolean = (first && !open_quote) || (index > 1 && str.charAt(index - 1) === '='); open_quote = left; res += `&${left ? 'l' : 'r'}squo;`; index += 4; } else if (str.slice(index, index + 6) === '"') { - let left: boolean = first && !open_quote; + let left: boolean = (first && !open_quote) || (index > 1 && str.charAt(index - 1) === '='); open_quote = left; res += `&${left ? 'l' : 'r'}dquo`; index += 5; @@ -70,7 +70,7 @@ export function smart_quotes( res += '&'; } } else if (!html && (char === '"' || char === "'")) { - let left: boolean = first && !open_quote; + let left: boolean = (first && !open_quote) || (index > 1 && str.charAt(index - 1) === '='); open_quote = left; let double = char === '"'; res += double ? (left ? '“' : '”') : left ? '‘' : '’';