Files
memind/mindspace-page-tag.mjs

88 lines
3.7 KiB
JavaScript

import { injectBeforeDocumentClosingBody, injectBeforeDocumentClosingHead } from './html-document-injection.mjs';
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';
export const MINDSPACE_PAGE_FONT_STACK =
'-apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif';
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-family: ${MINDSPACE_PAGE_FONT_STACK} !important;
font-size: 12px !important;
font-weight: 500 !important;
letter-spacing: 0.02em !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 injectBeforeDocumentClosingBody(normalized, `${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 injectBeforeDocumentClosingHead(source, `${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);
}