Files
hsa/Html/apps/views/markdown.py
2025-09-06 21:27:28 +08:00

31 lines
1.1 KiB
Python

import os
from flask import Blueprint, jsonify, current_app, send_from_directory
from ..services.markdown_service import (
convert_markdown_to_html, wrap_with_styles,
save_html, copy_images, load_markdown_file
)
bp = Blueprint("markdown", __name__)
@bp.route("/<course_id>-<chapter_name>-<lesson_name>-markdown", methods=["GET"])
def convert_md(course_id, chapter_name, lesson_name):
print(f"convert_md: {course_id}, {chapter_name}, {lesson_name}")
md_file = load_markdown_file(course_id, chapter_name, lesson_name)
# 转换 markdown -> html
html = convert_markdown_to_html(md_file)
html_with_styles = wrap_with_styles(html)
# 保存 HTML 文件到 static
html_output_path = os.path.join(current_app.config["STATIC_DIR"], f"{chapter_name}-{lesson_name}.html")
save_html(html_with_styles, html_output_path)
return jsonify({"html_url": f"/static/{chapter_name}-{lesson_name}.html"})
# 提供静态文件访问
@bp.route("/static/<path:filename>")
def serve_static(filename):
return send_from_directory(current_app.config["STATIC_DIR"], filename)