All files / packages/css/src syntax.ts

100% Statements 26/26
100% Branches 4/4
100% Functions 4/4
100% Lines 26/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351                                  4x                                                                         88x 88x 1496x       88x 88x 88x 88x 88x 88x 88x 88x 88x 88x 88x 88x 88x 88x 88x 88x 88x   88x             4x                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 3x   3x                       1x    
// SPDX-License-Identifier: MIT
/**
 * Syntax highlighting styles for Turbo Themes.
 *
 * Provides CSS for code syntax highlighting that uses Turbo theme variables.
 * Compatible with Rouge (Jekyll), Prism, highlight.js, and other highlighters.
 *
 * @packageDocumentation
 */
 
import type { ThemeFlavor, ThemeTokens } from '@turbocoder13/turbo-themes-core';
import { CSS_VAR_PREFIX } from './generator.js';
 
/**
 * Syntax highlighting token types and their CSS class mappings.
 * These cover the common classes used by most syntax highlighters.
 */
const SYNTAX_CLASS_GROUPS = {
  // Comments
  comment: ['.c', '.cm', '.c1', '.cs', '.cp', '.cpf'],
  // Keywords (control flow, declarations)
  keyword: ['.k', '.kc', '.kd', '.kn', '.kp', '.kr', '.kt', '.kv'],
  // Strings
  string: ['.s', '.s1', '.s2', '.sb', '.sc', '.sd', '.se', '.sh', '.si', '.sr', '.ss', '.sx'],
  // Numbers
  number: ['.m', '.mi', '.mf', '.mh', '.mo', '.mb', '.mx', '.il'],
  // Functions and methods
  function: ['.nf', '.fm', '.nb'],
  // Classes and types
  type: ['.nc', '.nn', '.no', '.nd'],
  // Variables and identifiers
  variable: ['.n', '.na', '.nv', '.vc', '.vg', '.vi', '.vm'],
  // Operators
  operator: ['.o', '.ow'],
  // Punctuation
  punctuation: ['.p'],
  // HTML/XML tags
  tag: ['.nt', '.nx'],
  // Attributes
  attribute: ['.na', '.atn'],
  // Values (attribute values, etc.)
  value: ['.atv'],
  // Errors
  error: ['.err', '.gr'],
  // Deletions (diffs)
  deleted: ['.gd'],
  // Insertions (diffs)
  inserted: ['.gi'],
} as const;
 
/**
 * Generates CSS variable declarations for syntax highlighting.
 */
export function generateSyntaxVarsFromTokens(tokens: ThemeTokens): string[] {
  const lines: string[] = [];
  const add = (name: string, value: string): void => {
    lines.push(`  --${CSS_VAR_PREFIX}-syntax-${name}: ${value};`);
  };
 
  // Map theme tokens to syntax colors
  add('fg', tokens.content?.codeBlock?.fg ?? tokens.text.primary);
  add('bg', tokens.content?.codeBlock?.bg ?? tokens.background.surface);
  add('comment', tokens.text.secondary);
  add('keyword', tokens.brand.primary);
  add('string', tokens.state.success);
  add('number', tokens.state.warning);
  add('function', tokens.state.info);
  add('type', tokens.brand.primary);
  add('variable', tokens.text.primary);
  add('operator', tokens.text.secondary);
  add('punctuation', tokens.text.secondary);
  add('tag', tokens.state.danger);
  add('attribute', tokens.state.info);
  add('value', tokens.state.success);
  add('error', tokens.state.danger);
  add('deleted', tokens.state.danger);
  add('inserted', tokens.state.success);
 
  return lines;
}
 
/**
 * CSS styles for syntax highlighting using Turbo variables.
 * Works with Rouge, Prism, highlight.js, and similar highlighters.
 */
