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 ) from markdown_it import MarkdownIt from mdit_py_plugins import texmath bp = Blueprint("markdown", __name__) @bp.route("/---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解析器,启用数学公式支持 md = MarkdownIt() md.use(texmath.texmath_plugin) # 添加数学公式插件 # 转换markdown为html(包含公式标记) html = md.render(md_file) # 包装样式时添加MathJax支持,用于在浏览器中渲染公式 html_with_styles = wrap_with_styles(html) # 保存HTML文件到static html_output_path = os.path.join(current_app.config["STATIC_DIR"], f"{course_id}-{chapter_name}-{lesson_name}.html") save_html(html_with_styles, html_output_path) return jsonify({"html_url": f"/static/{course_id}-{chapter_name}-{lesson_name}.html"}) # 提供静态文件访问 @bp.route("/static/") def serve_static(filename): return send_from_directory(current_app.config["STATIC_DIR"], filename) def wrap_with_styles(html_content): # 在HTML中添加MathJax支持 mathjax_script = """ """ # 包装HTML内容和样式 return f""" Document {mathjax_script} {html_content} """