Skip to content
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

Merged
merged 7 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions docs/js/extra.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
function cleanupClipboardText(targetSelector) {
const targetElement = document.querySelector(targetSelector);

// exclude "Generic Prompt" and "Generic Output" spans from copy
const excludedClasses = ["gp", "go"];

const clipboardText = Array.from(targetElement.childNodes)
.filter(
(node) =>
!excludedClasses.some((className) =>
node?.classList?.contains(className),
),
)
.map((node) => node.textContent)
.filter((s) => s != "");
return clipboardText.join("").trim();
}

// Sets copy text to attributes lazily using an Intersection Observer.
function setCopyText() {
// The `data-clipboard-text` attribute allows for customized content in the copy
// See: https://www.npmjs.com/package/clipboard#copy-text-from-attribute
const attr = "clipboardText";
// all "copy" buttons whose target selector is a <code> element
const elements = document.querySelectorAll(
'button.md-clipboard[data-clipboard-target$="code"], button.md-code__button[data-clipboard-target$="code"]',
zanieb marked this conversation as resolved.
Show resolved Hide resolved
);
const observer = new IntersectionObserver((entries) => {
entries.forEach((entry) => {
// target in the viewport that have not been patched
if (
entry.intersectionRatio > 0 &&
entry.target.dataset[attr] === undefined
) {
entry.target.dataset[attr] = cleanupClipboardText(
entry.target.dataset.clipboardTarget,
);
}
});
});

elements.forEach((elt) => {
observer.observe(elt);
});
}

// Using the document$ observable is particularly important if you are using instant loading since
// it will not result in a page refresh in the browser
// See `How to integrate with third-party JavaScript libraries` guideline:
// https://squidfunk.github.io/mkdocs-material/customization/?h=javascript#additional-javascript
document$.subscribe(function () {
setCopyText();
});
4 changes: 4 additions & 0 deletions docs/stylesheets/extra.css
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,7 @@ This is a consequence of the reduced nav spacing below. */
.md-nav--secondary .md-nav__link {
margin-top: 0.5em;
}
/* See: https://mkdocstrings.github.io/recipes/#prevent-selection-of-prompts-and-output-in-python-code-blocks */
.highlight .gp, .highlight .go { /* Generic.Prompt, Generic.Output */
user-select: none;
}
2 changes: 2 additions & 0 deletions mkdocs.template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ plugins:
- typeset
extra_css:
- stylesheets/extra.css
extra_javascript:
- js/extra.js
extra:
analytics:
provider: fathom
Expand Down
Loading