Skip to content

Commit 655887c

Browse files
committed
be more strict and use system font for better kbd shortcut rendering on macos (#5879)
1 parent cb60ecb commit 655887c

File tree

2 files changed

+34
-9
lines changed

2 files changed

+34
-9
lines changed
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)