mkdocs-material: Override clipboard text for code blocks

Context

No response

Description

Our documentation contains shell snippets including a command’s expected output, like:

image

One of our users complained about the clipboard button grabbing everything in the block, which is indeed rather annoying for something like shell-session. Even though the code highlighter correctly recognizes the prompt and the output, that doesn’t translate to the behaviour of the clipboard button.

Related links

I ended up here (https://github.com/facelessuser/pymdown-extensions/issues/1802) in my search, but that only really affects text selection. It’s still a nice UX improvement, though, and I’ll follow the advice given there as well.

Use Cases

I started out trying to influence clipboard.js’ behaviour by setting { .shell-session data-clipboard-target="foo !important" data-clipboard-text="go mod init ebpf-test && go mod tidy" } on the block (ending up on the div), but this only works when I remove the data-clipboard-target property from the button using my browser’s devtools.

Since it can’t easily be determined what the correct behaviour is in shell-session blocks containing multiple lines with commands, I figured it would be nice to be able to manually specify the snippet used by the clipboard button. XYProblem, but it looks like a relatively straightforward change from a distance.

It looks like setupClipboardJS() already takes data-clipboard-text into account, but data-clipboard-target seems to take precedence if both are specified on an element. Not sure why that’s the case.

Visuals

No response

Before submitting

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 1
  • Comments: 15 (10 by maintainers)

Most upvoted comments

There are currently no plans of removing it from our code base, but we’re still not advertising it. In the end, Markdown support is necessary for this, which is provided by Python Markdown and friends, so we have no control over this. Maybe, in the future, we find some time to create our own Markdown parser? Who knows? 😅

Looks like there’s been some progress in https://github.com/Python-Markdown/markdown/pull/1414, would that be sufficient to support this long-term?

I’m not sure what we can do to improve our integration – if Python Markdown supports it, we would immediately benefit from it, no change required. After all, it boils down to what Python Markdown does with the text you feed it. Material for MkDocs then takes this value from the HTML attribute, nothing more. What’s in that attribute is solely dictated by the parser.