Skip to content

Commit f9c2aa2

Browse files
committed
rewrite smart quotes logic
1 parent 923674d commit f9c2aa2

File tree

1 file changed

+25
-15
lines changed
  • packages/site-kit/src/lib/markdown

1 file changed

+25
-15
lines changed

packages/site-kit/src/lib/markdown/utils.ts

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -50,23 +50,33 @@ export function smart_quotes(
5050
// wouldn't correctly handle `That '70s show` or `My country 'tis of thee`
5151
// but a) it's very unlikely they'll occur in our docs, and
5252
// b) they can be dealt with manually
53-
return str.replace(
54-
html ? /(.|^)('|")(.|$)/g : /(.|^)('|")(.|$)/g,
55-
(m, before, quote, after) => {
56-
const left = (first && before === '') || [' ', '\n', '('].includes(before);
57-
let replacement = '';
58-
59-
if (html) {
60-
const double = quote === '"';
61-
replacement = `&${left ? 'l' : 'r'}${double ? 'd' : 's'}quo;`;
62-
} else {
63-
const double = quote === '"';
64-
replacement = double ? (left ? '“' : '”') : left ? '‘' : '’';
53+
let open_quote = false;
54+
let res = '';
55+
const len = str.length;
56+
for (let index = 0; index < len; index++) {
57+
let char = str.charAt(index);
58+
if (html && char === '&') {
59+
if (str.slice(index, index + 5) === '&#39;') {
60+
let left: boolean = first && !open_quote;
61+
open_quote = left;
62+
res += `&${left ? 'l' : 'r'}squo;`;
63+
index += 4;
64+
} else if (str.slice(index, index + 6) === '&quot;') {
65+
let left: boolean = first && !open_quote;
66+
open_quote = left;
67+
res += `&${left ? 'l' : 'r'}dquo`;
68+
index += 5;
6569
}
66-
67-
return (before ?? '') + replacement + (after ?? '');
70+
} else if (!html && (char === '"' || char === "'")) {
71+
let left: boolean = first && !open_quote;
72+
open_quote = left;
73+
let double = char === '"';
74+
res += double ? (left ? '“' : '”') : left ? '‘' : '’';
75+
} else {
76+
res += char;
6877
}
69-
);
78+
}
79+
return res;
7080
}
7181

7282
const tokenizer: TokenizerObject = {

0 commit comments

Comments
 (0)