89 lines
2.8 KiB
TypeScript
89 lines
2.8 KiB
TypeScript
/** Public presentation only. Originals, source records and management fields stay intact. */
|
|
export function publicReference(
|
|
value: string | null | undefined,
|
|
): string | null {
|
|
const text = value?.trim();
|
|
if (!text) return null;
|
|
if (/^(?:javascript|data|vbscript|file):/i.test(text)) return null;
|
|
if (/^https?:\/\//i.test(text)) {
|
|
try {
|
|
const u = new URL(text);
|
|
if (
|
|
u.username ||
|
|
u.password ||
|
|
/^(?:localhost|127\.|10\.|192\.168\.|172\.(?:1[6-9]|2\d|3[01])\.|\[?::1)/i.test(
|
|
u.hostname,
|
|
)
|
|
)
|
|
return null;
|
|
} catch {
|
|
return null;
|
|
}
|
|
if (
|
|
/(?:\/(?:outputs|resources\/js|node_modules|tests)\/|\.(?:tsx?|jsx?|sql|py|cjs|mjs)(?:[?#]|$))/i.test(
|
|
new URL(text).pathname,
|
|
)
|
|
)
|
|
return null;
|
|
return text;
|
|
}
|
|
if (
|
|
/(?:[A-Z]:[\\/]|\\\\|file:\/\/|(?:^|[\s/])(?:outputs|resources|storage|vendor|node_modules|docs|tests|app)[\\/]|\b(?:localhost|127\.0\.0\.\d|0\.0\.0\.0)\b|\.(?:tsx?|jsx?|py|php|sql|md|json|log|cjs|mjs|docx?|xlsx?|xlsm|emf)(?:\b|$)|\b(?:MASTER|AUD|QA|REQ|ME|PN)[-_ ](?:[A-Z]*\d|[A-Z0-9]{6,})|\bgit\s+(?:commit|branch|diff)\b)/i.test(
|
|
text,
|
|
)
|
|
)
|
|
return null;
|
|
return text;
|
|
}
|
|
export function uniquePresentation<T>(
|
|
rows: readonly T[],
|
|
key: (row: T) => string,
|
|
): T[] {
|
|
const seen = new Set<string>();
|
|
return rows.filter((row) => {
|
|
const id = key(row);
|
|
if (seen.has(id)) return false;
|
|
seen.add(id);
|
|
return true;
|
|
});
|
|
}
|
|
export function publicLocation(
|
|
path: { name: string; location_type?: string }[] | undefined,
|
|
fallback: (string | null | undefined)[] = [],
|
|
): string {
|
|
const names = (path?.length ? path.map((p) => p.name) : fallback).filter(
|
|
(v): v is string => !!v?.trim(),
|
|
);
|
|
return (
|
|
names.map((v) => v.trim()).join(' · ') || 'Localização por confirmar'
|
|
);
|
|
}
|
|
|
|
export function publicSourceHref(
|
|
value: string | null | undefined,
|
|
): string | undefined {
|
|
const text = publicReference(value);
|
|
return text && /^https?:\/\//i.test(text) ? text : undefined;
|
|
}
|
|
|
|
/** Replace known editorial cross-reference codes with their published names. */
|
|
export function publicPortfolioText(
|
|
value: string,
|
|
names: Record<string, string>,
|
|
): string {
|
|
const codes = Object.keys(names)
|
|
.filter(Boolean)
|
|
.sort((a, b) => b.length - a.length);
|
|
if (!codes.length) return value;
|
|
const escaped = codes.map((code) =>
|
|
code.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'),
|
|
);
|
|
return value.replace(
|
|
new RegExp(
|
|
`(?<![\\p{L}\\p{N}_])(${escaped.join('|')})(?![\\p{L}\\p{N}_])`,
|
|
'gu',
|
|
),
|
|
(code) => names[code],
|
|
);
|
|
}
|