Skip to content

Commit 210691f

Browse files
authored
Respect code-copy for the citation BibTeX copy button (#15209)
With `code-copy: false`, the BibTeX block in the citation appendix still got a copy button, but clicking it did nothing: clipboard.js and its setup script are not emitted when code copy is off. #14387 moved the button inside the block, which made the dead button easier to notice. The appendix now adds the scaffold, the code-with-copy class and the button only when code copy is enabled, using the same resolution as the other code blocks. That resolution moves to formatHasCodeCopy() in format-html-shared.ts, next to htmlFormatFeatureDefaults(), so the minimal default stays in one place. The appendix cannot import format-html.ts without an import cycle, since that module imports the bootstrap postprocessor, which calls the appendix. - Gate the BibTeX copy button and its scaffold on code-copy - Resolve code-copy in one shared helper, not inline in format-html.ts - Stop createCodeBlock from always adding code-with-copy Fixes #15207
1 parent 5b7009c commit 210691f

5 files changed

Lines changed: 65 additions & 33 deletions

File tree

‎news/changelog-1.11.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ All changes included in 1.11:
8181
- ([#15125](https://github.com/quarto-dev/quarto-cli/pull/15125)): Fix Google Scholar metadata failing the render with a CSL JSON (`.json`) bibliography.
8282
- ([#15128](https://github.com/quarto-dev/quarto-cli/pull/15128)): Fix Google Scholar metadata missing references with a YAML bibliography not wrapped in `---` delimiters.
8383
- ([#15131](https://github.com/quarto-dev/quarto-cli/issues/15131)): Fix the light/dark toggle appearing, with no dark styles to switch to, when the `dark` brand file holds only comments and is ignored.
84+
- ([#15207](https://github.com/quarto-dev/quarto-cli/issues/15207)): Fix the BibTeX citation in the HTML appendix showing a copy button that does nothing when `code-copy: false` is set.
8485

8586
### `pdf`
8687

‎src/format/html/format-html-appendix.ts‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
import {
2727
createCodeBlock,
2828
createCodeCopyButton,
29+
formatHasCodeCopy,
2930
hasMarginCites,
3031
hasMarginRefs,
3132
insertFootnotesTitle,
@@ -306,13 +307,18 @@ export async function processDocumentAppendix(
306307
const bibTexDiv = createCodeBlock(doc, cite.bibtex, "bibtex");
307308
bibTexDiv.classList.add(kQuartoCiteBibtexClass);
308309

309-
const outerScaffold = doc.createElement("div");
310-
outerScaffold.classList.add("code-copy-outer-scaffold");
311-
outerScaffold.appendChild(bibTexDiv);
312-
contentsDiv.appendChild(outerScaffold);
310+
if (formatHasCodeCopy(format)) {
311+
bibTexDiv.classList.add("code-with-copy");
312+
const outerScaffold = doc.createElement("div");
313+
outerScaffold.classList.add("code-copy-outer-scaffold");
314+
outerScaffold.appendChild(bibTexDiv);
315+
contentsDiv.appendChild(outerScaffold);
313316

314-
const copyButton = createCodeCopyButton(doc, format);
315-
outerScaffold.appendChild(copyButton);
317+
const copyButton = createCodeCopyButton(doc, format);
318+
outerScaffold.appendChild(copyButton);
319+
} else {
320+
contentsDiv.appendChild(bibTexDiv);
321+
}
316322
}
317323

318324
if (cite?.html) {

‎src/format/html/format-html-shared.ts‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ import {
2828
import { formatResourcePath } from "../../core/resources.ts";
2929
import { Document, Element } from "../../core/deno-dom.ts";
3030
import { normalizePath } from "../../core/path.ts";
31+
import { formatHasBootstrap } from "./format-html-info.ts";
32+
import { HtmlFormatFeatureDefaults } from "./format-html-types.ts";
3133

3234
// features that are enabled by default for 'html'. setting
3335
// all of these to false will yield the minimal html output
@@ -73,6 +75,33 @@ export const kCitation = "citation";
7375
export const kDocumentCss = "document-css";
7476
export const kBootstrapDependencyName = "bootstrap";
7577

78+
export function htmlFormatFeatureDefaults(
79+
format: Format,
80+
): HtmlFormatFeatureDefaults {
81+
const bootstrap = formatHasBootstrap(format);
82+
const minimal = format.metadata[kMinimal] === true;
83+
return {
84+
tabby: !minimal && !bootstrap,
85+
copyCode: !minimal,
86+
anchors: !minimal,
87+
hoverCitations: !minimal,
88+
hoverFootnotes: !minimal,
89+
figResponsive: !minimal,
90+
codeAnnotations: !minimal,
91+
hoverXrefs: !minimal,
92+
};
93+
}
94+
95+
// whether code copy buttons (and clipboard.js) are enabled for this format
96+
export function formatHasCodeCopy(
97+
format: Format,
98+
enabledByDefault = htmlFormatFeatureDefaults(format).copyCode,
99+
): boolean {
100+
return enabledByDefault
101+
? format.metadata[kCodeCopy] !== false
102+
: !!format.metadata[kCodeCopy];
103+
}
104+
76105
export const clipboardDependency = () => {
77106
const dependency: FormatDependency = { name: "clipboard" };
78107
dependency.scripts = [];
@@ -492,7 +521,6 @@ export function createCodeBlock(
492521
) {
493522
const preEl = doc.createElement("PRE");
494523
preEl.classList.add("sourceCode");
495-
preEl.classList.add("code-with-copy");
496524

497525
const codeEl = doc.createElement("CODE");
498526
codeEl.classList.add("sourceCode");

‎src/format/html/format-html.ts‎

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,13 @@ import { bootstrapExtras } from "./format-html-bootstrap.ts";
6767
import {
6868
clipboardDependency,
6969
createCodeCopyButton,
70+
formatHasCodeCopy,
71+
htmlFormatFeatureDefaults,
7072
kAnchorSections,
7173
kAxe,
7274
kBootstrapDependencyName,
7375
kCitationsHover,
7476
kCodeAnnotations,
75-
kCodeCopy,
7677
kComments,
7778
kDocumentCss,
7879
kFootnotesHover,
@@ -306,11 +307,7 @@ export async function htmlFormatExtras(
306307
} else {
307308
options.tabby = format.metadata[kTabsets] || false;
308309
}
309-
if (featureDefaults.copyCode) {
310-
options.copyCode = format.metadata[kCodeCopy] !== false;
311-
} else {
312-
options.copyCode = format.metadata[kCodeCopy] || false;
313-
}
310+
options.copyCode = formatHasCodeCopy(format, featureDefaults.copyCode);
314311
if (featureDefaults.anchors) {
315312
options.anchors = format.metadata[kAnchorSections] !== false;
316313
} else {
@@ -682,23 +679,6 @@ function htmlFormatFilterParams(format: Format) {
682679
};
683680
}
684681

685-
function htmlFormatFeatureDefaults(
686-
format: Format,
687-
): HtmlFormatFeatureDefaults {
688-
const bootstrap = formatHasBootstrap(format);
689-
const minimal = format.metadata[kMinimal] === true;
690-
return {
691-
tabby: !minimal && !bootstrap,
692-
copyCode: !minimal,
693-
anchors: !minimal,
694-
hoverCitations: !minimal,
695-
hoverFootnotes: !minimal,
696-
figResponsive: !minimal,
697-
codeAnnotations: !minimal,
698-
hoverXrefs: !minimal,
699-
};
700-
}
701-
702682
function htmlFormatPostprocessor(
703683
format: Format,
704684
featureDefaults?: HtmlFormatFeatureDefaults,
@@ -712,9 +692,7 @@ function htmlFormatPostprocessor(
712692
}
713693

714694
// read options
715-
const codeCopy = featureDefaults.copyCode
716-
? format.metadata[kCodeCopy] !== false
717-
: format.metadata[kCodeCopy] || false;
695+
const codeCopy = formatHasCodeCopy(format, featureDefaults.copyCode);
718696

719697
const anchors = featureDefaults.anchors
720698
? format.metadata[kAnchorSections] !== false
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
title: "Citation BibTeX block without code-copy"
3+
author: "Author name"
4+
date: "2025-01-01"
5+
format: html
6+
citation: true
7+
code-copy: false
8+
_quarto:
9+
tests:
10+
html:
11+
ensureHtmlElements:
12+
-
13+
- '#quarto-citation pre.quarto-appendix-bibtex'
14+
-
15+
- '#quarto-citation button.code-copy-button'
16+
- '#quarto-citation div.code-copy-outer-scaffold'
17+
---
18+
19+
With `code-copy: false`, clipboard.js is not loaded, so a copy button on the BibTeX block in the citation appendix would do nothing. The BibTeX block is still rendered, but without the copy button or its scaffold, the same as other code blocks in the document (#15207).

0 commit comments

Comments
 (0)