37 lines
1.6 KiB
JavaScript
37 lines
1.6 KiB
JavaScript
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 · 智趣';
|
|
|
|
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}</${tag}>`;
|
|
});
|
|
}
|
|
|
|
export function prepareHtmlPageBrandMarkers(html) {
|
|
return ensurePlatformBrandPageTags(html);
|
|
}
|