2025-08-15 16:34:37 +08:00
|
|
|
|
from abc import ABC, abstractmethod
|
2025-08-28 09:20:15 +08:00
|
|
|
|
from collections.abc import Iterable
|
2025-08-18 23:08:22 +08:00
|
|
|
|
from pathlib import Path
|
2025-08-28 09:20:15 +08:00
|
|
|
|
from typing import Any, Literal
|
2025-08-15 16:34:37 +08:00
|
|
|
|
|
2025-08-18 23:08:22 +08:00
|
|
|
|
import aiofiles
|
2025-08-15 16:34:37 +08:00
|
|
|
|
from pydantic import BaseModel, Field
|
|
|
|
|
|
|
2025-08-18 23:08:22 +08:00
|
|
|
|
from zhenxun.services.log import logger
|
|
|
|
|
|
|
2025-08-28 09:20:15 +08:00
|
|
|
|
from .base import ContainerComponent, RenderableComponent
|
2025-08-18 23:08:22 +08:00
|
|
|
|
|
2025-08-15 16:34:37 +08:00
|
|
|
|
__all__ = [
|
|
|
|
|
|
"CodeElement",
|
2025-08-28 09:20:15 +08:00
|
|
|
|
"ComponentElement",
|
2025-08-15 16:34:37 +08:00
|
|
|
|
"HeadingElement",
|
|
|
|
|
|
"ImageElement",
|
|
|
|
|
|
"ListElement",
|
|
|
|
|
|
"ListItemElement",
|
|
|
|
|
|
"MarkdownData",
|
|
|
|
|
|
"MarkdownElement",
|
|
|
|
|
|
"QuoteElement",
|
|
|
|
|
|
"RawHtmlElement",
|
|
|
|
|
|
"TableElement",
|
|
|
|
|
|
"TextElement",
|
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MarkdownElement(BaseModel, ABC):
|
|
|
|
|
|
@abstractmethod
|
|
|
|
|
|
def to_markdown(self) -> str:
|
|
|
|
|
|
"""Serializes the element to its Markdown string representation."""
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TextElement(MarkdownElement):
|
2025-08-28 09:20:15 +08:00
|
|
|
|
type: Literal["text"] = "text"
|
2025-08-15 16:34:37 +08:00
|
|
|
|
text: str
|
|
|
|
|
|
|
|
|
|
|
|
def to_markdown(self) -> str:
|
|
|
|
|
|
return self.text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class HeadingElement(MarkdownElement):
|
2025-08-28 09:20:15 +08:00
|
|
|
|
type: Literal["heading"] = "heading"
|
2025-08-15 16:34:37 +08:00
|
|
|
|
text: str
|
|
|
|
|
|
level: int = Field(..., ge=1, le=6)
|
|
|
|
|
|
|
|
|
|
|
|
def to_markdown(self) -> str:
|
|
|
|
|
|
return f"{'#' * self.level} {self.text}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ImageElement(MarkdownElement):
|
2025-08-28 09:20:15 +08:00
|
|
|
|
type: Literal["image"] = "image"
|
2025-08-15 16:34:37 +08:00
|
|
|
|
src: str
|
|
|
|
|
|
alt: str = "image"
|
|
|
|
|
|
|
|
|
|
|
|
def to_markdown(self) -> str:
|
|
|
|
|
|
return f""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CodeElement(MarkdownElement):
|
2025-08-28 09:20:15 +08:00
|
|
|
|
type: Literal["code"] = "code"
|
2025-08-15 16:34:37 +08:00
|
|
|
|
code: str
|
|
|
|
|
|
language: str = ""
|
|
|
|
|
|
|
|
|
|
|
|
def to_markdown(self) -> str:
|
|
|
|
|
|
return f"```{self.language}\n{self.code}\n```"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class RawHtmlElement(MarkdownElement):
|
2025-08-28 09:20:15 +08:00
|
|
|
|
type: Literal["raw_html"] = "raw_html"
|
2025-08-15 16:34:37 +08:00
|
|
|
|
html: str
|
|
|
|
|
|
|
|
|
|
|
|
def to_markdown(self) -> str:
|
|
|
|
|
|
return self.html
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TableElement(MarkdownElement):
|
2025-08-28 09:20:15 +08:00
|
|
|
|
type: Literal["table"] = "table"
|
2025-08-15 16:34:37 +08:00
|
|
|
|
headers: list[str]
|
|
|
|
|
|
rows: list[list[str]]
|
|
|
|
|
|
alignments: list[Literal["left", "center", "right"]] | None = None
|
|
|
|
|
|
|
|
|
|
|
|
def to_markdown(self) -> str:
|
|
|
|
|
|
header_row = "| " + " | ".join(self.headers) + " |"
|
|
|
|
|
|
|
|
|
|
|
|
if self.alignments:
|
|
|
|
|
|
align_map = {"left": ":---", "center": ":---:", "right": "---:"}
|
|
|
|
|
|
separator_row = (
|
|
|
|
|
|
"| "
|
|
|
|
|
|
+ " | ".join([align_map.get(a, "---") for a in self.alignments])
|
|
|
|
|
|
+ " |"
|
|
|
|
|
|
)
|
|
|
|
|
|
else:
|
|
|
|
|
|
separator_row = "| " + " | ".join(["---"] * len(self.headers)) + " |"
|
|
|
|
|
|
|
|
|
|
|
|
data_rows = "\n".join(
|
|
|
|
|
|
"| " + " | ".join(map(str, row)) + " |" for row in self.rows
|
|
|
|
|
|
)
|
|
|
|
|
|
return f"{header_row}\n{separator_row}\n{data_rows}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ContainerElement(MarkdownElement):
|
|
|
|
|
|
content: list[MarkdownElement] = Field(default_factory=list)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class QuoteElement(ContainerElement):
|
2025-08-28 09:20:15 +08:00
|
|
|
|
type: Literal["quote"] = "quote"
|
|
|
|
|
|
|
2025-08-15 16:34:37 +08:00
|
|
|
|
def to_markdown(self) -> str:
|
|
|
|
|
|
inner_md = "\n".join(part.to_markdown() for part in self.content)
|
|
|
|
|
|
return "\n".join([f"> {line}" for line in inner_md.split("\n")])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ListItemElement(ContainerElement):
|
|
|
|
|
|
def to_markdown(self) -> str:
|
|
|
|
|
|
return "\n".join(part.to_markdown() for part in self.content)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ListElement(ContainerElement):
|
2025-08-28 09:20:15 +08:00
|
|
|
|
type: Literal["list"] = "list"
|
2025-08-15 16:34:37 +08:00
|
|
|
|
ordered: bool = False
|
|
|
|
|
|
|
|
|
|
|
|
def to_markdown(self) -> str:
|
|
|
|
|
|
lines = []
|
|
|
|
|
|
for i, item in enumerate(self.content):
|
|
|
|
|
|
if isinstance(item, ListItemElement):
|
|
|
|
|
|
prefix = f"{i + 1}." if self.ordered else "*"
|
|
|
|
|
|
item_content = item.to_markdown()
|
|
|
|
|
|
lines.append(f"{prefix} {item_content}")
|
|
|
|
|
|
return "\n".join(lines)
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-08-28 09:20:15 +08:00
|
|
|
|
class ComponentElement(MarkdownElement):
|
|
|
|
|
|
"""一个特殊的元素,用于在Markdown流中持有另一个可渲染组件。"""
|
|
|
|
|
|
|
|
|
|
|
|
type: Literal["component"] = "component"
|
|
|
|
|
|
component: RenderableComponent
|
|
|
|
|
|
|
|
|
|
|
|
def to_markdown(self) -> str:
|
|
|
|
|
|
return ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MarkdownData(ContainerComponent):
|
2025-08-15 16:34:37 +08:00
|
|
|
|
"""Markdown转图片的数据模型"""
|
|
|
|
|
|
|
|
|
|
|
|
style_name: str | None = None
|
2025-08-28 09:20:15 +08:00
|
|
|
|
elements: list[MarkdownElement] = Field(default_factory=list)
|
2025-08-15 16:34:37 +08:00
|
|
|
|
width: int = 800
|
|
|
|
|
|
css_path: str | None = None
|
2025-08-18 23:08:22 +08:00
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
|
def template_name(self) -> str:
|
|
|
|
|
|
return "components/core/markdown"
|
|
|
|
|
|
|
2025-08-28 09:20:15 +08:00
|
|
|
|
def get_children(self) -> Iterable[RenderableComponent]:
|
|
|
|
|
|
"""让CSS/JS依赖收集器能够递归地找到所有嵌入的组件。"""
|
|
|
|
|
|
|
|
|
|
|
|
def find_components_recursive(
|
|
|
|
|
|
elements: list[MarkdownElement],
|
|
|
|
|
|
) -> Iterable[RenderableComponent]:
|
|
|
|
|
|
for element in elements:
|
|
|
|
|
|
if isinstance(element, ComponentElement):
|
|
|
|
|
|
yield element.component
|
|
|
|
|
|
if hasattr(element.component, "get_children"):
|
|
|
|
|
|
yield from element.component.get_children()
|
|
|
|
|
|
elif isinstance(element, ContainerElement):
|
|
|
|
|
|
yield from find_components_recursive(element.content)
|
|
|
|
|
|
|
|
|
|
|
|
yield from find_components_recursive(self.elements)
|
|
|
|
|
|
|
|
|
|
|
|
async def get_extra_css(self, context: Any) -> str:
|
2025-08-18 23:08:22 +08:00
|
|
|
|
if self.css_path:
|
|
|
|
|
|
css_file = Path(self.css_path)
|
|
|
|
|
|
if css_file.is_file():
|
|
|
|
|
|
async with aiofiles.open(css_file, encoding="utf-8") as f:
|
|
|
|
|
|
return await f.read()
|
|
|
|
|
|
else:
|
|
|
|
|
|
logger.warning(f"Markdown自定义CSS文件不存在: {self.css_path}")
|
|
|
|
|
|
else:
|
2025-08-28 09:20:15 +08:00
|
|
|
|
style_name = self.style_name or "light"
|
|
|
|
|
|
# 使用上下文对象来解析路径
|
|
|
|
|
|
css_path = await context.theme_manager.resolve_markdown_style_path(
|
|
|
|
|
|
style_name, context
|
2025-08-18 23:08:22 +08:00
|
|
|
|
)
|
2025-08-28 09:20:15 +08:00
|
|
|
|
if css_path and css_path.exists():
|
2025-08-18 23:08:22 +08:00
|
|
|
|
async with aiofiles.open(css_path, encoding="utf-8") as f:
|
|
|
|
|
|
return await f.read()
|
|
|
|
|
|
return ""
|