Skip to content

Commit 1756832

Browse files
cdervclaude
andcommitted
Refactor: extract getHighlightTheme helper function
Consolidate repeated theme resolution logic into a single exported function. Three functions now use this helper instead of duplicating the fallback chain. - Add getHighlightTheme() with syntax-highlighting > highlight-style > default - Simplify readHighlightingTheme, hasAdaptiveTheme, hasTextHighlighting - Add 4 unit tests for getHighlightTheme (18 total) Co-Authored-By: Claude Opus 4.5 <[email protected]>
1 parent 31fcf2f commit 1756832

2 files changed

Lines changed: 51 additions & 26 deletions

File tree

src/quarto-core/text-highlighting.ts

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ export interface ThemeDescriptor {
2222
const kDarkSuffix = "dark";
2323
const kLightSuffix = "light";
2424

25+
// Resolve highlight theme from syntax-highlighting (new) or highlight-style (deprecated)
26+
export function getHighlightTheme(
27+
pandoc: FormatPandoc,
28+
): string | Record<string, string> {
29+
return pandoc[kSyntaxHighlighting] ||
30+
pandoc[kHighlightStyle] ||
31+
kDefaultHighlightStyle;
32+
}
33+
2534
export function textHighlightThemePath(
2635
inputDir: string,
2736
theme: string | Record<string, string>,
@@ -69,39 +78,23 @@ export function readHighlightingTheme(
6978
pandoc: FormatPandoc,
7079
style: "dark" | "light" | "default",
7180
): ThemeDescriptor | undefined {
72-
// Check both syntax-highlighting (new) and highlight-style (deprecated alias)
73-
const theme = pandoc[kSyntaxHighlighting] ||
74-
pandoc[kHighlightStyle] ||
75-
kDefaultHighlightStyle;
76-
if (theme) {
77-
const themeRaw = readTheme(inputDir, theme, style);
78-
if (themeRaw) {
79-
return {
80-
json: JSON.parse(themeRaw),
81-
isAdaptive: isAdaptiveTheme(theme),
82-
};
83-
} else {
84-
return undefined;
85-
}
86-
} else {
87-
return undefined;
81+
const theme = getHighlightTheme(pandoc);
82+
const themeRaw = readTheme(inputDir, theme, style);
83+
if (themeRaw) {
84+
return {
85+
json: JSON.parse(themeRaw),
86+
isAdaptive: isAdaptiveTheme(theme),
87+
};
8888
}
89+
return undefined;
8990
}
9091

9192
export function hasAdaptiveTheme(pandoc: FormatPandoc) {
92-
// Check both syntax-highlighting (new) and highlight-style (deprecated alias)
93-
const theme = pandoc[kSyntaxHighlighting] ||
94-
pandoc[kHighlightStyle] ||
95-
kDefaultHighlightStyle;
96-
return theme && isAdaptiveTheme(theme);
93+
return isAdaptiveTheme(getHighlightTheme(pandoc));
9794
}
9895

9996
export function hasTextHighlighting(pandoc: FormatPandoc): boolean {
100-
// Check both syntax-highlighting (new) and highlight-style (deprecated alias)
101-
const theme = pandoc[kSyntaxHighlighting] ||
102-
pandoc[kHighlightStyle] ||
103-
kDefaultHighlightStyle;
104-
return theme !== "none";
97+
return getHighlightTheme(pandoc) !== "none";
10598
}
10699

107100
export function isAdaptiveTheme(theme: string | Record<string, string>) {

tests/unit/text-highlighting.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,44 @@
77
import { unitTest } from "../test.ts";
88
import { assertEquals } from "testing/asserts";
99
import {
10+
getHighlightTheme,
1011
hasAdaptiveTheme,
1112
hasTextHighlighting,
1213
isAdaptiveTheme,
1314
} from "../../src/quarto-core/text-highlighting.ts";
1415
import { FormatPandoc } from "../../src/config/types.ts";
1516

17+
// getHighlightTheme tests
18+
19+
// deno-lint-ignore require-await
20+
unitTest("getHighlightTheme - returns default when no options set", async () => {
21+
const pandoc: FormatPandoc = {};
22+
assertEquals(getHighlightTheme(pandoc), "arrow");
23+
});
24+
25+
// deno-lint-ignore require-await
26+
unitTest("getHighlightTheme - returns syntax-highlighting when set", async () => {
27+
const pandoc: FormatPandoc = { "syntax-highlighting": "github" };
28+
assertEquals(getHighlightTheme(pandoc), "github");
29+
});
30+
31+
// deno-lint-ignore require-await
32+
unitTest("getHighlightTheme - returns highlight-style as fallback", async () => {
33+
const pandoc: FormatPandoc = { "highlight-style": "monokai" };
34+
assertEquals(getHighlightTheme(pandoc), "monokai");
35+
});
36+
37+
// deno-lint-ignore require-await
38+
unitTest("getHighlightTheme - syntax-highlighting takes precedence", async () => {
39+
const pandoc: FormatPandoc = {
40+
"syntax-highlighting": "github",
41+
"highlight-style": "monokai",
42+
};
43+
assertEquals(getHighlightTheme(pandoc), "github");
44+
});
45+
46+
// hasTextHighlighting tests
47+
1648
// deno-lint-ignore require-await
1749
unitTest("text-highlighting - returns true when no options set (default applies)", async () => {
1850
const pandoc: FormatPandoc = {};

0 commit comments

Comments
 (0)