Files
hsa/Html/apps/views/markdown.py

56 lines
1.6 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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("/<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源文件内容不进行服务器端渲染
return jsonify({"markdown_content": md_file})
# 提供静态文件访问
@bp.route("/static/<path:filename>")
def serve_static(filename):
return send_from_directory(current_app.config["STATIC_DIR"], filename)
def wrap_with_styles(html_content):
# 在HTML中添加MathJax支持
mathjax_script = """
<script src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
<script>
MathJax.config = {
tex: {
inlineMath: [['$', '$'], ['\\(', '\\)']]
},
svg: {
fontCache: 'global'
}
};
</script>
"""
# 包装HTML内容和样式
return f"""
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
<!-- 你的其他样式 -->
{mathjax_script}
</head>
<body>
{html_content}
</body>
</html>
"""