-
Notifications
You must be signed in to change notification settings - Fork 790
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve copy of console command examples #5397
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Noted: The current implementation is not restricted to console
code blocks at this time.
If we find it too general, we could restrict it to specific languages by following the warning guidelines in https://mkdocstrings.github.io/recipes/#prevent-selection-of-prompts-and-output-in-python-code-blocks.
Thanks! I'll play with this a bit. |
Huh this isn't working for me on Vivaldi, it's working for you? |
Oh, really? I've tested it on Firefox and Arc without any issues. |
Vivaldi is Chromium based fwiw. I can try Firefox too. |
Doesn't work on Firefox for me either, let me see if it's something in my setup. |
It's not my ad-blocker (uBlock Origin) |
Yes, I can reproduce the issue as well and have found a solution. Instead of using document.addEventListener("DOMContentLoaded", () => {
setCopyText();
}); we should use the following instead document$.subscribe(function() {
setCopyText();
}) Follow the |
2bb98cc
to
18d7222
Compare
@zanieb A friendly ping, in case you missed the fix. |
I was on vacation :) I'll take a look at it this week though! |
This still doesn't work for me in Vivaldi or Firefox on macOS :/ |
Weird, I just tested it in Vivaldi (6.8.3381.48), Firefox (128.0.3) and Arc (1.52.0) on macOS without any issue. Have you seen any js errors in the developer tools? |
Weird.. no errors in the developer tools console, just the "Enabled live reload" message. I can see |
(Sorry I don't have the extra time to dig into what's going on right now) |
No worries, we can revisit this later anytime. |
The following is a non-lazy patch that has been modified to allow work with instant mode, which you might also like to try: function getTextWithoutPromptAndOutput(targetSelector) {
const targetElement = document.querySelector(targetSelector);
// exclude "Generic Prompt" and "Generic Output" spans from copy
const excludedClasses = ["gp", "go"];
const clipboardText = [];
[...targetElement.childNodes].map((node) => {
// If the element does not contain the matching class,
// add to the clipboard text array
if (
!excludedClasses.some((className) => node?.classList?.contains(className))
) {
return clipboardText.push(node.textContent);
}
return null;
});
return clipboardText.join("").trim();
}
function patchCopyCodeButtons() {
// select all "copy" buttons whose target selector is a <code> element
[
...document.querySelectorAll(
`button.md-clipboard[data-clipboard-target$="code"]`,
),
].map((btn) =>
btn.setAttribute(
"data-clipboard-text",
getTextWithoutPromptAndOutput(btn.dataset.clipboardTarget),
),
);
}
document$.subscribe(function () {
patchCopyCodeButtons();
}); Although both lazy and non-lazy work for me. |
Hey so uhh have we been talking about different things the whole time? I'm pressing the "copy text" button (which includes the "$" in the clipboard). I just realized if I try to highlight and copy the text it works as described here ("$" is excluded) :) |
Hmmm, the results are not as expected.
|
Ah tragic. I don't understand why the JS isn't working for me... I'll see if someone else on our side can reproduce. |
Oh, and I think there's one more thing we could check first. Check the data attribute |
I don't see that attribute. I feel like I'm doing something wrong 😭 <button class="md-code__button" title="Copy to clipboard" data-clipboard-target="#__code_0 > code" data-md-type="copy"></button> If I add diff --git a/docs/js/extra.js b/docs/js/extra.js
index a8058688..8b08034c 100644
--- a/docs/js/extra.js
+++ b/docs/js/extra.js
@@ -25,6 +25,7 @@ function setCopyText() {
const elements = document.querySelectorAll(
'button.md-clipboard[data-clipboard-target$="code"]',
);
+ console.log(elements);
const observer = new IntersectionObserver((entries) => {
entries.forEach((entry) => {
// target in the viewport that have not been patched I see |
Doing something sloppy like this makes it work diff --git a/docs/js/extra.js b/docs/js/extra.js
index a8058688..dd49ab48 100644
--- a/docs/js/extra.js
+++ b/docs/js/extra.js
@@ -23,8 +23,9 @@ function setCopyText() {
const attr = "clipboardText";
// all "copy" buttons whose target selector is a <code> element
const elements = document.querySelectorAll(
- 'button.md-clipboard[data-clipboard-target$="code"]',
+ "#__code_0 > nav > button"
);
+ console.log(elements);
const observer = new IntersectionObserver((entries) => {
entries.forEach((entry) => {
// target in the viewport that have not been patched |
(Is it possible it's a difference from the insiders build?) |
Indeed, I can reproduce the issue on https://docs.astral.sh/uv/. There is also no button with the |
Victory! |
Thanks :) |
Thanks for your time and the review :) |
Is there a compelling argument for the more complicated lazy-version or should we do the second patch you suggested? |
The lazy version only patches elements within the viewport via The only drawback of the lazy version is browser compatibility. You can find more information on browser compatibility here: https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/IntersectionObserver#browser_compatibility Currently, |
Co-authored-by: eth3lbert <[email protected]>
@zanieb Could you also indent the suggestion before merging :) |
Thanks ! |
Summary
This PR improves the copy of the console command example by:
Most of the changes are inspired by opensafely/documentation#1461
Some other useful refs:
>>>
characters frompycon
fenced codeblocks squidfunk/mkdocs-material#3647Resolves #5355
Test Plan