2024-08-13 18:27:56 +02:00
|
|
|
export function hashStringToColor(str: string): string {
|
2024-08-13 21:40:34 +02:00
|
|
|
if (!str) {
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-13 18:27:56 +02:00
|
|
|
let hash = 0;
|
|
|
|
|
for (let i = 0; i < str.length; i++) {
|
|
|
|
|
hash = str.charCodeAt(i) + ((hash << 5) - hash);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const r = (hash >> 24) & 0xff;
|
|
|
|
|
const g = (hash >> 16) & 0xff;
|
|
|
|
|
const b = (hash >> 8) & 0xff;
|
|
|
|
|
|
|
|
|
|
return `rgb(${r}, ${g}, ${b})`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getTextColorForBackground(rgb: string): string {
|
|
|
|
|
const [r, g, b] = rgb.match(/\d+/g)?.map(Number) || [0, 0, 0];
|
|
|
|
|
const brightness = r * 0.299 + g * 0.587 + b * 0.114;
|
|
|
|
|
return brightness > 125 ? 'black' : 'white';
|
|
|
|
|
}
|
2024-08-27 18:46:42 +02:00
|
|
|
|
2024-09-16 11:24:54 +02:00
|
|
|
export function removeMarkdown(md: string): string {
|
|
|
|
|
return md
|
2026-03-09 15:31:53 +08:00
|
|
|
.replace(/\[spoiler\](.*?)\[\/spoiler\]/gis, '$1') // spoiler tags - keep inner text
|
2026-03-04 15:20:06 +08:00
|
|
|
.replace(/\[([^\]]*?)\]\([^)]*\)/g, '$1') // [text](url) -> text
|
|
|
|
|
.replace(/ /g, ' ') // -> space
|
|
|
|
|
.replace(/^>\s*/gm, '') // greentext at line start
|
|
|
|
|
.replace(/[*_]/g, '') // bold/italic markers
|
|
|
|
|
.replace(/```[\s\S]*?```/g, (m) => m.slice(3, -3)) // code blocks - keep content
|
|
|
|
|
.replace(/`([^`]*)`/g, '$1') // inline code - keep content
|
2024-09-16 11:24:54 +02:00
|
|
|
.trim();
|
|
|
|
|
}
|