From 3fe26261ef4a420642cb3a34b090ff4631af7c69 Mon Sep 17 00:00:00 2001 From: bisnsh Date: Sun, 8 Feb 2026 10:16:27 +0800 Subject: [PATCH] Remove genkey.js and push2typesense.js from repository; add to .gitignore --- genkey.js | 27 ------------------- push2typesense.js | 69 ----------------------------------------------- 2 files changed, 96 deletions(-) delete mode 100644 genkey.js delete mode 100644 push2typesense.js diff --git a/genkey.js b/genkey.js deleted file mode 100644 index cf98a27..0000000 --- a/genkey.js +++ /dev/null @@ -1,27 +0,0 @@ -const http = require('https'); - -const data = JSON.stringify({ - "description": "Public search only key", - "actions": ["documents:search"], - "collections": ["blogs"] -}); - -const options = { - hostname: 'typesense.biss.click', // 不要带 https:// - port: 443, - path: '/keys', - method: 'POST', - headers: { - 'X-TYPESENSE-API-KEY': 'tDPfzkghH3Zy55DHyWOnYkQiijOqN8bx', - 'Content-Type': 'application/json', - 'Content-Length': data.length - } -}; - -const req = http.request(options, res => { - res.on('data', d => { process.stdout.write(d); }); -}); - -req.on('error', error => { console.error(error); }); -req.write(data); -req.end(); \ No newline at end of file diff --git a/push2typesense.js b/push2typesense.js deleted file mode 100644 index 73e4145..0000000 --- a/push2typesense.js +++ /dev/null @@ -1,69 +0,0 @@ -const Typesense = require('typesense'); -const fs = require('fs'); -const xml2js = require('xml2js'); - -// --- 配置区域 --- -const CONFIG = { - apiKey: 'tDPfzkghH3Zy55DHyWOnYkQiijOqN8bx', // 必须是 Admin Key - host: 'typesense.biss.click', - port: 443, - protocol: 'https', - collectionName: 'blogs' -}; - -const client = new Typesense.Client({ - 'nodes': [{ 'host': CONFIG.host, 'port': CONFIG.port, 'protocol': CONFIG.protocol }], - 'apiKey': CONFIG.apiKey, - 'connectionTimeoutSeconds': 5 -}); - -async function sync() { - try { - // 1. 读取并解析 XML - const xml = fs.readFileSync('./public/search.xml', 'utf8'); - const parser = new xml2js.Parser({ explicitArray: false }); - const result = await parser.parseStringPromise(xml); - - // 提取文章列表 (处理单篇文章和多篇文章的情况) - let entries = result.search.entry; - if (!Array.isArray(entries)) entries = [entries]; - - // 格式化数据以适配 Typesense - const documents = entries.map(post => ({ - title: post.title, - url: post.url, - content: post.content, - categories: post.categories ? (Array.isArray(post.categories.category) ? post.categories.category : [post.categories.category]) : [], - tags: post.tags ? (Array.isArray(post.tags.tag) ? post.tags.tag : [post.tags.tag]) : [], - })); - - // 2. 检查或创建 Collection (Schema) - try { - await client.collections(CONFIG.collectionName).retrieve(); - } catch (err) { - const schema = { - name: CONFIG.collectionName, - fields: [ - // 关键点:指定 locale 为 'zh' 开启中文分词 - { name: 'title', type: 'string', locale: 'zh' }, - { name: 'content', type: 'string', locale: 'zh' }, - { name: 'url', type: 'string' }, - { name: 'categories', type: 'string[]', facet: true }, - { name: 'tags', type: 'string[]', facet: true } - ] - }; - await client.collections().create(schema); - console.log('Collection created with Chinese support!'); - } - - // 3. 导入数据 (使用 upsert 模式:存在则更新,不存在则创建) - console.log(`Syncing ${documents.length} posts to Typesense...`); - await client.collections(CONFIG.collectionName).documents().import(documents, { action: 'upsert' }); - console.log('Sync complete!'); - - } catch (error) { - console.error('Sync failed:', error); - } -} - -sync(); \ No newline at end of file