Skip to content

Commit 5b2a4c5

Browse files
authored
feat(formatter): add writeCSSComments option (#1112)
Currently when writing the css file comments before each class is present. ```css /* Background */ .bg { color: #cdd6f4; background-color: #1e1e2e;-moz-tab-size: 2; -o-tab-size: 2; tab-size: 2; } /* PreWrapper */ .chroma { color: #cdd6f4; background-color: #1e1e2e;-moz-tab-size: 2; -o-tab-size: 2; tab-size: 2; } /* Error */ .chroma .err { color: #f38ba8 } /* LineLink */ .chroma .lnlinks { outline: none; text-decoration: none; color: inherit } /* LineTableTD */ .chroma .lntd { vertical-align: top; padding: 0; margin: 0; border: 0; } /* LineTable */ .chroma .lntable { border-spacing: 0; padding: 0; margin: 0; border: 0; } /* LineHighlight */ .chroma .hl { background-color: #45475a } ... ``` (Also why is there a .bg class, feels like that could collide) This is probably a micro-optimization but felt like the comments didn't serve any purpose except clarity and debug value. Also skipped writing empty css classes. Before and after writing css file for style "catppuccin-mocha" ```sh > ls -l -B .rw-r--r-- 4,934 jocke 25 Jul 01:51 with.css .rw-r--r-- 3,337 jocke 25 Jul 01:50 without.css ``` I didn't wanna make any changes to the output without changes to the options. Maybe we could just remove the comments all together and skip this extra logic.
1 parent 02ff9d4 commit 5b2a4c5

File tree

1 file changed

+33
-9
lines changed

1 file changed

+33
-9
lines changed

formatters/html/html.go

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ func WithCustomCSS(css map[chroma.TokenType]string) Option {
3434
}
3535
}
3636

37+
// WithCSSComments adds prefixe comments to the css classes. Defaults to true.
38+
func WithCSSComments(b bool) Option { return func(f *Formatter) { f.writeCSSComments = b } }
39+
3740
// TabWidth sets the number of characters for a tab. Defaults to 8.
3841
func TabWidth(width int) Option { return func(f *Formatter) { f.tabWidth = width } }
3942

@@ -131,8 +134,9 @@ func BaseLineNumber(n int) Option {
131134
// New HTML formatter.
132135
func New(options ...Option) *Formatter {
133136
f := &Formatter{
134-
baseLineNumber: 1,
135-
preWrapper: defaultPreWrapper,
137+
baseLineNumber: 1,
138+
preWrapper: defaultPreWrapper,
139+
writeCSSComments: true,
136140
}
137141
f.styleCache = newStyleCache(f)
138142
for _, option := range options {
@@ -197,6 +201,7 @@ type Formatter struct {
197201
Classes bool // Exported field to detect when classes are being used
198202
allClasses bool
199203
customCSS map[chroma.TokenType]string
204+
writeCSSComments bool
200205
preWrapper PreWrapper
201206
inlineCode bool
202207
preventSurroundingPre bool
@@ -416,29 +421,49 @@ func (f *Formatter) tabWidthStyle() string {
416421
return ""
417422
}
418423

424+
func (f *Formatter) writeCSSRule(w io.Writer, comment string, selector string, styles string) error {
425+
if styles == "" {
426+
return nil
427+
}
428+
if f.writeCSSComments && comment != "" {
429+
if _, err := fmt.Fprintf(w, "/* %s */ ", comment); err != nil {
430+
return err
431+
}
432+
}
433+
if _, err := fmt.Fprintf(w, "%s { %s }\n", selector, styles); err != nil {
434+
return err
435+
}
436+
return nil
437+
}
438+
419439
// WriteCSS writes CSS style definitions (without any surrounding HTML).
420440
func (f *Formatter) WriteCSS(w io.Writer, style *chroma.Style) error {
421441
css := f.styleCache.get(style, false)
442+
422443
// Special-case background as it is mapped to the outer ".chroma" class.
423-
if _, err := fmt.Fprintf(w, "/* %s */ .%sbg { %s }\n", chroma.Background, f.prefix, css[chroma.Background]); err != nil {
444+
if err := f.writeCSSRule(w, chroma.Background.String(), fmt.Sprintf(".%sbg", f.prefix), css[chroma.Background]); err != nil {
424445
return err
425446
}
426447
// Special-case PreWrapper as it is the ".chroma" class.
427-
if _, err := fmt.Fprintf(w, "/* %s */ .%schroma { %s }\n", chroma.PreWrapper, f.prefix, css[chroma.PreWrapper]); err != nil {
448+
if err := f.writeCSSRule(w, chroma.PreWrapper.String(), fmt.Sprintf(".%schroma", f.prefix), css[chroma.PreWrapper]); err != nil {
428449
return err
429450
}
430451
// Special-case code column of table to expand width.
431452
if f.lineNumbers && f.lineNumbersInTable {
432-
if _, err := fmt.Fprintf(w, "/* %s */ .%schroma .%s:last-child { width: 100%%; }",
433-
chroma.LineTableTD, f.prefix, f.class(chroma.LineTableTD)); err != nil {
453+
selector := fmt.Sprintf(".%schroma .%s:last-child", f.prefix, f.class(chroma.LineTableTD))
454+
if err := f.writeCSSRule(w, chroma.LineTableTD.String(), selector, "width: 100%;"); err != nil {
434455
return err
435456
}
436457
}
437458
// Special-case line number highlighting when targeted.
438459
if f.lineNumbers || f.lineNumbersInTable {
439460
targetedLineCSS := StyleEntryToCSS(style.Get(chroma.LineHighlight))
440461
for _, tt := range []chroma.TokenType{chroma.LineNumbers, chroma.LineNumbersTable} {
441-
fmt.Fprintf(w, "/* %s targeted by URL anchor */ .%schroma .%s:target { %s }\n", tt, f.prefix, f.class(tt), targetedLineCSS)
462+
comment := fmt.Sprintf("%s targeted by URL anchor", tt)
463+
selector := fmt.Sprintf(".%schroma .%s:target", f.prefix, f.class(tt))
464+
if err := f.writeCSSRule(w, comment, selector, targetedLineCSS); err != nil {
465+
return err
466+
}
442467
}
443468
}
444469
tts := []int{}
@@ -456,8 +481,7 @@ func (f *Formatter) WriteCSS(w io.Writer, style *chroma.Style) error {
456481
if class == "" {
457482
continue
458483
}
459-
styles := css[tt]
460-
if _, err := fmt.Fprintf(w, "/* %s */ .%schroma .%s { %s }\n", tt, f.prefix, class, styles); err != nil {
484+
if err := f.writeCSSRule(w, tt.String(), fmt.Sprintf(".%schroma .%s", f.prefix, class), css[tt]); err != nil {
461485
return err
462486
}
463487
}

0 commit comments

Comments
 (0)