File size: 992 Bytes
ef04721
 
9d67259
ef04721
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/**
 * 构建时向 HTML <head> 注入 inforadar-api-base meta(仅当 INFORADAR_API_BASE 已设置)。
 * GitHub Pages:`INFORADAR_API_BASE=https://infolens-api.xiaoyundqy.workers.dev npm run build`
 * HF / 本地:不设该变量 → 无 meta → 运行时 resolveApiBase() 为同源 ''。
 */

const { escapeHtmlText } = require('./injectPageMetaIntoHtml.js');

const META_NAME = 'inforadar-api-base';

/**
 * @param {string} html
 * @returns {string}
 */
function injectApiBaseMeta(html) {
    const raw = process.env.INFORADAR_API_BASE?.trim();
    if (!raw) return html;
    const content = escapeHtmlText(raw.replace(/\/+$/, ''));
    if (html.includes(`name="${META_NAME}"`)) return html;
    const tag = `<meta name="${META_NAME}" content="${content}">`;
    if (/<meta\s+charset/i.test(html)) {
        return html.replace(/(<meta\s+charset[^>]*>)/i, `$1\n    ${tag}`);
    }
    return html.replace(/<head>/i, `<head>\n    ${tag}`);
}

module.exports = { injectApiBaseMeta };