Skip to content

Commit b5eb6e9

Browse files
committed
smart quotes everywhere
1 parent b395980 commit b5eb6e9

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ async function parse({
269269
return this.parser!.parseInline(token.tokens);
270270
}
271271

272-
return smart_quotes(token.text);
272+
return smart_quotes(token.text, true);
273273
},
274274
heading({ tokens, depth, raw }) {
275275
const text = this.parser!.parseInline(tokens);

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

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,18 +77,28 @@ export const normalizeSlugify = (str: string) => {
7777
return slugify(removeHTMLEntities(removeMarkdown(str))).replace(/(<([^>]+)>)/gi, '');
7878
};
7979

80-
export function smart_quotes(str: string) {
80+
export function smart_quotes(str: string, html: boolean = false) {
8181
// replace dumb quotes with smart quotes. This isn't a perfect algorithm — it
8282
// wouldn't correctly handle `That '70s show` or `My country 'tis of thee`
8383
// but a) it's very unlikely they'll occur in our docs, and
8484
// b) they can be dealt with manually
85-
return str.replace(/(.|^)(&#39;|&quot;)(.|$)/g, (m, before, quote, after) => {
86-
const left = !before.trim();
87-
const double = quote === '&quot;';
88-
const entity = `&${left ? 'l' : 'r'}${double ? 'd' : 's'}quo;`;
89-
90-
return (before ?? '') + entity + (after ?? '');
91-
});
85+
return str.replace(
86+
html ? /(.|^)(&#39;|&quot;)(.|$)/g : /(.|^)('|")(.|$)/g,
87+
(m, before, quote, after) => {
88+
const left = !before.trim();
89+
let replacement = '';
90+
91+
if (html) {
92+
const double = quote === '&quot;';
93+
replacement = `&${left ? 'l' : 'r'}${double ? 'd' : 's'}quo;`;
94+
} else {
95+
const double = quote === '"';
96+
replacement = double ? (left ? '“' : '”') : left ? '‘' : '’';
97+
}
98+
99+
return (before ?? '') + replacement + (after ?? '');
100+
}
101+
);
92102
}
93103

94104
const tokenizer: TokenizerObject = {

0 commit comments

Comments
 (0)