Skip to content

Commit 7d45e02

Browse files
authored
Merge pull request #12495 from quarto-dev/theme/css-highlight
HTML - fix block vs inline highlighting
2 parents b820c8b + 0150969 commit 7d45e02

35 files changed

+604
-65
lines changed

news/changelog-1.7.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,9 @@ All changes included in 1.7:
6868

6969
- ([#1325](https://github.com/quarto-dev/quarto-cli/issues/1325)): Dark Mode pages should not flash light on reload. (Nor should Light Mode pages flash dark.)
7070
- ([#1470](https://github.com/quarto-dev/quarto-cli/issues/1470)): `respect-user-color-scheme` enables checking the media query `prefers-color-scheme` for user dark mode preference. Author preference still influences stylesheet order and NoJS experience. Defaults to `false`, leaving to author preference.
71-
- ([#10780])(https://github.com/quarto-dev/quarto-cli/issues/10780)): improve `link-external-filter` documentation.
71+
- ([#10780](https://github.com/quarto-dev/quarto-cli/issues/10780)): improve `link-external-filter` documentation.
7272
- ([#11860](https://github.com/quarto-dev/quarto-cli/issues/11860)): ES6 modules that import other local JS modules in documents with `embed-resources: true` are now correctly embedded.
73+
- ([#11911](https://github.com/quarto-dev/quarto-cli/issues/11911)): Code highlighting colors for tokens are now the same between code blocks and inline code when using Pandoc's syntax highlighting.
7374
- ([#12277](https://github.com/quarto-dev/quarto-cli/pull/12277)): Provide light and dark plot and table renderings with `renderings: [light,dark]`
7475
- ([#12307](https://github.com/quarto-dev/quarto-cli/issues/12307)): Tabsets using `tabby.js` in non-boostrap html (`theme: pandoc`, `theme: none` or `minimal: true`) now correctly render reactive content when `server: shiny` is used.
7576
- ([#12319](https://github.com/quarto-dev/quarto-cli/pull/12319)): Provide switchable light and dark brands for a page with `brand.light` and `brand.dark`.

src/command/render/pandoc-html.ts

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -498,34 +498,67 @@ function generateThemeCssClasses(
498498
Record<string, unknown>
499499
>;
500500
if (textStyles) {
501-
const lines: string[] = [];
501+
const otherLines: string[] = [];
502+
otherLines.push("/* syntax highlight based on Pandoc's rules */");
503+
const tokenCssByAbbr: Record<string, string[]> = {};
504+
505+
const toCSS = function (
506+
abbr: string,
507+
styleName: string,
508+
cssValues: string[],
509+
) {
510+
const lines: string[] = [];
511+
lines.push(`/* ${styleName} */`);
512+
lines.push(`\ncode span${abbr !== "" ? `.${abbr}` : ""} {`);
513+
cssValues.forEach((value) => {
514+
lines.push(` ${value}`);
515+
});
516+
lines.push("}\n");
517+
518+
// Store by abbreviation for sorting later
519+
tokenCssByAbbr[abbr] = lines;
520+
};
502521

503522
Object.keys(textStyles).forEach((styleName) => {
504523
const abbr = kAbbrevs[styleName];
505524
if (abbr !== undefined) {
506525
const textValues = textStyles[styleName];
507526
const cssValues = generateCssKeyValues(textValues);
508527

509-
if (abbr !== "") {
510-
lines.push(`\ncode span.${abbr} {`);
511-
lines.push(...cssValues);
512-
lines.push("}\n");
513-
} else {
528+
toCSS(abbr, styleName, cssValues);
529+
530+
if (abbr == "") {
514531
[
515532
"pre > code.sourceCode > span",
516-
"code span",
517533
"code.sourceCode > span",
518534
"div.sourceCode,\ndiv.sourceCode pre.sourceCode",
519535
]
520536
.forEach((selector) => {
521-
lines.push(`\n${selector} {`);
522-
lines.push(...cssValues);
523-
lines.push("}\n");
537+
otherLines.push(`\n${selector} {`);
538+
otherLines.push(...cssValues);
539+
otherLines.push("}\n");
524540
});
525541
}
526542
}
527543
});
528-
return lines;
544+
545+
// Sort tokenCssLines by abbr and flatten them
546+
// Ensure empty abbr ("") comes first by using a custom sort function
547+
const sortedTokenCssLines: string[] = [];
548+
Object.keys(tokenCssByAbbr)
549+
.sort((a, b) => {
550+
// Empty string ("") should come first
551+
if (a === "") return -1;
552+
if (b === "") return 1;
553+
// Otherwise normal alphabetical sort
554+
return a.localeCompare(b);
555+
})
556+
.forEach((abbr) => {
557+
sortedTokenCssLines.push(...tokenCssByAbbr[abbr]);
558+
});
559+
560+
// return otherLines followed by tokenCssLines (now sorted by abbr)
561+
return otherLines.concat(sortedTokenCssLines);
529562
}
530563
return undefined;
531564
}

src/resources/editor/tools/vs-code.mjs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13031,29 +13031,31 @@ var require_yaml_intelligence_resources = __commonJS({
1303113031
{
1303213032
string: {
1303313033
completions: [
13034-
"pygments",
13035-
"tango",
13036-
"espresso",
13037-
"zenburn",
13038-
"kate",
13039-
"monochrome",
13040-
"breezedark",
13041-
"haddock",
13034+
"a11y",
1304213035
"arrow",
1304313036
"atom-one",
1304413037
"ayu",
1304513038
"ayu-mirage",
1304613039
"breeze",
13040+
"breezedark",
1304713041
"dracula",
13042+
"espresso",
1304813043
"github",
1304913044
"gruvbox",
13050-
"mokokai",
13045+
"haddock",
13046+
"kate",
13047+
"monochrome",
13048+
"monokai",
13049+
"none",
1305113050
"nord",
1305213051
"oblivion",
1305313052
"printing",
13053+
"pygments",
1305413054
"radical",
1305513055
"solarized",
13056-
"vim-dark"
13056+
"tango",
13057+
"vim-dark",
13058+
"zenburn"
1305713059
]
1305813060
}
1305913061
}
@@ -23986,7 +23988,7 @@ var require_yaml_intelligence_resources = __commonJS({
2398623988
"A url to the full text for this item.",
2398723989
{
2398823990
short: "Type, class, or subtype of the item",
23989-
long: "Type, class, or subtype of the item (e.g.&nbsp;\u201CDoctoral dissertation\u201D for\na PhD thesis; \u201CNIH Publication\u201D for an NIH technical report);\nDo not use for topical descriptions or categories (e.g.&nbsp;\uFFFD\uFFFDadventure\u201D\nfor an adventure movie)"
23991+
long: "Type, class, or subtype of the item (e.g.&nbsp;\u201CDoctoral dissertation\u201D for\na PhD thesis; \u201CNIH Publication\u201D for an NIH technical report);\nDo not use for topical descriptions or categories (e.g.&nbsp;\u201Cadventure\u201D\nfor an adventure movie)"
2399023992
},
2399123993
"Guest (e.g.&nbsp;on a TV show or podcast).",
2399223994
"Host of the item (e.g.&nbsp;of a TV show or podcast).",
@@ -24303,12 +24305,12 @@ var require_yaml_intelligence_resources = __commonJS({
2430324305
mermaid: "%%"
2430424306
},
2430524307
"handlers/mermaid/schema.yml": {
24306-
_internalId: 195064,
24308+
_internalId: 195005,
2430724309
type: "object",
2430824310
description: "be an object",
2430924311
properties: {
2431024312
"mermaid-format": {
24311-
_internalId: 195056,
24313+
_internalId: 194997,
2431224314
type: "enum",
2431324315
enum: [
2431424316
"png",
@@ -24324,7 +24326,7 @@ var require_yaml_intelligence_resources = __commonJS({
2432424326
exhaustiveCompletions: true
2432524327
},
2432624328
theme: {
24327-
_internalId: 195063,
24329+
_internalId: 195004,
2432824330
type: "anyOf",
2432924331
anyOf: [
2433024332
{

src/resources/editor/tools/yaml/web-worker.js

Lines changed: 16 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/resources/editor/tools/yaml/yaml-intelligence-resources.json

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6003,29 +6003,31 @@
60036003
{
60046004
"string": {
60056005
"completions": [
6006-
"pygments",
6007-
"tango",
6008-
"espresso",
6009-
"zenburn",
6010-
"kate",
6011-
"monochrome",
6012-
"breezedark",
6013-
"haddock",
6006+
"a11y",
60146007
"arrow",
60156008
"atom-one",
60166009
"ayu",
60176010
"ayu-mirage",
60186011
"breeze",
6012+
"breezedark",
60196013
"dracula",
6014+
"espresso",
60206015
"github",
60216016
"gruvbox",
6022-
"mokokai",
6017+
"haddock",
6018+
"kate",
6019+
"monochrome",
6020+
"monokai",
6021+
"none",
60236022
"nord",
60246023
"oblivion",
60256024
"printing",
6025+
"pygments",
60266026
"radical",
60276027
"solarized",
6028-
"vim-dark"
6028+
"tango",
6029+
"vim-dark",
6030+
"zenburn"
60296031
]
60306032
}
60316033
}
@@ -16958,7 +16960,7 @@
1695816960
"A url to the full text for this item.",
1695916961
{
1696016962
"short": "Type, class, or subtype of the item",
16961-
"long": "Type, class, or subtype of the item (e.g.&nbsp;“Doctoral dissertation” for\na PhD thesis; “NIH Publication” for an NIH technical report);\nDo not use for topical descriptions or categories (e.g.&nbsp;��adventure”\nfor an adventure movie)"
16963+
"long": "Type, class, or subtype of the item (e.g.&nbsp;“Doctoral dissertation” for\na PhD thesis; “NIH Publication” for an NIH technical report);\nDo not use for topical descriptions or categories (e.g.&nbsp;adventure”\nfor an adventure movie)"
1696216964
},
1696316965
"Guest (e.g.&nbsp;on a TV show or podcast).",
1696416966
"Host of the item (e.g.&nbsp;of a TV show or podcast).",
@@ -17275,12 +17277,12 @@
1727517277
"mermaid": "%%"
1727617278
},
1727717279
"handlers/mermaid/schema.yml": {
17278-
"_internalId": 195064,
17280+
"_internalId": 195005,
1727917281
"type": "object",
1728017282
"description": "be an object",
1728117283
"properties": {
1728217284
"mermaid-format": {
17283-
"_internalId": 195056,
17285+
"_internalId": 194997,
1728417286
"type": "enum",
1728517287
"enum": [
1728617288
"png",
@@ -17296,7 +17298,7 @@
1729617298
"exhaustiveCompletions": true
1729717299
},
1729817300
"theme": {
17299-
"_internalId": 195063,
17301+
"_internalId": 195004,
1730017302
"type": "anyOf",
1730117303
"anyOf": [
1730217304
{

src/resources/schema/document-code.yml

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,29 +105,31 @@
105105
dark: path
106106
- string:
107107
completions:
108-
- pygments
109-
- tango
110-
- espresso
111-
- zenburn
112-
- kate
113-
- monochrome
114-
- breezedark
115-
- haddock
108+
- a11y
116109
- arrow
117110
- atom-one
118111
- ayu
119112
- ayu-mirage
120113
- breeze
114+
- breezedark
121115
- dracula
116+
- espresso
122117
- github
123118
- gruvbox
124-
- mokokai
119+
- haddock
120+
- kate
121+
- monochrome
122+
- monokai
123+
- none
125124
- nord
126125
- oblivion
127126
- printing
127+
- pygments
128128
- radical
129129
- solarized
130+
- tango
130131
- vim-dark
132+
- zenburn
131133
description:
132134
short: Specifies the coloring style to be used in highlighted source code.
133135
long: |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/.quarto/
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
project:
2+
type: default
3+
title: "Quarto Code Highlighting"
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
title: Testing a11y code highlight
3+
highlight-style: a11y
4+
theme:
5+
light: flatly
6+
dark: darkly
7+
---
8+
9+
10+
```{.julia}
11+
function divide_floats(x::Float64, y::Float64)
12+
return x / y
13+
end
14+
```
15+
16+
And here the inline version `function divide_floats(x::Float64, y::Float64)`{.julia}.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
title: Testing arrow code highlight
3+
highlight-style: arrow
4+
theme:
5+
light: flatly
6+
dark: darkly
7+
---
8+
9+
10+
```{.julia}
11+
function divide_floats(x::Float64, y::Float64)
12+
return x / y
13+
end
14+
```
15+
16+
And here the inline version `function divide_floats(x::Float64, y::Float64)`{.julia}.

0 commit comments

Comments
 (0)