export const CSS_SYNTAX = `/* Turbo Themes - Syntax Highlighting */
 
/* Astro Code component css-variables theme mapping */
:root {
  --astro-code-color-text: var(--turbo-syntax-fg, var(--turbo-text-primary));
  --astro-code-color-background: var(--turbo-syntax-bg, var(--turbo-code-block-bg));
  --astro-code-token-constant: var(--turbo-syntax-number);
  --astro-code-token-string: var(--turbo-syntax-string);
  --astro-code-token-comment: var(--turbo-syntax-comment);
  --astro-code-token-keyword: var(--turbo-syntax-keyword);
  --astro-code-token-parameter: var(--turbo-syntax-variable);
  --astro-code-token-function: var(--turbo-syntax-function);
  --astro-code-token-string-expression: var(--turbo-syntax-string);
  --astro-code-token-punctuation: var(--turbo-syntax-punctuation);
  --astro-code-token-link: var(--turbo-accent-link, var(--turbo-brand-primary));
  /* Additional Astro tokens */
  --astro-code-background: var(--turbo-syntax-bg, var(--turbo-code-block-bg));
  --astro-code-foreground: var(--turbo-syntax-fg, var(--turbo-text-primary));
}
 
/* Astro Code component styling */
.astro-code,
pre.astro-code {
  background-color: var(--turbo-syntax-bg, var(--turbo-code-block-bg)) !important;
  padding: var(--space-lg);
  border-radius: var(--radius-lg);
  overflow-x: auto;
  border: 1px solid var(--turbo-border-default);
}
 
.astro-code code {
  background-color: transparent !important;
  color: inherit;
}
 
/* Base highlight container - only applies to pre elements */
.highlight,
pre.highlight,
pre[class*="language-"] {
  background-color: var(--turbo-syntax-bg, var(--turbo-code-block-bg));
  color: var(--turbo-syntax-fg, var(--turbo-code-block-fg));
}
 
/* Code inside pre should have transparent background */
.highlight pre,
.highlight code,
pre code,
pre code[class*="language-"] {
  background-color: transparent;
  color: inherit;
}
 
/* Comments */
.highlight .c,
.highlight .cm,
.highlight .c1,
.highlight .cs,
.highlight .cp,
.highlight .cpf,
.token.comment,
.token.prolog,
.token.doctype,
.token.cdata,
.hljs-comment,
.hljs-quote {
  color: var(--turbo-syntax-comment);
  font-style: italic;
}
 
/* Keywords */
.highlight .k,
.highlight .kc,
.highlight .kd,
.highlight .kn,
.highlight .kp,
.highlight .kr,
.highlight .kt,
.highlight .kv,
.token.keyword,
.token.atrule,
.token.important,
.hljs-keyword,
.hljs-selector-tag,
.hljs-built_in,
.hljs-type {
  color: var(--turbo-syntax-keyword);
}
 
/* Strings */
.highlight .s,
.highlight .s1,
.highlight .s2,
.highlight .sb,
.highlight .sc,
.highlight .sd,
.highlight .se,
.highlight .sh,
.highlight .si,
.highlight .sr,
.highlight .ss,
.highlight .sx,
.token.string,
.token.char,
.token.regex,
.token.template-string,
.hljs-string,
.hljs-regexp {
  color: var(--turbo-syntax-string);
}
 
/* Numbers */
.highlight .m,
.highlight .mi,
.highlight .mf,
.highlight .mh,
.highlight .mo,
.highlight .mb,
.highlight .mx,
.highlight .il,
.token.number,
.token.boolean,
.token.constant,
.hljs-number,
.hljs-literal {
  color: var(--turbo-syntax-number);
}
 
/* Functions */
.highlight .nf,
.highlight .fm,
.highlight .nb,
.token.function,
.token.builtin,
.hljs-title.function_,
.hljs-built_in {
  color: var(--turbo-syntax-function);
}
 
/* Classes and types */
.highlight .nc,
.highlight .nn,
.highlight .no,
.highlight .nd,
.token.class-name,
.token.namespace,
.hljs-title.class_,
.hljs-class .hljs-title {
  color: var(--turbo-syntax-type);
}
 
/* Variables and identifiers */
.highlight .n,
.highlight .nv,
.highlight .vc,
.highlight .vg,
.highlight .vi,
.highlight .vm,
.token.variable,
.hljs-variable,
.hljs-template-variable {
  color: var(--turbo-syntax-variable);
}
 
/* Operators */
.highlight .o,
.highlight .ow,
.token.operator,
.hljs-operator {
  color: var(--turbo-syntax-operator);
}
 
/* Punctuation */
.highlight .p,
.token.punctuation,
.hljs-punctuation {
  color: var(--turbo-syntax-punctuation);
}
 
/* HTML/XML tags */
.highlight .nt,
.highlight .nx,
.token.tag,
.hljs-tag,
.hljs-name {
  color: var(--turbo-syntax-tag);
}
 
/* Attributes */
.highlight .na,
.highlight .atn,
.token.attr-name,
.hljs-attr {
  color: var(--turbo-syntax-attribute);
}
 
/* Attribute values */
.highlight .atv,
.token.attr-value,
.hljs-attr .hljs-string {
  color: var(--turbo-syntax-value);
}
 
/* Errors */
.highlight .err,
.highlight .gr,
.token.error,
.hljs-error {
  color: var(--turbo-syntax-error);
}
 
/* Diff: deletions */
.highlight .gd,
.token.deleted,
.hljs-deletion {
  color: var(--turbo-syntax-deleted);
  background-color: color-mix(in srgb, var(--turbo-syntax-deleted) 15%, transparent);
}
 
/* Diff: insertions */
.highlight .gi,
.token.inserted,
.hljs-addition {
  color: var(--turbo-syntax-inserted);
  background-color: color-mix(in srgb, var(--turbo-syntax-inserted) 15%, transparent);
}
 
/* Line numbers (if used) */
.highlight .lineno,
.line-numbers .line-numbers-rows,
.hljs-ln-numbers {
  color: var(--turbo-text-secondary);
  opacity: 0.5;
  user-select: none;
}
 
/* Selection within code */
.highlight ::selection,
pre[class*="language-"] ::selection,
code[class*="language-"] ::selection {
  background-color: var(--turbo-selection-bg);
  color: var(--turbo-selection-fg);
}
`;
 
/**
 * Generates syntax highlighting CSS for a specific theme.
 * This includes both the variable definitions and the styling rules.
 */
export function generateSyntaxCss(flavor: ThemeFlavor): string {
  const vars = generateSyntaxVarsFromTokens(flavor.tokens);
 
  return `/* Turbo Themes - Syntax Highlighting for ${flavor.label} */
[data-theme="${flavor.id}"] {
${vars.join('\n')}
}
`;
}
 
/**
 * Generates the complete syntax highlighting CSS file.
 * Includes base styles that work with any theme.
 */
export function generateSyntaxBaseCss(): string {
  return CSS_SYNTAX;
}