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>
This commit is contained in:
+20
-22
@@ -9,6 +9,7 @@
|
||||
import * as cheerio from "cheerio";
|
||||
import type { Element } from "domhandler";
|
||||
import { sanitizeBlockquoteContent } from "@/lib/blockquote-content";
|
||||
import { detectBlockquoteVariant } from "@/lib/blockquote-variant";
|
||||
import {
|
||||
detectTableVariant,
|
||||
sanitizeTableCellContent,
|
||||
@@ -39,7 +40,7 @@ export interface BlockquoteBlockPayload {
|
||||
/** HTML محتوای نقلقول (بدون تگ cite). */
|
||||
content: string;
|
||||
citation?: string;
|
||||
/** استایل رنگی؛ پیشفرض طوسی. از کلاسهای quote-yellow | quote-green | quote-gray در وردپرس. */
|
||||
/** استایل رنگی؛ از کلاس وردپرس یا تشخیص خودکار متن. */
|
||||
variant?: BlockquoteVariant;
|
||||
}
|
||||
|
||||
@@ -341,8 +342,6 @@ function discoverFaqCollapseBlocks(
|
||||
return faqEntries;
|
||||
}
|
||||
|
||||
const BLOCKQUOTE_VARIANT_CYCLE: BlockquoteVariant[] = ["yellow", "green", "gray"];
|
||||
|
||||
function parseExplicitBlockquoteVariant(
|
||||
classList: string[]
|
||||
): BlockquoteVariant | undefined {
|
||||
@@ -366,18 +365,28 @@ function parseExplicitBlockquoteVariant(
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function resolveBlockquoteVariant(
|
||||
content: string,
|
||||
citation: string | undefined,
|
||||
explicit?: BlockquoteVariant
|
||||
): BlockquoteVariant {
|
||||
if (explicit) return explicit;
|
||||
return detectBlockquoteVariant(content, citation);
|
||||
}
|
||||
|
||||
function extractBlockquotePayload(
|
||||
$: cheerio.CheerioAPI,
|
||||
el: Element
|
||||
): BlockquoteBlockPayload {
|
||||
const $el = $(el);
|
||||
const classList = ($el.attr("class") ?? "").split(/\s+/);
|
||||
const variant = parseExplicitBlockquoteVariant(classList);
|
||||
const explicitVariant = parseExplicitBlockquoteVariant(classList);
|
||||
const citation = $el.find("cite").first().text().trim() || undefined;
|
||||
const $body = $el.clone();
|
||||
$body.find("cite").remove();
|
||||
const content = sanitizeBlockquoteContent($body.html() ?? "");
|
||||
return { content, citation, ...(variant ? { variant } : {}) };
|
||||
const variant = resolveBlockquoteVariant(content, citation, explicitVariant);
|
||||
return { content, citation, variant };
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -393,13 +402,14 @@ function extractPullquotePayload(
|
||||
...($el.attr("class") ?? "").split(/\s+/),
|
||||
...($el.find("blockquote").first().attr("class") ?? "").split(/\s+/),
|
||||
];
|
||||
const variant = parseExplicitBlockquoteVariant(classList);
|
||||
const explicitVariant = parseExplicitBlockquoteVariant(classList);
|
||||
const $blockquote = $el.find("blockquote").first();
|
||||
const citation = $blockquote.find("cite").first().text().trim() || undefined;
|
||||
const $body = $blockquote.clone();
|
||||
$body.find("cite").remove();
|
||||
const content = sanitizeBlockquoteContent($body.html() ?? "");
|
||||
return { content, citation, ...(variant ? { variant } : {}) };
|
||||
const variant = resolveBlockquoteVariant(content, citation, explicitVariant);
|
||||
return { content, citation, variant };
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -410,7 +420,7 @@ function extractTablePayload(
|
||||
el: Element
|
||||
): TableBlockPayload {
|
||||
const $el = $(el);
|
||||
const variant = detectTableVariant($el.text());
|
||||
const rawText = $el.text();
|
||||
const captionRaw = $el.find("figcaption").first().text().trim();
|
||||
const caption = captionRaw
|
||||
? sanitizeTableCellContent(captionRaw)
|
||||
@@ -451,6 +461,8 @@ function extractTablePayload(
|
||||
}
|
||||
}
|
||||
|
||||
const variant = detectTableVariant({ rawText, caption, headers, rows });
|
||||
|
||||
return { variant, headers, rows, ...(caption ? { caption } : {}) };
|
||||
}
|
||||
|
||||
@@ -576,20 +588,6 @@ export function discoverBlocks(html: string): DiscoveredContent {
|
||||
// مرتبسازی بر اساس ترتیب در سند (از آخر به اول جایگزین میکنیم تا ایندکسها جابجا نشوند)
|
||||
entries.sort((a, b) => orderMap.get(a.el)! - orderMap.get(b.el)!);
|
||||
|
||||
// اگر وردپرس کلاس رنگی نفرستاده، بهترتیب زرد → سبز → طوسی چرخش میدهد
|
||||
let quoteAutoIndex = 0;
|
||||
for (const entry of entries) {
|
||||
if (entry.blockType !== "blockquote" && entry.blockType !== "pullquote") {
|
||||
continue;
|
||||
}
|
||||
const payload = entry.payload as BlockquoteBlockPayload;
|
||||
if (!payload.variant) {
|
||||
payload.variant =
|
||||
BLOCKQUOTE_VARIANT_CYCLE[quoteAutoIndex % BLOCKQUOTE_VARIANT_CYCLE.length];
|
||||
}
|
||||
quoteAutoIndex++;
|
||||
}
|
||||
|
||||
// جایگزینی با placeholder از آخر به اول
|
||||
for (let i = entries.length - 1; i >= 0; i--) {
|
||||
const { el, blockType, blockId, removeNodes } = entries[i];
|
||||
|
||||
Reference in New Issue
Block a user