Skip to content

Commit dc2898f

Browse files
authored
refactor(svg): centralize GHOST_HEIGHT_PX and other layout constants … (JhaSourav07#651)
refactor(svg): split layout and generator constants as per PR feedback Co-authored-by: Ixotic27 <ixotic27@users.noreply.github.com>
1 parent 9b64cab commit dc2898f

5 files changed

Lines changed: 30 additions & 20 deletions

File tree

lib/svg/constants.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.

lib/svg/generator.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,18 @@ import { TOWER_ANIMATION_CSS } from './animations';
55
import { computeTowers, type TowerData } from './layout';
66
import { sanitizeFont, sanitizeHexColor, sanitizeRadius, sanitizeGoogleFontUrl } from './sanitizer';
77

8-
import { SVG_WIDTH, SVG_HEIGHT, FONT_MAP } from './constants';
8+
import { SVG_WIDTH, SVG_HEIGHT, FONT_MAP, isFontKey } from './generatorConstants';
99

1010
// helpers
1111
function truncateUsername(name: string, max = 20): string {
1212
return name.length > max ? name.slice(0, max) + '…' : name;
1313
}
1414

15+
function getFontFromMap(font: string | null): string | null {
16+
if (!font) return null;
17+
const f = font.toLowerCase();
18+
return isFontKey(f) ? FONT_MAP[f] : null;
19+
}
1520
function getSizeScale(size?: 'small' | 'medium' | 'large'): number {
1621
if (size === 'small') return 400 / SVG_WIDTH;
1722
if (size === 'large') return 800 / SVG_WIDTH;
@@ -256,7 +261,7 @@ export function generateSVG(
256261
const text = `#${sanitizeHexColor(params.text, 'ffffff')}`;
257262

258263
const sanitizedFont = sanitizeFont(params.font);
259-
const predefinedFont = sanitizedFont ? FONT_MAP[sanitizedFont.toLowerCase()] : null;
264+
const predefinedFont = getFontFromMap(sanitizedFont);
260265
const isPredefinedFont = Boolean(predefinedFont);
261266
const selectedFont = isPredefinedFont
262267
? predefinedFont
@@ -300,7 +305,7 @@ function generateAutoThemeSVG(
300305
const safeUser = escapeXML(params.user || 'GitHub User');
301306
const sanitizedFont = sanitizeFont(params.font);
302307
const selectedFont = sanitizedFont
303-
? FONT_MAP[sanitizedFont.toLowerCase()] || `"${sanitizedFont}", sans-serif`
308+
? getFontFromMap(sanitizedFont) || `"${sanitizedFont}", sans-serif`
304309
: null;
305310
const statsFont = selectedFont || '"Space Grotesk", sans-serif';
306311
const sf = getSizeScale(params.size);
@@ -402,7 +407,7 @@ export function generateMonthlySVG(stats: MonthlyStats, params: BadgeParams): st
402407

403408
const sanitizeFont = (name: string) => name.replace(/[^a-zA-Z0-9\s-]/g, '').trim();
404409
const sanitizedFont = params.font ? sanitizeFont(params.font) : null;
405-
const predefinedFont = sanitizedFont ? FONT_MAP[sanitizedFont.toLowerCase()] : null;
410+
const predefinedFont = getFontFromMap(sanitizedFont);
406411
const isPredefinedFont = Boolean(predefinedFont);
407412
const selectedFont = isPredefinedFont
408413
? predefinedFont
@@ -489,7 +494,7 @@ function generateAutoThemeMonthlySVG(stats: MonthlyStats, params: BadgeParams):
489494
const safeUser = escapeXML(params.user || 'GitHub User');
490495
const sanitizeFont = (name: string) => name.replace(/[^a-zA-Z0-9\s-]/g, '').trim();
491496
const sanitizedFont = params.font ? sanitizeFont(params.font) : null;
492-
const predefinedFont = sanitizedFont ? FONT_MAP[sanitizedFont.toLowerCase()] : null;
497+
const predefinedFont = getFontFromMap(sanitizedFont);
493498
const isPredefinedFont = Boolean(predefinedFont);
494499
const selectedFont = isPredefinedFont
495500
? predefinedFont

lib/svg/generatorConstants.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export const SVG_WIDTH = 600;
2+
export const SVG_HEIGHT = 420;
3+
4+
export const FONT_MAP = {
5+
jetbrains: '"JetBrains Mono", monospace',
6+
fira: '"Fira Code", monospace',
7+
roboto: '"Roboto", sans-serif',
8+
} as const;
9+
10+
export type FontKey = keyof typeof FONT_MAP;
11+
12+
export function isFontKey(font: string): font is FontKey {
13+
return font in FONT_MAP;
14+
}

lib/svg/layout.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
LINEAR_SCALE_MULTIPLIER,
77
MAX_LOG_HEIGHT,
88
MAX_LINEAR_HEIGHT,
9-
} from './constants';
9+
} from './layoutConstants';
1010

1111
/** Shared layout data for a single isometric tower. */
1212
export interface FaceOpacity {

lib/svg/layoutConstants.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export const GHOST_HEIGHT_PX = 4;
2+
export const LOG_SCALE_MULTIPLIER = 12;
3+
export const LINEAR_SCALE_MULTIPLIER = 5;
4+
export const MAX_LOG_HEIGHT = 80;
5+
export const MAX_LINEAR_HEIGHT = 50;

0 commit comments

Comments
 (0)