Skip to content

Commit 641d7c4

Browse files
committed
Sort token CSS rules by keyword using Normal style first
1 parent 7af2986 commit 641d7c4

File tree

1 file changed

+35
-11
lines changed

1 file changed

+35
-11
lines changed

src/command/render/pandoc-html.ts

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -499,14 +499,24 @@ function generateThemeCssClasses(
499499
>;
500500
if (textStyles) {
501501
const otherLines: string[] = [];
502-
const tokenCssLines: string[] = [];
503-
504-
const toCSS = function (abbr: string, cssValues: string[]) {
505-
tokenCssLines.push(`\ncode span${abbr !== "" ? `.${abbr}` : ""} {`);
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}` : ""} {`);
506513
cssValues.forEach((value) => {
507-
tokenCssLines.push(` ${value}`);
514+
lines.push(` ${value}`);
508515
});
509-
tokenCssLines.push("}\n");
516+
lines.push("}\n");
517+
518+
// Store by abbreviation for sorting later
519+
tokenCssByAbbr[abbr] = lines;
510520
};
511521

512522
Object.keys(textStyles).forEach((styleName) => {
@@ -515,9 +525,7 @@ function generateThemeCssClasses(
515525
const textValues = textStyles[styleName];
516526
const cssValues = generateCssKeyValues(textValues);
517527

518-
tokenCssLines.push(`/* ${styleName} */`);
519-
520-
toCSS(abbr, cssValues);
528+
toCSS(abbr, styleName, cssValues);
521529

522530
if (abbr == "") {
523531
[
@@ -533,8 +541,24 @@ function generateThemeCssClasses(
533541
}
534542
}
535543
});
536-
// return otherLines followed by tokenCssLines
537-
return otherLines.concat(tokenCssLines);
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);
538562
}
539563
return undefined;
540564
}

0 commit comments

Comments
 (0)