From 625eab223fa75ee33ad2c2dbeafe680516d5721a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=A5=E6=9C=88=E5=8D=83=E9=B8=AE?= <40380042+camera-2018@users.noreply.github.com> Date: Tue, 14 Apr 2026 16:46:32 +0800 Subject: [PATCH] feat: enable shiki highlighting for t2i templates and add a template (#7501) * fix: enable shiki highlighting for t2i templates * fix: t2i templates cr * feat: add new t2i template astrbot_vitepress.html --- astrbot/core/utils/t2i/network_strategy.py | 33 +- .../t2i/template/astrbot_powershell.html | 76 ++- .../utils/t2i/template/astrbot_vitepress.html | 552 ++++++++++++++++++ astrbot/core/utils/t2i/template/base.html | 505 ++++++++-------- .../utils/t2i/template/shiki_runtime.iife.js | 152 +++++ astrbot/core/utils/t2i/template_manager.py | 6 +- dashboard/package.json | 1 + dashboard/scripts/build-t2i-shiki-runtime.mjs | 232 ++++++++ 8 files changed, 1300 insertions(+), 257 deletions(-) create mode 100644 astrbot/core/utils/t2i/template/astrbot_vitepress.html create mode 100644 astrbot/core/utils/t2i/template/shiki_runtime.iife.js create mode 100644 dashboard/scripts/build-t2i-shiki-runtime.mjs diff --git a/astrbot/core/utils/t2i/network_strategy.py b/astrbot/core/utils/t2i/network_strategy.py index 53d9441fa..5f9385614 100644 --- a/astrbot/core/utils/t2i/network_strategy.py +++ b/astrbot/core/utils/t2i/network_strategy.py @@ -1,6 +1,9 @@ import asyncio +import base64 import logging import random +from functools import lru_cache +from pathlib import Path import aiohttp @@ -16,6 +19,31 @@ ASTRBOT_T2I_DEFAULT_ENDPOINT = "https://t2i.soulter.top/text2img" logger = logging.getLogger("astrbot") +@lru_cache(maxsize=1) +def get_shiki_runtime() -> str: + runtime_path = ( + Path(__file__).resolve().parent / "template" / "shiki_runtime.iife.js" + ) + if not runtime_path.exists(): + logger.error( + "T2I Shiki runtime not found at %s. Run `cd dashboard && pnpm run build:t2i-shiki-runtime` to regenerate it. Continuing without code highlighting.", + runtime_path, + ) + return "" + + try: + runtime = runtime_path.read_text(encoding="utf-8") + except (OSError, UnicodeDecodeError) as err: + logger.warning( + "Failed to load T2I Shiki runtime from %s: %s. Continuing without code highlighting.", + runtime_path, + err, + ) + return "" + + return runtime.replace(" None: super().__init__() @@ -77,6 +105,7 @@ class NetworkRenderStrategy(RenderStrategy): if options: default_options |= options + tmpl_data = {"shiki_runtime": get_shiki_runtime()} | tmpl_data post_data = { "tmpl": tmpl_str, "json": return_url, @@ -129,9 +158,9 @@ class NetworkRenderStrategy(RenderStrategy): if not template_name: template_name = "base" tmpl_str = await self.get_template(name=template_name) - text = text.replace("`", "\\`") + text_base64 = base64.b64encode(text.encode("utf-8")).decode("ascii") return await self.render_custom_template( tmpl_str, - {"text": text, "version": f"v{VERSION}"}, + {"text_base64": text_base64, "version": f"v{VERSION}"}, return_url, ) diff --git a/astrbot/core/utils/t2i/template/astrbot_powershell.html b/astrbot/core/utils/t2i/template/astrbot_powershell.html index 9ed3e77a5..746d06fe8 100644 --- a/astrbot/core/utils/t2i/template/astrbot_powershell.html +++ b/astrbot/core/utils/t2i/template/astrbot_powershell.html @@ -2,20 +2,15 @@
-