Skip to content

Commit 534a02a

Browse files
WIP: also add extra final highlight stylesheet
when author preference is light, we need a last light stylesheet for NoJS same principal as #12514 implementation is much worse includes a test which will still fail due to a11y stylesheet bleed #12522
1 parent 05138c1 commit 534a02a

File tree

5 files changed

+96
-16
lines changed

5 files changed

+96
-16
lines changed

src/command/render/pandoc-html.ts

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -295,19 +295,9 @@ export async function resolveSassBundles(
295295
}
296296
}
297297

298-
// Resolve generated quarto css variables
299-
if (hasDarkStyles && defaultStyle !== "dark") {
300-
// Put dark stylesheet first if light is default (for NoJS)
301-
extras = await resolveQuartoSyntaxHighlighting(
302-
inputDir,
303-
extras,
304-
format,
305-
project,
306-
"dark",
307-
defaultStyle,
308-
);
309-
}
310-
298+
// light only: light
299+
// author prefers dark: light, dark
300+
// author prefers light: light, dark, light
311301
extras = await resolveQuartoSyntaxHighlighting(
312302
inputDir,
313303
extras,
@@ -317,8 +307,12 @@ export async function resolveSassBundles(
317307
defaultStyle,
318308
);
319309

320-
if (hasDarkStyles && defaultStyle === "dark") {
321-
// Put dark stylesheet second if dark is default (for NoJS)
310+
if (hasDarkStyles) {
311+
const lightDep = extras.html?.[kDependencies]?.find((extraDep) =>
312+
extraDep.name === kQuartoHtmlDependency
313+
);
314+
const lightEntry = lightDep?.stylesheets &&
315+
lightDep.stylesheets[lightDep.stylesheets.length - 1];
322316
extras = await resolveQuartoSyntaxHighlighting(
323317
inputDir,
324318
extras,
@@ -327,6 +321,19 @@ export async function resolveSassBundles(
327321
"dark",
328322
defaultStyle,
329323
);
324+
if (defaultStyle === "light") {
325+
const dep2 = extras.html?.[kDependencies]?.find((extraDep) =>
326+
extraDep.name === kQuartoHtmlDependency
327+
);
328+
assert(dep2?.stylesheets && lightEntry);
329+
dep2.stylesheets.push({
330+
...lightEntry,
331+
attribs: {
332+
...lightEntry.attribs,
333+
class: "quarto-color-scheme-extra",
334+
},
335+
});
336+
}
330337
}
331338

332339
if (isHtmlOutput(format.pandoc, true)) {

src/resources/formats/html/templates/quarto-html-before-body.ejs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,8 @@
172172
<% } %>
173173
174174
<% if (!darkModeDefault) { %>
175-
document.querySelector('link.quarto-color-scheme-extra').rel = 'disabled-stylesheet';
175+
document.querySelector('link#quarto-text-highlighting-styles.quarto-color-scheme-extra').rel = 'disabled-stylesheet';
176+
document.querySelector('link#quarto-bootstrap.quarto-color-scheme-extra').rel = 'disabled-stylesheet';
176177
<% } %>
177178
178179
let localAlternateSentinel = darkModeDefault ? 'alternate' : 'default';
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
format: html
3+
theme:
4+
light: united
5+
dark: slate
6+
_quarto:
7+
tests:
8+
html:
9+
ensureHtmlElements:
10+
- []
11+
- ["link[href*=\"-dark-\"]"]
12+
---
13+
14+
## Hello
15+
16+
```markdown
17+
![asdf](asd.png)
18+
```
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
---
2+
title: "author prefers light theme - syntax highlighting"
3+
execute:
4+
echo: false
5+
warning: false
6+
theme:
7+
light: [cosmo, theme.scss]
8+
dark: [cosmo, theme-dark.scss]
9+
highlight-style: a11y
10+
---
11+
12+
13+
14+
```` markdown
15+
---
16+
title: "matplotlib demo"
17+
format:
18+
html:
19+
code-fold: true
20+
jupyter: python3
21+
---
22+
23+
For a demonstration of a line plot on a polar axis, see @fig-polar.
24+
25+
```{{python}}
26+
#| label: fig-polar
27+
#| fig-cap: "A line plot on a polar axis"
28+
29+
import numpy as np
30+
import matplotlib.pyplot as plt
31+
32+
r = np.arange(0, 2, 0.01)
33+
theta = 2 * np.pi * r
34+
fig, ax = plt.subplots(
35+
subplot_kw = {'projection': 'polar'}
36+
)
37+
ax.plot(theta, r)
38+
ax.set_rticks([0.5, 1, 1.5, 2])
39+
ax.grid(True)
40+
plt.show()
41+
```
42+
````

tests/integration/playwright/tests/html-dark-mode-nojs.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,18 @@ test('Light brand default, no JS', async ({ page }) => {
2222
});
2323

2424

25+
test('Syntax highlighting, no JS', async ({ page }) => {
26+
// This document use a custom theme file that change the background color of the title banner
27+
// Same user defined color should be used in both dark and light theme
28+
await page.goto('./html/dark-brand/syntax-highlighting/syntax-highlighting.html');
29+
const locatr = await page.locator('body').first();
30+
await expect(locatr).toHaveClass('fullcontent quarto-light');
31+
await expect(locatr).toHaveCSS('background-color', 'rgb(255, 255, 255)');
32+
const importKeyword = await page.locator('span.im').first();
33+
await expect(importKeyword).toHaveCSS('color', 'rgb(84, 84, 84)');
34+
});
35+
36+
2537
test('Project dark brand default, no JS', async ({ page }) => {
2638
// This document use a custom theme file that change the background color of the title banner
2739
// Same user defined color should be used in both dark and light theme

0 commit comments

Comments
 (0)