All files / src/lib og-utils.ts

100% Statements 37/37
100% Branches 24/24
100% Functions 9/9
100% Lines 33/33

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                9x                       6x 3x                           4x 4x   4x 2x       2x 2x             32x             7x     5x 5x 1x   3x       5x 5x 5x       5x   5x             7x     5x 5x 1x   3x       5x 5x 5x       5x   5x                                 17x 13x 13x                      
/**
 * OG (Open Graph) image generation utilities
 */
 
/**
 * Escape special XML characters for safe use in SVG
 */
export function escapeXml(str: string): string {
	return str
		.replace(/&/g, "&")
		.replace(/</g, "&lt;")
		.replace(/>/g, "&gt;")
		.replace(/"/g, "&quot;")
		.replace(/'/g, "&apos;");
}
 
/**
 * Truncate text to a maximum length with ellipsis
 */
export function truncateText(text: string, maxLength: number): string {
	if (text.length <= maxLength) return text;
	return `${text.substring(0, maxLength - 1)}…`;
}
 
/**
 * Calculate optimal font size based on text length
 * Returns font size in pixels
 */
export function calculateFontSize(
	text: string,
	maxWidth: number,
	baseFontSize: number,
	minFontSize: number,
): number {
	// Rough approximation: average character width is ~0.6 of font size
	const charWidthRatio = 0.6;
	const textWidth = text.length * baseFontSize * charWidthRatio;
 
	if (textWidth <= maxWidth) {
		return baseFontSize;
	}
 
	// Calculate the font size that would fit
	const scaledSize = Math.floor(maxWidth / text.length / charWidthRatio);
	return Math.max(scaledSize, minFontSize);
}
 
/**
 * Validate hex color format
 */
export function isValidHexColor(color: string): boolean {
	return /^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/.test(color);
}
 
/**
 * Lighten a hex color by a percentage
 */
export function lightenColor(hex: string, percent: number): string {
	if (!isValidHexColor(hex)) return hex;
 
	// Remove # and expand short form
	let color = hex.replace("#", "");
	if (color.length === 3) {
		color = color
			.split("")
			.map((c) => c + c)
			.join("");
	}
 
	const num = Number.parseInt(color, 16);
	const r = Math.min(255, Math.floor((num >> 16) + 255 * (percent / 100)));
	const g = Math.min(
		255,
		Math.floor(((num >> 8) & 0x00ff) + 255 * (percent / 100)),
	);
	const b = Math.min(255, Math.floor((num & 0x0000ff) + 255 * (percent / 100)));
 
	return `#${((r << 16) | (g << 8) | b).toString(16).padStart(6, "0")}`;
}
 
/**
 * Darken a hex color by a percentage
 */
export function darkenColor(hex: string, percent: number): string {
	if (!isValidHexColor(hex)) return hex;
 
	// Remove # and expand short form
	let color = hex.replace("#", "");
	if (color.length === 3) {
		color = color
			.split("")
			.map((c) => c + c)
			.join("");
	}
 
	const num = Number.parseInt(color, 16);
	const r = Math.max(0, Math.floor((num >> 16) * (1 - percent / 100)));
	const g = Math.max(
		0,
		Math.floor(((num >> 8) & 0x00ff) * (1 - percent / 100)),
	);
	const b = Math.max(0, Math.floor((num & 0x0000ff) * (1 - percent / 100)));
 
	return `#${((r << 16) | (g << 8) | b).toString(16).padStart(6, "0")}`;
}
 
/**
 * OG image configuration interface
 */
export interface OGImageConfig {
	title: string;
	subtitle: string;
	icon: string;
	gradient: { from: string; to: string };
}
 
/**
 * Validate OG image configuration
 */
export function isValidOGConfig(config: unknown): config is OGImageConfig {
	if (typeof config !== "object" || config === null) return false;
	const c = config as Record<string, unknown>;
	return (
		typeof c.title === "string" &&
		c.title.length > 0 &&
		typeof c.subtitle === "string" &&
		typeof c.icon === "string" &&
		typeof c.gradient === "object" &&
		c.gradient !== null &&
		typeof (c.gradient as Record<string, unknown>).from === "string" &&
		typeof (c.gradient as Record<string, unknown>).to === "string"
	);
}