ForcePilot/backend/package/yuxi/channel/extensions/twitch/markdown.py
Kris 0babb1ca8e feat(channel): 添加 Tlon 渠道扩展
新增 Tlon 渠道扩展,支持在 Yuxi 平台中集成 Tlon/Urbit 去中心化通讯平台。

包含以下功能模块:
- tlon_api: Tlon API 客户端封装
- config: 渠道配置管理
- gateway: SSE/WebSocket 网关接入
- sse_client: SSE 客户端
- outbound: 外发消息管理
- send: 消息发送
- security: 安全校验
- auth: 认证管理
- monitor: 渠道状态监控
- status: 会话状态管理
- session: 会话管理
- approval: 审批流程
- channel_mgmt: 频道管理
- channel_ops: 频道操作
- contacts: 联系人管理
- discovery: 服务发现
- doctor: 健康诊断
- expose: 服务暴露
- gallery: 图库管理
- history: 历史记录
- hooks: 钩子管理
- media: 媒体资源处理
- notebook: 笔记本功能
- settings_store: 设置存储
- setup: 初始化设置
- story: 故事功能
- targets: 目标管理
- cite_parser: 引用解析
- utils: 工具函数
- types: 类型定义
2026-05-21 11:55:25 +08:00

42 lines
1.5 KiB
Python

import re
def strip_markdown_for_twitch(markdown: str) -> str:
text = markdown
text = re.sub(r"!\[[^\]]*\]\([^)]+\)", "", text)
text = re.sub(r"\[([^\]]+)\]\([^)]+\)", r"\1", text)
text = re.sub(r"\*\*([^*]+)\*\*", r"\1", text)
text = re.sub(r"__([^_]+)__", r"\1", text)
text = re.sub(r"(?<!\*)\*([^*]+)\*(?!\*)", r"\1", text)
text = re.sub(r"(?<!_)_([^_]+)_(?!_)", r"\1", text)
text = re.sub(r"~~([^~]+)~~", r"\1", text)
text = re.sub(r"```[\s\S]*?```", lambda m: m.group(0).replace("```", "").strip(), text)
text = re.sub(r"`([^`]+)`", r"\1", text)
text = re.sub(r"^#{1,6}\s+", "", text, flags=re.MULTILINE)
text = re.sub(r"^\s*[-*+]\s+", "", text, flags=re.MULTILINE)
text = re.sub(r"^\s*\d+\.\s+", "", text, flags=re.MULTILINE)
text = text.replace("\n", " ")
text = re.sub(r"[ \t]{2,}", " ", text)
return text.strip()
def chunk_text_for_twitch(text: str, limit: int = 500) -> list[str]:
cleaned = strip_markdown_for_twitch(text)
if len(cleaned) <= limit:
return [cleaned] if cleaned else []
chunks = []
remaining = cleaned
while len(remaining) > limit:
window = remaining[:limit]
last_space = window.rfind(" ")
if last_space == -1:
chunks.append(window)
remaining = remaining[limit:]
else:
chunks.append(window[:last_space])
remaining = remaining[last_space + 1 :]
if remaining:
chunks.append(remaining)
return chunks