export const MINDSPACE_PAGE_TAG_ATTR = 'data-mindspace-page-tag'; export const MINDSPACE_PAGE_TAG_PLATFORM_BRAND = 'platform-brand'; export const PLATFORM_BRAND_TEXT = 'TKMind · 智趣'; export const PLATFORM_BRAND_STYLE_ID = 'mindspace-platform-brand-style'; const PLATFORM_BRAND_VISIBILITY_STYLE = ``; const PLATFORM_BRAND_INNER_RE = /(?:TKMind\s*·\s*智趣|contact@tkmind\.(?:ai|cn)|📧[^<]*tkmind)/i; const SIMPLE_BRAND_ELEMENT_RE = /<(p|div|span|small|footer|a)(\s[^>]*)?>([^<]*(?:TKMind\s*·\s*智趣|contact@tkmind\.(?:ai|cn)|📧[^<]*tkmind)[^<]*)<\/\1>/gi; export function normalizePlatformDomainText(text) { return String(text ?? '').replace(/tkmind\.ai/gi, 'tkmind.cn'); } export function normalizePlatformBrandHtml(html) { let next = normalizePlatformDomainText(html); next = next.replace(/mailto:([^"'>\s]+@tkmind)\.ai/gi, 'mailto:$1.cn'); next = next.replace(/https?:\/\/([a-z0-9.-]*\.)?tkmind\.ai/gi, (match) => match.replace(/tkmind\.ai/gi, 'tkmind.cn'), ); next = next.replace(/(?:📧\s*)?contact@tkmind\.cn/gi, PLATFORM_BRAND_TEXT); return next; } export function ensurePlatformBrandPageTags(html) { const normalized = normalizePlatformBrandHtml(html); return normalized.replace(SIMPLE_BRAND_ELEMENT_RE, (match, tag, attrs, inner) => { const attrStr = attrs ?? ''; if (attrStr.includes(MINDSPACE_PAGE_TAG_ATTR)) return match; if (!PLATFORM_BRAND_INNER_RE.test(inner)) return match; const spacer = attrStr ? '' : ' '; return `<${tag}${attrStr}${spacer}${MINDSPACE_PAGE_TAG_ATTR}="${MINDSPACE_PAGE_TAG_PLATFORM_BRAND}">${inner}`; }); } export function hasPlatformBrandMarker(html) { return new RegExp( `${MINDSPACE_PAGE_TAG_ATTR}\\s*=\\s*["']${MINDSPACE_PAGE_TAG_PLATFORM_BRAND}["']`, 'i', ).test(String(html ?? '')); } /** Append the platform brand footer when authors omitted data-mindspace-page-tag. */ export function ensurePlatformBrandFooter(html) { const normalized = ensurePlatformBrandPageTags(html); if (hasPlatformBrandMarker(normalized)) return normalized; const footer = `

${PLATFORM_BRAND_TEXT}

`; if (/<\/body>/i.test(normalized)) { return normalized.replace(/<\/body>/i, `${footer}\n`); } return `${normalized}\n${footer}`; } /** Override author CSS that hides the platform brand line (e.g. opacity 0.25). */ export function injectPlatformBrandVisibilityStyle(html) { const source = String(html ?? ''); if (!hasPlatformBrandMarker(source) || source.includes(PLATFORM_BRAND_STYLE_ID)) return source; if (/<\/head>/i.test(source)) { return source.replace(/<\/head>/i, `${PLATFORM_BRAND_VISIBILITY_STYLE}\n`); } return `${PLATFORM_BRAND_VISIBILITY_STYLE}${source}`; } export function preparePublishedPlatformBrand(html) { return injectPlatformBrandVisibilityStyle(ensurePlatformBrandFooter(html)); } export function prepareHtmlPageBrandMarkers(html) { return ensurePlatformBrandPageTags(html); }