Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
# Source HTML partials we intentionally track (not build output).
!qwt-reveal-toggle.html
!theme-picker.html
!tabset-code-in-tabs.html
!anchor-landing.html
!watch-anchors.html
!table-roles.html
Expand Down
2 changes: 2 additions & 0 deletions _quarto-website.yml
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ format:
# Replaces Quarto's two-way toggle with a Light / Off-white / Parchment / Dark dropdown.
include-in-header: theme-picker.html
include-after-body:
- tabset-code-in-tabs.html
- anchor-landing.html
- watch-anchors.html
- table-roles.html
Expand Down Expand Up @@ -235,6 +236,7 @@ format:
search: false # decks duplicate the HTML pages, so the search indexes only the HTML format
theme: [simple, styles-reveal.scss]
include-in-header: qwt-reveal-toggle.html
include-after-body: tabset-code-in-tabs.html
slide-number: true
chalkboard: false
preview-links: auto
Expand Down
45 changes: 45 additions & 0 deletions tabset-code-in-tabs.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<script>
// Put each folded code cell inside the tab it belongs to.
// Quarto hoists the `details.code-fold` of a cross-referenced figure out of the figure,
// so an R and a Python tab end up with their folds stacked above the whole figure.
// For every language tabset, take the run of folds immediately before it (or before the figure
// that holds it), match each fold to a tab by language, and move it to the top of that tab.
// The selectors cover both the website tabsets (Bootstrap) and the slide tabsets (tabby).
document.querySelectorAll("div.panel-tabset").forEach(function (tabset) {
var anchor = tabset.closest("div.quarto-float") || tabset;
var panes = Array.prototype.slice.call(tabset.querySelectorAll(":scope > .tab-content > div"));
var tabs = Array.prototype.slice.call(tabset.querySelectorAll(":scope > ul > li > a"));
var folds = [];
var node = anchor.previousElementSibling;
while (node && node.matches("details.code-fold")) {
folds.unshift(node);
node = node.previousElementSibling;
}
if (folds.length === 0 || folds.length !== panes.length || tabs.length !== panes.length) {
return;
}
var language = function (fold) {
var code = fold.querySelector("pre.sourceCode");
var found = code && Array.prototype.find.call(code.classList, function (name) {
return name !== "sourceCode" && name !== "downlit" && name !== "code-with-copy";
});
return found ? found.toLowerCase() : "";
};
var used = {};
var unused = function () {
return tabs.findIndex(function (tab, j) {
return !used[j];
});
};
folds.forEach(function (fold) {
var index = tabs.findIndex(function (tab, j) {
return !used[j] && tab.textContent.trim().toLowerCase() === language(fold);
});
if (index < 0) {
index = unused();
}
used[index] = true;
panes[index].insertBefore(fold, panes[index].firstChild);
});
});
</script>
Loading