Files
web_hgcg_h5/tests/helpers/seo.mjs
T
谢宇宁andClaude Opus 5.5 8cdbfee08f Initial commit
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 16:23:48 +07:00

29 lines
2.0 KiB
JavaScript

import {readFile} from 'node:fs/promises';
import ts from 'typescript';
const compile=async path=>ts.transpileModule(await readFile(new URL('../../'+path,import.meta.url),'utf8'),{compilerOptions:{target:ts.ScriptTarget.ES2022,module:ts.ModuleKind.ESNext}}).outputText;
const moduleUrl=source=>'data:text/javascript;base64,'+Buffer.from(source).toString('base64');
const routingUrl=moduleUrl(await compile('src/lib/routing.ts'));
const protocolUrl=moduleUrl(await compile('src/lib/api/protocol.ts'));
const playbackUrl=moduleUrl((await compile('src/lib/client/playback.ts')).replace("'../api/protocol'",JSON.stringify(protocolUrl)));
export const structuredUrl=moduleUrl((await compile('src/lib/structured-data.ts')).replace("'./routing'",JSON.stringify(routingUrl)));
export const structuredData=await import(structuredUrl);
export const seoUrl=moduleUrl((await compile('src/lib/seo.ts')).replace("'./routing'",JSON.stringify(routingUrl)).replace("'./structured-data'",JSON.stringify(structuredUrl)).replace("'./client/playback'",JSON.stringify(playbackUrl)));
export const seo=await import(seoUrl);
export const clientSeo=await import(moduleUrl((await compile('src/lib/client/seo.ts')).replace("'../structured-data'",JSON.stringify(structuredUrl))));
export function headDocument(){
const nodes=[];
const doc={title:'',head:{
querySelectorAll(selector){
const match=selector.match(/^(meta|link|script)\[([\w-]+)(?:="([^"]+)")?\]$/);
return match?nodes.filter(node=>node.tagName===match[1]&&(match[3]===undefined?Object.hasOwn(node.attributes,match[2]):node.attributes[match[2]]===match[3])):[];
},
appendChild(node){nodes.push(node);},
},createElement(tagName){return {tagName,attributes:{},setAttribute(name,value){this.attributes[name]=value;},remove(){const i=nodes.indexOf(this);if(i>=0)nodes.splice(i,1);}};}};
doc.value=selector=>doc.head.querySelectorAll(selector)[0]?.attributes[selector.startsWith('link')?'href':'content'];
doc.querySelectorAll=selector=>doc.head.querySelectorAll(selector);
doc.nodes=nodes;
return doc;
}