init: TriliumNext × WechatSync 多平台发布桥接
- bridge/ 常驻桥接服务器(WebSocket 9600 + HTTP 9601,平台状态缓存) - patch/ TriliumNext 补丁(publisher 服务、API 路由、发布控制台前端) - install.sh 一键安装 + Docker 容器 CLI 处理 - 全链路使用 host.docker.internal 解决容器访问宿主机 - mcp/ AI Agent 集成说明
This commit is contained in:
commit
b5acbed047
11 files changed
+1266
No files matched your search
@@ -0,0 +1,62 @@
|
||||
# TriliumNext WechatSync Publisher Patch
|
||||
# 将补丁应用到 TriliumNext 源码,并处理 Docker 网络差异
|
||||
#
|
||||
# 用法: bash patch.sh /path/to/triliumnext
|
||||
set -e
|
||||
|
||||
TRILIUM_DIR="${1:-/path/to/trilium}"
|
||||
HERE="$(cd "$(dirname "$0")" && pwd)"
|
||||
echo "Applying patches to $TRILIUM_DIR..."
|
||||
|
||||
# --- 源码补丁 ---
|
||||
copy_patch() {
|
||||
local src="$1" dst="$2" label="$3"
|
||||
if [ -f "$src" ]; then
|
||||
cp "$src" "$dst" && echo " ✅ $label"
|
||||
else
|
||||
echo " ⚠️ 缺失: $src"
|
||||
fi
|
||||
}
|
||||
copy_patch "$HERE/wechatsync.ts" "$TRILIUM_DIR/apps/server/src/services/publisher/wechatsync.ts" "wechatsync.ts"
|
||||
copy_patch "$HERE/publisher_frontend.ts" "$TRILIUM_DIR/apps/server/src/routes/api/publisher_frontend.ts" "publisher_frontend.ts"
|
||||
copy_patch "$HERE/publish.html" "$TRILIUM_DIR/apps/client/public/publish.html" "publish.html"
|
||||
|
||||
# --- 构建 ---
|
||||
echo ""
|
||||
echo "Rebuilding TriliumNext server..."
|
||||
cd "$TRILIUM_DIR" && pnpm server:build
|
||||
|
||||
# --- Docker 运行时修正 ---
|
||||
# 若 Trilium 在容器内运行,源码里的 host.docker.internal 已生效;
|
||||
# 但若你直接用镜像(未从源码构建),需对已编译的 main.cjs 打补丁:
|
||||
if [ -f "/.dockerenv" ] || grep -qa docker /proc/1/cgroup 2>/dev/null; then
|
||||
echo ""
|
||||
echo "🐳 Docker 环境:修正已编译产物桥接地址..."
|
||||
MC="$TRILIUM_DIR/apps/server/dist/main.cjs"
|
||||
if [ -f "$MC" ]; then
|
||||
sed -i 's|http://localhost:9601|http://host.docker.internal:9601|g' "$MC" 2>/dev/null || true
|
||||
sed -i 's|http://localhost:${Hca}/status|http://host.docker.internal:${Hca}/status|g' "$MC" 2>/dev/null || true
|
||||
sed -i 's|SYNC_WS_PORT:"9600"|SYNC_WS_PORT:"9600",SYNC_WS_HOST:"host.docker.internal"|g' "$MC" 2>/dev/null || true
|
||||
echo " ✅ main.cjs 桥接地址已修正"
|
||||
fi
|
||||
# 在容器内安装 wechatsync CLI 并修正其 hostname
|
||||
if ! command -v wechatsync &>/dev/null; then
|
||||
echo " 📦 安装 wechatsync CLI..."
|
||||
npm install -g @wechatsync/cli 2>/dev/null && echo " ✅ CLI installed" || echo " ⚠️ CLI 安装失败"
|
||||
fi
|
||||
CLI_JS=$(find /usr/local/lib/node_modules/@wechatsync/cli/dist -name "index.js" 2>/dev/null | head -1)
|
||||
if [ -n "$CLI_JS" ]; then
|
||||
sed -i 's/hostname: "localhost"/hostname: process.env.SYNC_WS_HOST || "host.docker.internal"/g' "$CLI_JS" 2>/dev/null \
|
||||
&& echo " ✅ CLI hostname 已修正" || true
|
||||
fi
|
||||
|
||||
# 把前端页面拷入已编译产物目录(镜像内路径可能不同)
|
||||
for dst in "$TRILIUM_DIR/apps/server/dist/public/publish.html" /usr/src/app/dist/public/publish.html; do
|
||||
[ -d "$(dirname "$dst")" ] && cp "$HERE/publish.html" "$dst" 2>/dev/null && echo " ✅ 页面已更新: $dst" || true
|
||||
done
|
||||
fi
|
||||
|
||||
echo ""
|
||||
echo "✅ 补丁完成!重启 TriliumNext 生效:"
|
||||
echo " systemctl restart triliumnext # 裸机"
|
||||
echo " docker restart triliumnext # Docker"
|
||||
@@ -0,0 +1,297 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
<head><meta charset="utf-8"><title>📤 发布控制台</title>
|
||||
<style>
|
||||
*{margin:0;padding:0;box-sizing:border-box}
|
||||
body{font-family:-apple-system,PingFang SC,Microsoft YaHei,sans-serif;background:#f3f4f6;color:#111;min-height:100vh}
|
||||
.header{background:#fff;border-bottom:1px solid #e5e7eb;padding:16px 32px;display:flex;align-items:center;flex-wrap:wrap;gap:8px}
|
||||
.header h1{font-size:20px;font-weight:700;flex:1;min-width:120px}
|
||||
.bridge-badge{font-size:12px}
|
||||
.container{max-width:800px;margin:24px auto;padding:0 20px 60px}
|
||||
.phase{display:flex;margin-bottom:16px;background:#fff;border-radius:12px;overflow:hidden;border:1px solid #e5e7eb}
|
||||
.phase-step{flex:1;padding:12px;text-align:center;font-size:13px;font-weight:500;color:#9ca3af;border-right:1px solid #e5e7eb}
|
||||
.phase-step:last-child{border-right:none}
|
||||
.phase-step.active{color:#22c55e;background:#f0fdf4;font-weight:700}
|
||||
.phase-step.done{color:#6b7280;background:#f9fafb}
|
||||
.section{background:#fff;border:1px solid #e5e7eb;border-radius:12px;margin-bottom:12px;overflow:hidden}
|
||||
.section-title{font-size:13px;font-weight:600;padding:12px 16px;border-bottom:1px solid #e5e7eb;display:flex;align-items:center;gap:8px}
|
||||
.section-title .sub{font-size:11px;font-weight:400;color:#9ca3af}
|
||||
.load-area{padding:12px 16px;background:#f9fafb;display:flex;gap:8px;flex-wrap:wrap;border-bottom:1px solid #e5e7eb}
|
||||
.load-area input{flex:1;min-width:120px;padding:8px 12px;border:1px solid #d1d5db;border-radius:8px;font-size:13px}
|
||||
.load-area button{padding:8px 16px;background:#3b82f6;color:#fff;border:none;border-radius:8px;font-size:13px;font-weight:600;cursor:pointer}
|
||||
.load-area .hint{font-size:12px;color:#6b7280;width:100%}
|
||||
.item{display:flex;align-items:center;padding:8px 16px;gap:10px;border-bottom:1px solid #f3f4f6;cursor:pointer;transition:background .1s}
|
||||
.item:hover{background:#f9fafb}
|
||||
.item input[type=checkbox]{width:15px;height:15px;cursor:pointer;flex-shrink:0}
|
||||
.item .name{font-size:13px;font-weight:500;flex:1;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
|
||||
.item .status{font-size:11px;flex-shrink:0}
|
||||
.item.disabled{opacity:.4;cursor:not-allowed;pointer-events:none}
|
||||
.item.disabled input{cursor:not-allowed}
|
||||
.actions{display:flex;gap:8px;padding:12px 16px}
|
||||
.btn{padding:10px 20px;border:1px solid #d1d5db;border-radius:8px;font-size:13px;font-weight:600;cursor:pointer;background:#fff;color:#111;flex:1;text-align:center;transition:background .15s}
|
||||
.btn-primary{background:#22c55e;color:#fff;border-color:#22c55e}
|
||||
.btn-primary:hover{background:#16a34a}
|
||||
.btn:disabled{opacity:.4;cursor:not-allowed}
|
||||
.select-bar{display:flex;align-items:center;gap:10px;padding:8px 16px;border-bottom:1px solid #f3f4f6;background:#fafafa;font-size:12px}
|
||||
.select-bar .spacer{flex:1}
|
||||
.grp{border:1px solid #e5e7eb;border-radius:8px;margin:6px 12px}
|
||||
.grp-title{font-size:12px;font-weight:600;padding:6px 12px;background:#f9fafb;border-bottom:1px solid #e5e7eb}
|
||||
.grp .item{padding:5px 12px 5px 24px;font-size:12px}
|
||||
.empty{text-align:center;padding:40px 20px;color:#9ca3af;font-size:13px}
|
||||
.tip{font-size:11px;color:#9ca3af;text-align:center;padding:6px 16px 10px}
|
||||
</style></head><body>
|
||||
<div class="header">
|
||||
<h1>📤 发布控制台</h1>
|
||||
<span id="bridgeStatus" class="bridge-badge">加载中...</span>
|
||||
</div>
|
||||
<div class="container">
|
||||
<div class="phase">
|
||||
<div class="phase-step active" id="ph1">📝 选文章</div>
|
||||
<div class="phase-step" id="ph2">🌐 选平台</div>
|
||||
<div class="phase-step" id="ph3">🚀 结果</div>
|
||||
</div>
|
||||
<div class="section" id="secNotes">
|
||||
<div class="load-area">
|
||||
<span class="hint">输入目录笔记ID(从URL复制):</span>
|
||||
<input type="text" id="parentInput" placeholder="如: PuLnqqL23KQo" />
|
||||
<button id="loadBtn">加载</button>
|
||||
</div>
|
||||
<div id="notesList"><div class="empty">输入目录ID后点加载</div></div>
|
||||
<div id="nextStepDiv" style="display:none;padding:12px 16px;border-top:1px solid #e5e7eb">
|
||||
<button class="btn btn-primary" id="nextBtn">下一步:选择平台 →</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="section" id="secPlats" style="display:none">
|
||||
<div class="section-title">🌐 选择发布平台 <span class="sub" id="authSummary"></span></div>
|
||||
<div id="platsList"></div>
|
||||
<div class="tip">❌ 灰底 = 扩展中未登录 · 点击扩展图标扫码登录</div>
|
||||
<div class="actions">
|
||||
<button class="btn" id="backBtn">← 返回</button>
|
||||
<button class="btn btn-primary" id="pubBtn">🚀 发布</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="section" id="secRes" style="display:none">
|
||||
<div class="section-title">🚀 发布结果</div>
|
||||
<div id="resList"></div>
|
||||
<div class="actions"><button class="btn" id="doneBtn">← 完成</button></div>
|
||||
</div>
|
||||
</div>
|
||||
<script>
|
||||
var notes=[],platforms=[],T=new Map(),SN=new Set(),SP=new Set(),results=new Map(),busy=false,
|
||||
API=window.location.origin,authMap=new Map(),extCached=false,extCachedAt=null;
|
||||
function $(id){return document.getElementById(id)}
|
||||
|
||||
async function init(){
|
||||
try{
|
||||
// 先检查 Trilium 登录
|
||||
var r1=await fetch(API+"/api/publisher/platforms?_t="+Date.now(),{credentials:"include"});
|
||||
if(r1.status===401){$("bridgeStatus").innerHTML='<span style="color:#ef4444">🔒 请先登录Trilium</span>';return}
|
||||
var d=await r1.json();
|
||||
if(!d||!d.platforms){$("bridgeStatus").textContent="⚠️ 无平台配置";return}
|
||||
|
||||
platforms=d.platforms;
|
||||
for(var p of platforms)SP.add(p.id);
|
||||
|
||||
// 获取扩展登录状态(单独 catch 不阻断主流程)
|
||||
try{
|
||||
var r2=await fetch(API+"/api/publisher/platform-status?_t="+Date.now(),{credentials:"include"});
|
||||
var ext=await r2.json();
|
||||
if(ext&&ext.platforms){
|
||||
for(var p of ext.platforms) authMap.set(p.id,p);
|
||||
extCached=ext.cached===true;
|
||||
extCachedAt=ext.cachedAt||null;
|
||||
renderBridgeStatus(ext);
|
||||
}else{
|
||||
$("bridgeStatus").innerHTML='<span style="color:#f59e0b">● 扩展离线</span>';
|
||||
}
|
||||
}catch(e){
|
||||
$("bridgeStatus").innerHTML='<span style="color:#f59e0b">● 扩展未连接</span>';
|
||||
}
|
||||
|
||||
renderPlats();
|
||||
|
||||
// 绑定事件
|
||||
$("loadBtn").onclick=loadChildren;
|
||||
$("parentInput").onkeydown=function(e){if(e.key==="Enter")loadChildren()};
|
||||
$("nextBtn").onclick=function(){goPhase(2)};
|
||||
$("backBtn").onclick=function(){goPhase(1)};
|
||||
$("pubBtn").onclick=handlePublish;
|
||||
$("doneBtn").onclick=function(){location.reload()};
|
||||
}catch(e){$("bridgeStatus").textContent="⚠️ 加载失败: "+e.message}
|
||||
}
|
||||
|
||||
function renderBridgeStatus(ext){
|
||||
if(!ext||!ext.platforms){
|
||||
$("bridgeStatus").innerHTML='<span style="color:#ef4444;cursor:pointer" onclick="showBridgeHelp()">● 桥接离线</span>';
|
||||
return;
|
||||
}
|
||||
var logged=ext.platforms.filter(function(p){return p.isAuthenticated}).length;
|
||||
var total=ext.platforms.length;
|
||||
var cachedBadge=extCached?' <span style="font-size:11px;color:#f59e0b;cursor:help" title="扩展离线,显示的是上次缓存数据('+new Date(extCachedAt).toLocaleTimeString()+')">📦 缓存</span>':'';
|
||||
$("bridgeStatus").innerHTML='<span style="color:#22c55e">●</span> 已登录 '+logged+'/'+total+' 平台'+cachedBadge;
|
||||
var names=ext.platforms.filter(function(p){return p.isAuthenticated}).map(function(p){return p.name+(p.username?'('+p.username+')':'')}).join('、')||'(无)';
|
||||
$("bridgeStatus").title='已登录平台:'+names+(extCached?' (缓存数据)':'');
|
||||
}
|
||||
|
||||
function showBridgeHelp(){
|
||||
if(confirm("同步助手扩展未连接。\n\n1. Chrome 打开 WechatSync 扩展\n2. 设置服务器地址为 ws://192.168.68.19:9600\n3. 输入 Token(扩展设置页显示)\n4. 扫码登录各平台\n\n确定后打开帮助页面?")){
|
||||
window.open("https://github.com/wechatsync/WechatSync","_blank")
|
||||
}
|
||||
}
|
||||
|
||||
async function loadChildren(){
|
||||
var id=$("parentInput").value.trim();
|
||||
if(!id)return;
|
||||
try{
|
||||
var r=await fetch(API+"/api/publisher/notes-with-children/"+id,{credentials:"include"});
|
||||
if(r.status===401){alert("Trilium 登录已过期,请刷新页面重新登录");return}
|
||||
var d=await r.json();
|
||||
if(d&&d.notes){
|
||||
for(var n of d.notes){if(!T.has(n.noteId)){T.set(n.noteId,n.title);SN.add(n.noteId)}}
|
||||
notes=[];T.forEach(function(t,id){notes.push({noteId:id,title:t})});
|
||||
renderNotes();renderNextBtn();
|
||||
}else{
|
||||
alert("该目录没有子笔记");
|
||||
}
|
||||
}catch(e){alert("加载失败: "+e.message)}
|
||||
}
|
||||
|
||||
function renderNextBtn(){var el=$("nextStepDiv");el.style.display=(notes.length>0&&SN.size>0)?"block":"none"}
|
||||
|
||||
function renderNotes(){
|
||||
var el=$("notesList");
|
||||
if(!notes.length){el.innerHTML='<div class="empty">无文章,输入其他目录ID试试</div>';return}
|
||||
var ac=(SN.size===notes.length);
|
||||
var h='<div class="select-bar"><input type="checkbox" id="ckAllN"'+(ac?" checked":"")+'><label for="ckAllN" style="cursor:pointer">'+(ac?"取消全选":"全选")+'</label><span>'+SN.size+"/"+notes.length+"篇</span></div>";
|
||||
for(var n of notes){
|
||||
var ck=SN.has(n.noteId)?" checked":"";
|
||||
h+='<div class="item"><input type="checkbox" class="nc" value="'+n.noteId+'"'+ck+'><span class="name">'+n.title+'</span></div>';
|
||||
}
|
||||
el.innerHTML=h;
|
||||
$("ckAllN").onchange=function(){if(this.checked){for(var n of notes)SN.add(n.noteId)}else SN.clear();renderNotes()};
|
||||
el.querySelectorAll(".item").forEach(function(e){
|
||||
e.onclick=function(ev){
|
||||
if(ev.target.tagName==='INPUT')return;
|
||||
var c=e.querySelector(".nc");c.checked=!c.checked;
|
||||
if(c.checked)SN.add(c.value);else SN.delete(c.value);renderNotes()
|
||||
}
|
||||
});
|
||||
renderNextBtn();
|
||||
}
|
||||
|
||||
function renderPlats(){
|
||||
if(!platforms.length){$("platsList").innerHTML='<div class="empty">无可用平台</div>';return}
|
||||
var authCount=0,totalCount=platforms.length;
|
||||
for(var p of platforms){if(getAuth(p))authCount++}
|
||||
$("authSummary").textContent="(已登录 "+authCount+"/"+totalCount+",仅可选已登录平台)";
|
||||
|
||||
// 获取过滤状态(checkbox可能尚未渲染,默认显示所有)
|
||||
var faEl=document.getElementById("filterAuth");
|
||||
var filterHidden=faEl&&faEl.checked;
|
||||
|
||||
var h='<div class="select-bar"><span>仅显示已登录 <label><input type="checkbox" id="filterAuth" checked> 隐藏未登录</label></span><span class="spacer"></span><span id="platCount">'+SP.size+"/"+totalCount+"个</span></div>";
|
||||
for(var p of platforms){
|
||||
var auth=getAuth(p);
|
||||
var statusHtml=getStatusHtml(p);
|
||||
var isDisabled=(!auth);
|
||||
var hidden=(filterHidden&&isDisabled)?" style='display:none'":"";
|
||||
var ck=(auth&&SP.has(p.id))?" checked":"";
|
||||
if(isDisabled&&SP.has(p.id))SP.delete(p.id);
|
||||
h+='<div class="item'+(isDisabled?" disabled":"")+'"'+hidden+' data-pid="'+p.id+'"><input type="checkbox" class="pc" value="'+p.id+'"'+ck+(isDisabled?" disabled":"")+'><span class="name">'+p.name+'</span><span class="status">'+statusHtml+'</span></div>';
|
||||
}
|
||||
var el=$("platsList");el.innerHTML=h;
|
||||
updateBtn();
|
||||
|
||||
// 单个点击
|
||||
el.querySelectorAll(".item").forEach(function(e){
|
||||
e.onclick=function(ev){
|
||||
if(ev.target.tagName==='INPUT')return;
|
||||
var c=e.querySelector(".pc");
|
||||
if(!c||c.disabled)return;
|
||||
c.checked=!c.checked;
|
||||
if(c.checked)SP.add(c.value);else SP.delete(c.value);renderPlats()
|
||||
}
|
||||
});
|
||||
// 过滤切换
|
||||
var fa=document.getElementById("filterAuth");
|
||||
if(fa)fa.onchange=function(){renderPlats()};
|
||||
}
|
||||
|
||||
function getAuth(p){
|
||||
var pid=p.config&&p.config.platforms;
|
||||
if(!pid)return true;
|
||||
var ids=pid.split(",");
|
||||
for(var i=0;i<ids.length;i++){
|
||||
var ep=authMap.get(ids[i].trim());
|
||||
if(ep&&ep.isAuthenticated)return true;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function getStatusHtml(p){
|
||||
var pid=p.config&&p.config.platforms;
|
||||
if(!pid)return '<span style="color:#22c55e">✅</span>';
|
||||
var ids=pid.split(",");
|
||||
for(var i=0;i<ids.length;i++){
|
||||
var ep=authMap.get(ids[i].trim());
|
||||
if(ep){
|
||||
if(ep.isAuthenticated)return '<span style="color:#22c55e">✅ '+escHtml(ep.username||'已登录')+'</span>';
|
||||
return '<span style="color:#ef4444">❌ 未登录</span>';
|
||||
}
|
||||
}
|
||||
return '<span style="color:#9ca3af;font-style:italic">❓ 未知</span>';
|
||||
}
|
||||
|
||||
function escHtml(s){return String(s).replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>')}
|
||||
|
||||
function updateBtn(){var b=$("pubBtn");b.textContent="🚀 发布 "+SN.size+" 篇到 "+SP.size+" 个平台";b.disabled=!SN.size||!SP.size}
|
||||
|
||||
function goPhase(p){
|
||||
$("ph1").className="phase-step"+(p>=1?(p==1?" active":" done"):"");
|
||||
$("ph2").className="phase-step"+(p>=2?(p==2?" active":" done"):"");
|
||||
$("ph3").className="phase-step"+(p>=3?(p==3?" active":" done"):"");
|
||||
$("secNotes").style.display=p==1?"":"none";$("secPlats").style.display=p==2?"":"none";$("secRes").style.display=p==3?"":"none";
|
||||
if(p==2)updateBtn()
|
||||
}
|
||||
|
||||
async function handlePublish(){
|
||||
if(busy)return;busy=true;
|
||||
var nids=[],pids=[];SN.forEach(function(v){nids.push(v)});SP.forEach(function(v){pids.push(v)});
|
||||
results=new Map();goPhase(3);
|
||||
for(var ni=0;ni<nids.length;ni++){
|
||||
var nid=nids[ni],nr=new Map();results.set(nid,nr);
|
||||
for(var pi=0;pi<pids.length;pi++)nr.set(pids[pi],{status:"publishing"});
|
||||
rr();
|
||||
try{
|
||||
var r=await fetch(API+"/api/publisher/publish",{method:"POST",headers:{"Content-Type":"application/json"},credentials:"include",body:JSON.stringify({noteId:nid,platforms:pids})});
|
||||
if(r.status===401){alert("登录过期,刷新页面重新登录");busy=false;return}
|
||||
var rd=await r.json();
|
||||
if(rd&&rd.results){for(var ri=0;ri<rd.results.length;ri++){var r2=rd.results[ri];nr.set(r2.id||r2.platform,r2)}}
|
||||
}catch(e){for(var pi=0;pi<pids.length;pi++){var r3=nr.get(pids[pi]);if(r3&&r3.status==="publishing")nr.set(pids[pi],{status:"failed",errorMessage:e.message})}}
|
||||
results.set(nid,nr);rr();
|
||||
}
|
||||
busy=false
|
||||
}
|
||||
|
||||
function rr(){
|
||||
var el=$("resList"),h=[];
|
||||
results.forEach(function(nr,nid){
|
||||
var t=T.get(nid)||nid;
|
||||
h.push('<div class="grp"><div class="grp-title">'+t+"</div>");
|
||||
nr.forEach(function(r,pid){
|
||||
var nm=pid;for(var p of platforms){if(p.id===pid){nm=p.name;break}}
|
||||
var rh="";
|
||||
if(r.status==="success"&&r.url)rh='<a href="'+r.url+'" target="_blank">✏️ 编辑草稿</a>';
|
||||
else if(r.status==="success")rh="✅ 成功";
|
||||
else if(r.status==="failed")rh="❌ "+(r.errorMessage?r.errorMessage.substring(0,60):"失败");
|
||||
else if(r.status==="publishing")rh="⏳ 发布中…";
|
||||
else rh="⏳";
|
||||
h.push('<div class="item"><span class="name">'+nm+'</span><span class="status">'+rh+"</span></div>")
|
||||
});
|
||||
h.push("</div>")
|
||||
});
|
||||
el.innerHTML=h.length?h.join(""):'<div class="empty">无结果</div>'
|
||||
}
|
||||
init();
|
||||
</script>
|
||||
</body></html>
|
||||
@@ -0,0 +1,94 @@
|
||||
// TriliumNext WechatSync Publisher — API routes
|
||||
// 补丁目标: apps/server/src/routes/api/publisher_frontend.ts
|
||||
//
|
||||
// 提供:
|
||||
// GET /api/publisher/registered 已注册发布器
|
||||
// GET /api/publisher/platforms 已配置平台列表
|
||||
// GET /api/publisher/status 桥接/扩展连接状态
|
||||
// GET /api/publisher/platform-status 各平台登录状态(含缓存)
|
||||
// POST /api/publisher/platforms 保存平台配置
|
||||
// GET /api/publisher/notes-with-children/:id 列出目录下笔记
|
||||
// POST /api/publisher/publish 发布文章到平台
|
||||
|
||||
import { becca, options as optionService } from "@triliumnext/core";
|
||||
import type { Request, Response } from "express";
|
||||
import auth from "../../services/auth.js";
|
||||
import { apiResultHandler } from "../route_api.js";
|
||||
import { getRegisteredPublishers, parsePlatformConfigs, extractArticleContent, publishToPlatforms } from "../../services/publisher/core.js";
|
||||
import { WechatSyncPublisher } from "../../services/publisher/wechatsync.js";
|
||||
import type { PlatformConfig } from "../../services/publisher/types.js";
|
||||
|
||||
const OPT = "publisherConfigs";
|
||||
const BRIDGE_HOST = process.env.BRIDGE_HOST || "host.docker.internal";
|
||||
const BRIDGE_HTTP_PORT = parseInt(process.env.SYNC_WS_PORT || "9600", 10) + 1;
|
||||
const BRIDGE_URL = `http://${BRIDGE_HOST}:${BRIDGE_HTTP_PORT}`;
|
||||
|
||||
export function registerPublisherApi(
|
||||
apiRoute: (m: string, p: string, h: (req: Request, res: Response) => any) => void,
|
||||
asyncApiRoute: (m: string, p: string, h: (req: Request, res: Response) => Promise<any>) => void,
|
||||
asyncRoute: (m: string, p: string, mw: any[], h: (req: Request, res: Response) => Promise<any>, rh: any) => void
|
||||
) {
|
||||
apiRoute("get", "/api/publisher/registered", () => ({ publishers: getRegisteredPublishers() }));
|
||||
apiRoute("get", "/api/publisher/platforms", () => ({ platforms: parsePlatformConfigs(optionService.getOptionOrNull(OPT) || undefined) }));
|
||||
|
||||
asyncApiRoute("get", "/api/publisher/status", async () => ({
|
||||
bridge: await WechatSyncPublisher.checkBridge(),
|
||||
wechatsync: { installed: true }
|
||||
}));
|
||||
|
||||
// 平台登录状态(从桥接扩展获取,带缓存支持)
|
||||
asyncApiRoute("get", "/api/publisher/platform-status", async () => {
|
||||
try {
|
||||
const resp = await fetch(`${BRIDGE_URL}/platforms`);
|
||||
const data = await resp.json() as any;
|
||||
return {
|
||||
platforms: data.platforms || [],
|
||||
error: data.error || null,
|
||||
cached: data.cached === true,
|
||||
cachedAt: data.cachedAt || null
|
||||
};
|
||||
} catch (e: any) {
|
||||
return { platforms: [], error: e?.message || "Bridge not reachable", cached: false };
|
||||
}
|
||||
});
|
||||
|
||||
asyncRoute("post", "/api/publisher/platforms", [auth.checkApiAuth], async (req) => {
|
||||
const { platforms } = req.body as { platforms: PlatformConfig[] };
|
||||
if (!Array.isArray(platforms)) return [400, { error: "platforms must be an array" }] as any;
|
||||
optionService.setOption(OPT, JSON.stringify(platforms));
|
||||
return { success: true, count: platforms.length };
|
||||
}, apiResultHandler);
|
||||
|
||||
apiRoute("get", "/api/publisher/notes-with-children/:noteId", (req) => {
|
||||
const { noteId } = req.params;
|
||||
const note = becca.getNote(noteId);
|
||||
if (!note) return [404, { error: "Note not found" }] as any;
|
||||
const results: { noteId: string; title: string }[] = [];
|
||||
function walk(n: any) { results.push({ noteId: n.noteId, title: n.title }); for (const child of n.getChildNotes()) walk(child); }
|
||||
walk(note);
|
||||
return { notes: results };
|
||||
});
|
||||
|
||||
asyncRoute("post", "/api/publisher/publish", [auth.checkApiAuth], async (req) => {
|
||||
const { noteId, platforms: targetPlatforms } = req.body as { noteId: string; platforms?: string[] };
|
||||
if (!noteId) return [400, { error: "noteId is required" }] as any;
|
||||
const note = becca.getNote(noteId);
|
||||
if (!note) return [404, { error: "Note not found" }] as any;
|
||||
const html = note.getContent() || "";
|
||||
const article = extractArticleContent(noteId, note.title, html.toString());
|
||||
const configs = parsePlatformConfigs(optionService.getOptionOrNull(OPT) || undefined);
|
||||
const matched = (targetPlatforms && targetPlatforms.length > 0)
|
||||
? configs.filter(c => targetPlatforms.includes(c.platform) || targetPlatforms.includes(c.id))
|
||||
: configs;
|
||||
if (matched.length === 0) return [400, { error: "没有匹配的发布平台" }] as any;
|
||||
const results = await publishToPlatforms(article, matched);
|
||||
const mapped = results.map((r, i) => ({ ...r, id: matched[i]?.id || r.platform }));
|
||||
return {
|
||||
success: results.some(r => r.status === "success"),
|
||||
total: results.length,
|
||||
succeeded: results.filter(r => r.status === "success").length,
|
||||
failed: results.filter(r => r.status === "failed").length,
|
||||
results: mapped
|
||||
};
|
||||
}, apiResultHandler);
|
||||
}
|
||||
@@ -0,0 +1,181 @@
|
||||
// TriliumNext WechatSync Publisher
|
||||
// 补丁目标: apps/server/src/services/publisher/wechatsync.ts
|
||||
//
|
||||
// 说明:
|
||||
// - checkBridge() / publish() 通过桥接 HTTP API(默认 9601)与 Chrome 扩展通信
|
||||
// - 若 Trilium 运行在 Docker 容器内,桥接在宿主机上,必须用 host.docker.internal
|
||||
// 而非 localhost(容器内 localhost 指向容器自身)
|
||||
// - 桥接服务由 bridge/wechatsync-server.mjs 提供(PRIMARY 模式常驻)
|
||||
//
|
||||
// 环境变量:
|
||||
// BRIDGE_HOST - 桥接主机(默认 host.docker.internal;宿主机裸跑可设 localhost)
|
||||
// SYNC_WS_PORT - WebSocket 端口(默认 9600),HTTP API = 端口+1
|
||||
|
||||
import { execSync } from "child_process";
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
import os from "os";
|
||||
import { getLog } from "@triliumnext/core";
|
||||
import type { Publisher, ArticleContent, PlatformConfig, PublishResult } from "./types.js";
|
||||
|
||||
const BRIDGE_HOST = process.env.BRIDGE_HOST || "host.docker.internal";
|
||||
const BRIDGE_HTTP_PORT = parseInt(process.env.SYNC_WS_PORT || "9600", 10) + 1;
|
||||
const BRIDGE_URL = `http://${BRIDGE_HOST}:${BRIDGE_HTTP_PORT}`;
|
||||
|
||||
export class WechatSyncPublisher implements Publisher {
|
||||
id: string = "wechatsync";
|
||||
name: string = "WechatSync 文章同步助手";
|
||||
icon: string = "bxl-wechat";
|
||||
|
||||
async validateConfig(config: PlatformConfig) {
|
||||
const outDir = config.config.outputDir || path.join(os.homedir(), "wechatsync", "articles");
|
||||
try {
|
||||
fs.mkdirSync(outDir, { recursive: true });
|
||||
return { valid: true, message: "目录可写: " + outDir };
|
||||
} catch (e: any) {
|
||||
return { valid: false, message: e.message };
|
||||
}
|
||||
}
|
||||
|
||||
/** 检查桥接服务器与 Chrome 扩展连接状态 */
|
||||
static async checkBridge() {
|
||||
try {
|
||||
const resp = await fetch(`${BRIDGE_URL}/status`);
|
||||
if (!resp.ok) {
|
||||
return { online: false, extensionConnected: false, error: `HTTP ${resp.status}` };
|
||||
}
|
||||
const data = await resp.json();
|
||||
return { online: true, extensionConnected: data.connected === true, error: undefined };
|
||||
} catch (e: any) {
|
||||
return { online: false, extensionConnected: false, error: e.message || "bridge unreachable" };
|
||||
}
|
||||
}
|
||||
|
||||
async publish(article: ArticleContent, config: PlatformConfig): Promise<PublishResult> {
|
||||
const outDir = config.config.outputDir || path.join(os.homedir(), "wechatsync", "articles");
|
||||
const log = getLog();
|
||||
|
||||
try {
|
||||
fs.mkdirSync(outDir, { recursive: true });
|
||||
const markdown = this.toMarkdown(article);
|
||||
const safeTitle = article.title.replace(/[<>:"/\\|?*]/g, "_").substring(0, 80);
|
||||
const filePath = path.join(outDir, safeTitle + "-" + Date.now() + ".md");
|
||||
fs.writeFileSync(filePath, markdown, "utf-8");
|
||||
log.info("WechatSync: 导出 → " + filePath);
|
||||
|
||||
const maxRetries = 5;
|
||||
let draftUrl = "";
|
||||
let lastErr = "";
|
||||
|
||||
for (let attempt = 0; attempt < maxRetries && !draftUrl; attempt++) {
|
||||
const bridge = await WechatSyncPublisher.checkBridge();
|
||||
|
||||
if (!bridge.online) {
|
||||
return {
|
||||
platform: this.id, platformName: config.name, status: "failed",
|
||||
errorMessage: `桥接 HTTP API 不在线(${BRIDGE_HOST}:${BRIDGE_HTTP_PORT})`,
|
||||
publishedAt: new Date().toISOString()
|
||||
};
|
||||
}
|
||||
|
||||
if (!bridge.extensionConnected) {
|
||||
if (attempt < 2) {
|
||||
log.info(`WechatSync: 等待扩展连接... (${attempt + 1}/${maxRetries})`);
|
||||
await new Promise(r => setTimeout(r, 3000));
|
||||
continue;
|
||||
}
|
||||
return {
|
||||
platform: this.id, platformName: config.name, status: "failed",
|
||||
errorMessage: `Chrome 扩展未连接桥接(等了 ${attempt * 3}s 仍未连上)`,
|
||||
publishedAt: new Date().toISOString()
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
const platArg = config.config.platforms ? "--platforms " + config.config.platforms : "";
|
||||
const cmd = 'wechatsync sync "' + filePath + '" ' + platArg;
|
||||
const out = execSync(cmd, {
|
||||
timeout: 30000,
|
||||
stdio: ["pipe", "pipe", "pipe"],
|
||||
env: {
|
||||
...process.env,
|
||||
SYNC_WS_PORT: String(BRIDGE_HTTP_PORT - 1),
|
||||
SYNC_WS_HOST: BRIDGE_HOST
|
||||
}
|
||||
}).toString();
|
||||
draftUrl = this.extractDraftUrl(out);
|
||||
log.info("CLI输出: " + out.substring(0, 500));
|
||||
} catch (cliErr: any) {
|
||||
const combined = (cliErr.stdout || "") + "\n" + (cliErr.stderr || "");
|
||||
draftUrl = this.extractDraftUrl(combined);
|
||||
lastErr = combined.substring(0, 200);
|
||||
log.info("CLI(含stderr): " + combined.substring(0, 500));
|
||||
}
|
||||
|
||||
if (!draftUrl && attempt < maxRetries - 1) {
|
||||
log.info(`WechatSync: 未检测到 URL,重试 (${attempt + 1}/${maxRetries})...`);
|
||||
await new Promise(r => setTimeout(r, 3000));
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
platform: this.id, platformName: config.name,
|
||||
status: draftUrl ? "success" : "failed",
|
||||
url: draftUrl || undefined,
|
||||
errorMessage: draftUrl ? undefined : (lastErr || "未检测到返回的 URL,可能扩展未连接"),
|
||||
publishedAt: new Date().toISOString()
|
||||
};
|
||||
} catch (e: any) {
|
||||
return {
|
||||
platform: this.id, platformName: config.name, status: "failed",
|
||||
errorMessage: e.message, publishedAt: new Date().toISOString()
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/** 从 CLI 输出中提取草稿编辑 URL(优先匹配各平台已知草稿页,排除图片链接) */
|
||||
extractDraftUrl(output: string): string {
|
||||
const urls: string[] = [];
|
||||
const urlRe = /https?:\/\/[^\s"<>)\]]+/g;
|
||||
let m: RegExpExecArray | null;
|
||||
while ((m = urlRe.exec(output)) !== null) urls.push(m[0]);
|
||||
|
||||
const imageRe = /(?:img\.88531\.cn|i\.loli\.net|imgchr\.com|\.(?:png|jpg|jpeg|gif|webp|svg|ico)(?:\?|$))/i;
|
||||
const draftPatterns = [
|
||||
/mp\.weixin\.qq\.com\/cgi-bin\/appmsg/, // 微信公众号
|
||||
/mp\.csdn\.net\/mp_blog\/creation/, // CSDN
|
||||
/zhuanlan\.zhihu\.com\/p\//, // 知乎
|
||||
/mp\.toutiao\.com\/profile_v4\//, // 头条号
|
||||
/zhiyou\.smzdm\.com\/member\//, // 什么值得买
|
||||
/juejin\.cn\/editor\/drafts\//, // 掘金
|
||||
/segmentfault\.com\/\w+\/drafts/, // 思否
|
||||
/weibo\.com\/ttarticle\/p\//, // 微博
|
||||
/mp\.weixin\.qq\.com\/s\// // 公众号分享页
|
||||
];
|
||||
for (const u of urls) for (const p of draftPatterns) if (p.test(u)) return u;
|
||||
// 无匹配时返回第一个非图片链接
|
||||
for (const u of urls) if (!imageRe.test(u)) return u;
|
||||
return "";
|
||||
}
|
||||
|
||||
/** HTML → Markdown(带 title frontmatter) */
|
||||
toMarkdown(article: ArticleContent): string {
|
||||
const header = ["---", 'title: "' + article.title + '"', "---\n"];
|
||||
const body = article.content
|
||||
.replace(/<img[^>]*src="([^"]*)"[^>]*>/gi, "")
|
||||
.replace(/<h2[^>]*>/gi, "## ")
|
||||
.replace(/<\/h2>/gi, "\n\n")
|
||||
.replace(/<h3[^>]*>/gi, "### ")
|
||||
.replace(/<\/h3>/gi, "\n\n")
|
||||
.replace(/<p[^>]*>/gi, "")
|
||||
.replace(/<\/p>/gi, "\n\n")
|
||||
.replace(/<strong>/gi, "**")
|
||||
.replace(/<\/strong>/gi, "**")
|
||||
.replace(/<a[^>]*href="([^"]*)"[^>]*>([\s\S]*?)<\/a>/gi, "[$2]($1)")
|
||||
.replace(/<br\s*\/?>/gi, "\n")
|
||||
.replace(/<[^>]*>/g, "")
|
||||
.replace(/\n{3,}/g, "\n\n")
|
||||
.trim();
|
||||
return header.join("\n") + body;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user