UNPKG

mcp-prd-server

Version:
73 lines (72 loc) 2.79 kB
// utils.ts import fs from "fs"; import vm from "vm"; import { projectListPath, projectVersionsPath, projectNameMap, } from "./config.js"; // 安全读取JSON文件 function safeReadJsonFile(filePath, defaultValue = []) { try { if (!fs.existsSync(filePath)) { console.warn(`文件不存在: ${filePath},使用默认值`); return defaultValue; } const content = fs.readFileSync(filePath, "utf-8"); return JSON.parse(content); } catch (error) { if (error instanceof SyntaxError) { console.error(`JSON解析失败: ${filePath}`, error); } else { console.error(`读取文件失败: ${filePath}`, error); } return defaultValue; } } export const projectList = safeReadJsonFile(projectListPath, []); export const projectVersions = safeReadJsonFile(projectVersionsPath, {}); // 检查项目是否有效 export function isValidProject(project) { return projectList.includes(project); } // 获取指定项目的所有版本号 export function getAllVersionsOfProject(project) { return projectVersions[project] || []; } // 检查版本号是否有效 export function isValidVersion(project, version) { return getAllVersionsOfProject(project).includes(version); } /** * 增强HTML内容的语义化 * @param {string} htmlStr - 原始HTML字符串 * @returns {string} 处理后的HTML字符串 */ export function enhanceHtmlSemantics(htmlStr) { Object.entries(projectNameMap).forEach(([pinyin, chinese]) => { const pinyinRegex = new RegExp(`(${pinyin})(?![^<]*>)`, "gi"); htmlStr = htmlStr.replace(pinyinRegex, `<span class=\"project-name\" data-chinese=\"${chinese}\" data-pinyin=\"$1\">$1</span>`); const chineseRegex = new RegExp(`(${chinese})(?![^<]*>)`, "gi"); htmlStr = htmlStr.replace(chineseRegex, `<span class=\"project-name\" data-chinese=\"$1\" data-pinyin=\"${pinyin}\">$1</span>`); }); return htmlStr; } /** * 精简和处理HTML字符串 * @param {string} htmlStr - 原始HTML字符串 * @returns {string} 处理后的HTML字符串 */ export function htmlReduce(htmlStr) { htmlStr = htmlStr.replace(/\s+/g, " ").trim(); htmlStr = htmlStr.replace(/<script\b[^>]*>.*?<\/script>/gi, ""); htmlStr = enhanceHtmlSemantics(htmlStr); return htmlStr; } // 假设 jsContent 是 document.js 的内容(字符串) export function getCreatorResult(jsContent) { const funcMatch = jsContent.match(/\(\s*function\s*\(\)\s*\{[\s\S]*?return _creator\(\);\s*\}\s*\)\s*\(\s*\)/); if (!funcMatch) throw new Error("未找到 function() { ... }() 结构"); const funcStr = funcMatch[0]; const script = new vm.Script(funcStr); return script.runInNewContext(); }