Files
aragrp/lib/table-content.ts
ar.badieiandCursor 6cb9e2aac5 feat: auto-detect blockquote and table colors from content
Replace manual hashtags and color cycling with content-tone heuristics for blockquotes and tables, and refine blockquote layout with a centered icon, larger sizing, and clearer contrast over background decorations.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-12 01:55:50 +03:30

74 lines
2.0 KiB
TypeScript

import type { TableVariant } from "@/lib/blog-blocks";
import { detectContentTone, stripHtml } from "@/lib/content-tone";
/** حذف برچسب‌های انتخاب UI (#one, #two) از محتوای سلول جدول. */
export function sanitizeTableCellContent(html: string): string {
return html.replace(/#(?:one|two)\b/gi, "").trim();
}
/** سیگنال‌های هشدار مخصوص جدول (مقایسه، معایب، محدودیت). */
const TABLE_WARNING_PATTERNS: RegExp[] = [
/معایب/,
/محدودیت/,
/نکات منفی/,
/هزینه.?ها/,
/چالش.?ها/,
/مشکلات/,
/ضعف/,
/منفی/,
/اجتناب/,
/اجتناب کنید/,
];
/** سیگنال‌های مثبت مخصوص جدول (مزایا، ویژگی‌ها، پیشنهاد). */
const TABLE_POSITIVE_PATTERNS: RegExp[] = [
/مزایا/,
/مزیت/,
/ویژگی.?ها/,
/قابلیت.?ها/,
/پیشنهاد/,
/بهترین/,
/مزیت رقابتی/,
/خلاصه/,
];
function buildTableText(parts: {
caption?: string;
headers?: string[];
rows?: string[][];
}): string {
const chunks = [
parts.caption ?? "",
...(parts.headers ?? []),
...(parts.rows ?? []).flat(),
];
return stripHtml(chunks.join(" "));
}
export interface DetectTableVariantInput {
/** متن خام figure (برای override با #one/#two در محتوای قدیمی). */
rawText?: string;
caption?: string;
headers?: string[];
rows?: string[][];
}
/**
* تشخیص variant جدول: one = سبز (primary)، two = زرد (accent).
* اولویت: hashtag صریح → هشدار → مثبت/خنثی (سبز).
*/
export function detectTableVariant(input: DetectTableVariantInput): TableVariant {
const raw = input.rawText ?? "";
if (/#two\b/i.test(raw)) return "two";
if (/#one\b/i.test(raw)) return "one";
const text = buildTableText(input);
const tone = detectContentTone(text, {
warning: TABLE_WARNING_PATTERNS,
positive: TABLE_POSITIVE_PATTERNS,
});
if (tone === "warning") return "two";
return "one";
}