Skip to content

Commit a9da0b6

Browse files
committed
Added support for specifying copied text for code blocks
1 parent 48ff39a commit a9da0b6

File tree

5 files changed

+41
-7
lines changed

5 files changed

+41
-7
lines changed

docs/reference/code-blocks.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,37 @@ theme:
8181
```
8282
````
8383

84+
??? tip "Overriding the clipboard text"
85+
86+
If you want to define a slightly different text to be copied to the
87+
clipboard, you can use the `data-copy` attribute on the code block. Note
88+
that this attribute does not support multiple lines, which is not a
89+
limitation of Material for MkDocs, but of the Markdown parser. Example:
90+
91+
```` markdown title="Code block"
92+
``` { .sh data-copy="curl https://www.example.com" }
93+
$ curl https://www.example.com
94+
# <!doctype html>
95+
# <html>
96+
# ...
97+
```
98+
````
99+
100+
<div class="result" markdown>
101+
102+
``` { .sh data-copy="curl https://www.example.com" }
103+
$ curl https://www.example.com
104+
# <!doctype html>
105+
# <html>
106+
# ...
107+
```
108+
109+
</div>
110+
111+
We recommend to use this very sparingly, because sometimes it can be
112+
confusing to copy something different to the clipboard than what is
113+
actually displayed.
114+
84115
### Code selection button
85116

86117
<!-- md:sponsors -->

material/templates/assets/javascripts/bundle.2a9e8380.min.js renamed to material/templates/assets/javascripts/bundle.94c44541.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

material/templates/assets/javascripts/bundle.2a9e8380.min.js.map renamed to material/templates/assets/javascripts/bundle.94c44541.min.js.map

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

material/templates/base.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@
249249
</script>
250250
{% endblock %}
251251
{% block scripts %}
252-
<script src="{{ 'assets/javascripts/bundle.2a9e8380.min.js' | url }}"></script>
252+
<script src="{{ 'assets/javascripts/bundle.94c44541.min.js' | url }}"></script>
253253
{% for script in config.extra_javascript %}
254254
{{ script | script_tag }}
255255
{% endfor %}

src/templates/assets/javascripts/integrations/clipboard/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,10 @@ interface SetupOptions {
5555
*/
5656
function extract(el: HTMLElement): string {
5757
el.setAttribute("data-md-copying", "")
58-
const text = el.getAttribute("data-clipboard-text") ?? el.innerText
58+
const copy = el.closest("[data-copy]")
59+
const text = copy
60+
? copy.getAttribute("data-copy")!
61+
: el.innerText
5962
el.removeAttribute("data-md-copying")
6063
return text
6164
}

0 commit comments

Comments
 (0)