# 宇之然内容创作平台 - 文章预览API from fastapi import APIRouter, Depends, HTTPException, status from sqlalchemy.orm import Session from typing import Dict from app.database import get_db from app.models import Topic router = APIRouter() @router.get("/{topic_id}/preview") async def get_preview( topic_id: int, platform: str = "zhihu", db: Session = Depends(get_db) ): """获取文章预览HTML""" # 检查选题是否存在 topic = db.query(Topic).filter(Topic.id == topic_id).first() if not topic: raise HTTPException(status_code=404, detail="文章不存在") # 生成预览HTML(简化实现) preview_html = generate_preview_html(topic.title, platform) return {"html": preview_html} def generate_preview_html(title: str, platform: str) -> str: """根据平台和标题生成预览HTML""" base_template = f"""

{title}

{platform} 2026-04-26

这里是文章的正文内容...

文章包含多个段落,展示不同的写作风格和结构。

在实际生产环境中,这里应该是完整的文章内容。

""" return base_template