Skip to content

Commit ad6dabc

Browse files
authored
Merge pull request #13041 from quarto-dev/bugfix/5879
Improve font rendering of `kbd` shortcode on macOS
2 parents cb60ecb + 0280891 commit ad6dabc

File tree

3 files changed

+35
-9
lines changed

3 files changed

+35
-9
lines changed

news/changelog-1.8.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ All changes included in 1.8:
1818
- ([#678](https://github.com/quarto-dev/quarto-cli/issues/678)): a11y - Provide appropriate `aria-label` to search button.
1919
- ([#726](https://github.com/quarto-dev/quarto-cli/issues/726)): a11y - Provide `.screen-reader-only` callout type when callout text doesn't naturally include the type.
2020
- ([#5538](https://github.com/quarto-dev/quarto-cli/issues/5538)): Fix code-copy button style so that scrolling behaves properly.
21+
- ([#5879](https://github.com/quarto-dev/quarto-cli/issues/5879)): Improve font rendering of `kbd` shortcode on macOS. `kbd` will now also be stricter in converting keyboard shortcuts to macOS icons.
2122
- ([#10983](https://github.com/quarto-dev/quarto-cli/issues/10983)): Fix spacing inconsistency between paras and first section headings.
2223
- ([#12259](https://github.com/quarto-dev/quarto-cli/issues/12259)): Fix conflict between `html-math-method: katex` and crossref popups (author: @benkeks).
2324
- ([#12341](https://github.com/quarto-dev/quarto-cli/issues/12341)): Enable light and dark logos for html formats (sidebar, navbar, dashboard).
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1-
kbd.kbd {
1+
kbd.kbd.mac {
2+
/* When rendering keyboard icons on macOS, we revert the font family to the
3+
system font to ensure the icons render with the same X-height as upper case letters. */
4+
font-family: inherit;
25
}

src/resources/extensions/quarto/kbd/resources/kbd.js

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,43 @@
1717
}
1818
const os = guessOS();
1919

20+
const macosModifiers = {
21+
"command": "⌘",
22+
"cmd": "⌘",
23+
"shift": "⇧",
24+
"ctrl": "⌃",
25+
"control": "⌃",
26+
"option": "⌥",
27+
};
28+
const keyRegex = /^[a-z0-9_]+$/;
29+
// use a non-capturing group to avoid capturing the key names
30+
31+
const macosModifiersRegex = new RegExp(
32+
// use a non-capturing group to avoid capturing the key names
33+
`^(?:${Object.keys(macosModifiers).join("|")})-`,
34+
"gi"
35+
);
36+
const shortcutParses = (text) => {
37+
text = text.toLocaleLowerCase();
38+
while (text.match(macosModifiersRegex)) {
39+
text = text.replace(macosModifiersRegex, "");
40+
}
41+
return text.match(keyRegex) !== null;
42+
}
43+
2044
// deno-lint-ignore no-window-prefix
2145
window.addEventListener("DOMContentLoaded", (_) => {
2246
for (const el of Array.from(document.querySelectorAll("kbd"))) {
2347
el.classList.add("kbd");
2448
if (el.dataset[os.name] !== undefined) {
2549
el.innerText = el.dataset[os.name];
2650
}
27-
if (os.name === "mac") {
28-
el.innerText = el.innerText
29-
.replaceAll(/command-?/gi, "⌘")
30-
.replaceAll(/cmd-?/gi, "⌘")
31-
.replaceAll(/shift-?/gi, "⇧")
32-
.replaceAll(/ctrl-?/gi, "⌃")
33-
.replaceAll(/control-?/gi, "⌃")
34-
.replaceAll(/option-?/gi, "⌥");
51+
if (os.name === "mac" && shortcutParses(el.innerText)) {
52+
el.classList.add("mac");
53+
for (const [key, value] of Object.entries(macosModifiers)) {
54+
el.innerText = el.innerText.replaceAll(new RegExp(`${key}-`, "gi"), value);
55+
}
56+
el.innerText = el.innerText.toLocaleUpperCase();
3557
}
3658
}
3759
});

0 commit comments

Comments
 (0)