fix: 缩短事件节点图谱标签

This commit is contained in:
Youzini-afk
2026-03-25 00:37:44 +08:00
parent 63ee782028
commit 4319fd2496
6 changed files with 86 additions and 41 deletions

63
node-labels.js Normal file
View File

@@ -0,0 +1,63 @@
const DEFAULT_GRAPH_LABEL_LENGTH = 14;
const GRAPH_LABEL_LENGTH_BY_TYPE = {
character: 12,
event: 14,
location: 12,
thread: 14,
rule: 14,
synopsis: 16,
reflection: 14,
};
function normalizeLabelText(value) {
return String(value ?? "")
.replace(/\s+/g, " ")
.trim();
}
export function truncateNodeLabel(text, maxLength = DEFAULT_GRAPH_LABEL_LENGTH) {
const normalized = normalizeLabelText(text);
if (!normalized) return "—";
if (!Number.isFinite(maxLength) || maxLength < 2) return normalized;
if (normalized.length <= maxLength) return normalized;
return `${normalized.slice(0, Math.max(1, maxLength - 1)).trimEnd()}`;
}
export function deriveEventTitleFromSummary(summary, maxLength = 18) {
const normalized = normalizeLabelText(summary).replace(/^事件[:]\s*/, "");
if (!normalized) return "";
const clause =
normalized.split(/[\r\n]+/, 1)[0]?.split(/[。!?!?]/, 1)[0]?.split(/[;]/, 1)[0]?.split(/[,]/, 1)[0] ||
normalized;
return truncateNodeLabel(clause || normalized, maxLength);
}
export function ensureEventTitle(fields = {}) {
const nextFields = { ...(fields || {}) };
if (!nextFields.title && nextFields.summary) {
nextFields.title = deriveEventTitleFromSummary(nextFields.summary);
}
return nextFields;
}
export function getNodeDisplayName(node) {
const label = normalizeLabelText(
node?.fields?.name ||
node?.fields?.title ||
node?.fields?.summary ||
node?.fields?.insight ||
node?.name ||
node?.id?.slice(0, 8) ||
"—",
);
return label || "—";
}
export function getGraphNodeLabel(node) {
const maxLength =
GRAPH_LABEL_LENGTH_BY_TYPE[node?.type] || DEFAULT_GRAPH_LABEL_LENGTH;
return truncateNodeLabel(getNodeDisplayName(node), maxLength);
}