927e16d861
Inject serve-time CSS so author styles cannot hide TKMind · 智趣 with low opacity, which made pages like weather.html look brandless. Co-authored-by: Cursor <cursoragent@cursor.com>
83 lines
3.3 KiB
JavaScript
83 lines
3.3 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 · 智趣';
|
|
export const PLATFORM_BRAND_STYLE_ID = 'mindspace-platform-brand-style';
|
|
|
|
const PLATFORM_BRAND_VISIBILITY_STYLE = `<style id="${PLATFORM_BRAND_STYLE_ID}">
|
|
[data-mindspace-page-tag="${MINDSPACE_PAGE_TAG_PLATFORM_BRAND}"] {
|
|
display: block !important;
|
|
opacity: 1 !important;
|
|
visibility: visible !important;
|
|
color: #9aa3a0 !important;
|
|
font-size: 12px !important;
|
|
font-weight: 600 !important;
|
|
letter-spacing: 0.08em !important;
|
|
text-shadow: 0 1px 3px rgba(0, 0, 0, 0.35);
|
|
}
|
|
</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}</${tag}>`;
|
|
});
|
|
}
|
|
|
|
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 = `<p ${MINDSPACE_PAGE_TAG_ATTR}="${MINDSPACE_PAGE_TAG_PLATFORM_BRAND}">${PLATFORM_BRAND_TEXT}</p>`;
|
|
if (/<\/body>/i.test(normalized)) {
|
|
return normalized.replace(/<\/body>/i, `${footer}\n</body>`);
|
|
}
|
|
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</head>`);
|
|
}
|
|
return `${PLATFORM_BRAND_VISIBILITY_STYLE}${source}`;
|
|
}
|
|
|
|
export function preparePublishedPlatformBrand(html) {
|
|
return injectPlatformBrandVisibilityStyle(ensurePlatformBrandFooter(html));
|
|
}
|
|
|
|
export function prepareHtmlPageBrandMarkers(html) {
|
|
return ensurePlatformBrandPageTags(html);
|
|
}
|