This commit is contained in:
2026-04-18 13:20:20 +09:00
parent 2dcd28ecd9
commit 156a4ef951
16 changed files with 165 additions and 21 deletions
+28
View File
@@ -3,6 +3,8 @@ import json
import yaml
import random
import mistune
import resvg_py
from html import escape
from pathlib import Path
from bs4 import BeautifulSoup
from markitdown import MarkItDown
@@ -99,6 +101,32 @@ welcome to nercone.dev!
async def fake_error_page(request: Request, code: str):
return error_page(templates=templates, request=request, status_code=int(code))
@app.api_route("/assets/thumbnail/{path:path}", methods=["GET"])
async def thumbnail(request: Request, path: str) -> Response:
title = request.query_params.get("title", "Untitled Page")
description = request.query_params.get("description", "No description.")
template_type = request.query_params.get("template", "normal")
parts = [p for p in path.strip("/").split("/") if p]
path_display = "nercone.dev / " + " / ".join(parts) if parts else "nercone.dev"
svg_filename = "error.svg" if template_type == "error" else "normal.svg"
fonts_dir = Path.cwd().joinpath("public", "assets", "fonts")
svg_path = Path.cwd().joinpath("public", "assets", "thumbnails", svg_filename)
svg = svg_path.read_text(encoding="utf-8")
svg = svg.replace("__PATH__", escape(path_display))
svg = svg.replace("__TITLE__", escape(title))
svg = svg.replace("__DESCRIPTION__", escape(description))
font_files = [
str(fonts_dir / "MesloBIZUD-Regular.ttf"),
str(fonts_dir / "InterBIZUD-Regular.ttf"),
str(fonts_dir / "InterBIZUD-Bold.ttf"),
]
png = resvg_py.svg_to_bytes(svg, font_files=font_files, width=1200, height=600)
return Response(content=png, media_type="image/png")
@app.api_route("/{full_path:path}", methods=["GET", "POST", "HEAD"])
async def default_response(request: Request, full_path: str) -> Response:
if not full_path.endswith(".html") and not full_path.endswith(".md"):