Skip to content

fix: rewrite smart quotes logic #1325

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 25 additions & 15 deletions packages/site-kit/src/lib/markdown/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) === '&#39;') {
let left: boolean = first && !open_quote;
open_quote = left;
res += `&${left ? 'l' : 'r'}squo;`;
index += 4;
} else if (str.slice(index, index + 6) === '&quot;') {
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 = {
Expand Down