Skip to content

Commit d36fe54

Browse files
cscheidclaude
andauthored
Add mode=plain to kbd shortcode (#13489) (#14079)
* Add `mode=plain` to `kbd` shortcode (#13489) Allow rendering keyboard shortcuts as literal text without OS-specific symbol translation, for teaching/slideshow contexts where shortcuts for multiple OSes need to be shown simultaneously. Co-Authored-By: Claude Opus 4.6 <[email protected]> * Fix kbd shortcode handling empty mode kwarg and test format Quarto's shortcode system populates kwargs with empty values for unspecified keys, so check for empty string before validating mode. Also fix test to use correct ensureFileRegexMatches format (single array of match patterns). Co-Authored-By: Claude Opus 4.6 <[email protected]> --------- Co-authored-by: Claude Opus 4.6 <[email protected]>
1 parent fd2eafc commit d36fe54

4 files changed

Lines changed: 48 additions & 1 deletion

File tree

news/changelog-1.9.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ All changes included in 1.9:
33
## Shortcodes
44

55
- ([#13342](https://github.com/quarto-dev/quarto-cli/issues/13342)): Ensure that the `contents` shortcode works inside metadata.
6+
- ([#13489](https://github.com/quarto-dev/quarto-cli/issues/13489)): Add `mode=plain` option to the `kbd` shortcode to render keyboard shortcuts exactly as written, without OS-specific symbol translation.
67
- ([#14061](https://github.com/quarto-dev/quarto-cli/issues/14061)): Fix `meta` shortcode not preserving line breaks in values. The shortcode now respects its usage context (block, inline, or text) and preserves paragraph breaks in block and code block contexts.
78

89
## Regression fixes

src/resources/extensions/quarto/kbd/kbd.lua

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,40 @@ return {
44
local function get_osname(v)
55
if v == "win" then return "windows" end
66
if v == "mac" then return "mac" end
7-
if v == "linux" then return "linux" end
7+
if v == "linux" then return "linux" end
88
end
9+
10+
-- Extract and validate mode kwarg
11+
local mode = kwargs["mode"]
12+
if mode ~= nil then
13+
mode = pandoc.utils.stringify(mode)
14+
kwargs["mode"] = nil
15+
if mode == "" then
16+
mode = nil
17+
elseif mode ~= "plain" then
18+
return quarto.shortcode.error_output("kbd", "unknown mode: " .. mode .. ", supported modes are: plain", "inline")
19+
else
20+
-- plain mode requires a positional argument
21+
if #args == 0 then
22+
return quarto.shortcode.error_output("kbd", "plain mode requires a positional argument", "inline")
23+
end
24+
-- plain mode doesn't accept OS kwargs
25+
for k, _ in pairs(kwargs) do
26+
return quarto.shortcode.error_output("kbd", "plain mode does not accept OS-specific arguments", "inline")
27+
end
28+
end
29+
end
30+
931
if quarto.doc.is_format("html:js") then
1032
quarto.doc.add_html_dependency({
1133
name = 'kbd',
1234
scripts = { 'resources/kbd.js' },
1335
stylesheets = { 'resources/kbd.css' }
1436
})
37+
if mode == "plain" then
38+
local text = pandoc.utils.stringify(args[1])
39+
return pandoc.RawInline('html', '<kbd data-mode="plain" class="kbd">' .. text .. '</kbd>')
40+
end
1541
local kwargs_strs = {}
1642
local title_strs = {}
1743
for k, v in pairs(kwargs) do
@@ -73,6 +99,9 @@ return {
7399
-- {{< kbd Shift-Ctrl-P >}}
74100
-- {{< kbd Shift-Ctrl-P mac=Shift-Command-P >}}
75101
-- {{< kbd mac=Shift-Command-P win=Shift-Control-S linux=Shift-Ctrl-S >}}
102+
if mode == "plain" then
103+
return pandoc.Code(pandoc.utils.stringify(args[1]))
104+
end
76105
local result = {};
77106
local n_kwargs = 0
78107
for k, v in pairs(kwargs) do

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@
4545
window.addEventListener("DOMContentLoaded", (_) => {
4646
for (const el of Array.from(document.querySelectorAll("kbd"))) {
4747
el.classList.add("kbd");
48+
if (el.dataset.mode === "plain") {
49+
continue;
50+
}
4851
if (el.dataset[os.name] !== undefined) {
4952
el.innerText = el.dataset[os.name];
5053
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
title: kbd plain mode test
3+
format: html
4+
_quarto:
5+
tests:
6+
html:
7+
ensureFileRegexMatches:
8+
- ['data-mode="plain"', 'Shift-Ctrl-K', 'Command-Shift-K']
9+
- []
10+
---
11+
12+
{{< kbd Shift-Ctrl-K mode=plain >}}
13+
14+
{{< kbd Command-Shift-K mode=plain >}}

0 commit comments

Comments
 (0)