{"id":1633,"date":"2024-06-14T13:20:57","date_gmt":"2024-06-14T05:20:57","guid":{"rendered":"https:\/\/blogsalt.trollbar.xyz\/?p=1633"},"modified":"2024-06-28T00:57:06","modified_gmt":"2024-06-27T16:57:06","slug":"%e7%bc%96%e5%86%99%e4%b8%80%e4%b8%aa%e5%9f%ba%e4%ba%8emarkdown%e7%9a%84%e9%95%bf%e5%be%ae%e5%8d%9a%e7%94%9f%e6%88%90%e5%99%a8","status":"publish","type":"post","link":"https:\/\/blogsalt.trollbar.xyz\/index.php\/2024\/06\/1633\/","title":{"rendered":"\u7f16\u5199\u4e00\u4e2a\u57fa\u4e8eMarkdown\u7684\u957f\u5fae\u535a\u751f\u6210\u5668"},"content":{"rendered":"<h1>\u524d\u8a00<\/h1>\n<p>\u8d77\u56e0\u662f\u6211\u60f3\u6574\u7406\u4e00\u4e0b\u4eca\u5e74\u770b\u7684\u957f\u7247\uff0c\u53d1\u4e2a\u957f\u5fae\u535a\uff0c\u51c6\u5907\u7a0d\u5fae\u4e0a\u70b9\u683c\u5f0f\uff0c\u4e8e\u662f\u7814\u7a76\u4e86\u4e00\u4e0b\u5728\u7ebfmd\u7f16\u8f91\u5668\uff08\u672c\u7740\u6298\u817e\u7684\u7cbe\u795e\uff0c\u5f53\u7136\u662f\u8981\u90e8\u7f72\u5728\u5bb6\u5ead\u670d\u52a1\u5668\u4e0a\u7684\uff09\uff0c\u6700\u540e\u90e8\u7f72\u4e86StackEdit\u7684\u4e2d\u6587\u7248\u3002\u5b83\u53ef\u4ee5\u76f4\u63a5\u7528github\u540c\u6b65\uff0c\u800cgithub\u53ef\u80fd\u662f\u552f\u4e00\u652f\u6301\u975ehttps oauth2\u548ccallback\u7684\u670d\u52a1\uff0c\u4e3a\u4ec0\u4e48\u554a\uff1f<\/p>\n<p>\u4f46\u968f\u5373\u6211\u53d1\u73b0\u7f51\u7edc\u4e0a\u5e76\u6ca1\u6709\u7279\u522b\u597d\u7528\u7684\uff0c\u80fd\u628aMD\u6587\u4ef6\u76f4\u63a5\u6e32\u67d3\u6210\u957f\u56fe\u7684\u5de5\u5177\u2014\u2014\u4e3a\u4ec0\u4e48\uff1f\u4e00\u4e9b\u4ec5\u6709\u7684\u9009\u9879\u6548\u679c\u975e\u5e38\u4e11\u964b\uff0c\u529f\u80fd\u4e5f\u5f88\u6b8b\u7f3a\u3002\u6240\u4ee5\u6211\u51b3\u5b9a\u81ea\u5df1\u52a8\u624b\uff0c\u5199\u4e00\u4e2aDocker\u955c\u50cf\uff0c\u80fd\u4e00\u952e\u90e8\u7f72\u3001\u4e00\u952e\u8f6c\u6362\uff0c\u800c\u4e14\u8981\u652f\u6301\u4ee5\u4e0b\u7279\u6027\uff1a<\/p>\n<div class='shortcode-todo custom-control custom-checkbox'>\n\t\t\t\t<input class='custom-control-input' type='checkbox' checked>\n\t\t\t\t<label class='custom-control-label'>\n\t\t\t\t\t<span>\u81ea\u5b9a\u4e49\u56fe\u7247\u5bbd\u5ea6\uff1b<\/span>\n\t\t\t\t<\/label>\n\t\t\t<\/div>\n<div class='shortcode-todo custom-control custom-checkbox'>\n\t\t\t\t<input class='custom-control-input' type='checkbox' checked>\n\t\t\t\t<label class='custom-control-label'>\n\t\t\t\t\t<span>\u81ea\u5b9a\u4e49\u6587\u5b57\u5c3a\u5bf8\uff1b<\/span>\n\t\t\t\t<\/label>\n\t\t\t<\/div>\n<div class='shortcode-todo custom-control custom-checkbox'>\n\t\t\t\t<input class='custom-control-input' type='checkbox' checked>\n\t\t\t\t<label class='custom-control-label'>\n\t\t\t\t\t<span>\u652f\u6301\u9644\u52a0CSS\u6837\u5f0f\uff1b<\/span>\n\t\t\t\t<\/label>\n\t\t\t<\/div>\n<div class='shortcode-todo custom-control custom-checkbox'>\n\t\t\t\t<input class='custom-control-input' type='checkbox' checked>\n\t\t\t\t<label class='custom-control-label'>\n\t\t\t\t\t<span>\u652f\u6301\u5b9e\u65f6\u9884\u89c8\u6e32\u67d3\u6548\u679c\uff1b<\/span>\n\t\t\t\t<\/label>\n\t\t\t<\/div>\n<div class='shortcode-todo custom-control custom-checkbox'>\n\t\t\t\t<input class='custom-control-input' type='checkbox'>\n\t\t\t\t<label class='custom-control-label'>\n\t\t\t\t\t<span>\u652f\u6301Latex\u3001UML\u56fe\u7b49\u6548\u679c\uff1b<\/span>\n\t\t\t\t<\/label>\n\t\t\t<\/div>\n<div class='shortcode-todo custom-control custom-checkbox'>\n\t\t\t\t<input class='custom-control-input' type='checkbox'>\n\t\t\t\t<label class='custom-control-label'>\n\t\t\t\t\t<span>\u652f\u6301\u76f4\u63a5\u8f6c\u6362html<\/span>\n\t\t\t\t<\/label>\n\t\t\t<\/div>\n<p>\u5176\u4e2d\u540e\u4e24\u4e2a\u7684\u4f18\u5148\u7ea7\u4e0d\u9ad8\uff0c\u5c31\u5148\u8fd9\u6837\u5427\u3002<\/p>\n<h1>\u7ed3\u6784\u548c\u4f9d\u8d56<\/h1>\n<p>\u548c\u4ee5\u524d\u4e00\u6837\uff0c\u6574\u4e2a\u5e94\u7528\u662f\u7528Python\u5199\u7684\uff0c\u524d\u7aef\u4f7f\u7528Flask\u3002\u4f7f\u7528mistune\u6765\u5c06md\u6587\u4ef6\u89e3\u6790\u4e3ahtml\uff08\u6bd4markdown\u597d\u7528\u4e86\u5341\u500d\u751a\u81f3\u4e5d\u500d\uff09\uff0c\u7136\u540e\u7528imgkit\u5e93\u5c06\u5176\u6e32\u67d3\u4e3a\u56fe\u7247\uff0cimgkit\u5e93\u5728\u6e32\u67d3\u65f6\u9700\u8981\u547d\u4ee4\u884c\u5de5\u5177wkhtmltopdf\u3002\u6700\u540e\uff0c\u4f7f\u7528Pillow\u5e93\u8fdb\u884c\u56fe\u7247\u538b\u7f29\uff0c\u63d0\u4f9b\u7ed9\u7528\u6237\u4e0b\u8f7d\u3002\u9664\u6b64\u4e4b\u5916\uff0c\u8fd8\u9700\u8981\u4f7f\u7528\u547d\u4ee4\u884c\u5de5\u5177fontconfig\u6765\u914d\u7f6e\u5b57\u4f53\uff0c\u9632\u6b62wkhtmltopdf\u5728\u964c\u751f\u7684\u7cfb\u7edf\u4e0a\u516d\u4eb2\u4e0d\u8ba4\u3002<\/p>\n<p>\u6700\u7ec8\uff0c\u6574\u4e2a\u6587\u4ef6\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n<pre class=\"code\">\u00a0MD2IMG\r\n\u251c\u2500 app.py\r\n\u251c\u2500 Dockerfile\r\n\u251c\u2500 requirements.txt\r\n\u251c\u2500 templates\r\n\u2502  \u2514\u2500 index.html\r\n\u2514\u2500 static\r\n   \u251c\u2500 default_light.css\r\n   \u251c\u2500 default_night.css\r\n   \u251c\u2500 logo.ico\r\n   \u251c\u2500 logo.png\r\n   \u251c\u2500 script.js\r\n   \u251c\u2500 style.css\r\n   \u2514\u2500 fonts\r\n      \u2514\u2500 \u5fae\u8f6f\u96c5\u9ed1.ttf\r\n<\/pre>\n<p>\u5176\u4e2drequirements.txt\u5305\u62ec\uff1a<\/p>\n<pre class=\"code\">Flask\r\nmistune\r\nimgkit\r\nPillow\r\n<\/pre>\n<p>Dockerfile\uff1a<\/p>\n<pre class=\"code\">FROM python:3.8-slim\r\n\r\n# \u5b89\u88c5 wkhtmltopdf \u548c fontconfig\r\nRUN apt-get update &amp;&amp; \\\r\n    apt-get install -y wkhtmltopdf fontconfig &amp;&amp; \\\r\n    apt-get clean\r\n\r\n# \u8bbe\u7f6e\u5de5\u4f5c\u76ee\u5f55\r\nWORKDIR \/app\r\n\r\n# \u590d\u5236\u5e94\u7528\u6587\u4ef6\r\nCOPY . \/app\r\n\r\n# \u5b89\u88c5 Python \u4f9d\u8d56\r\nRUN pip install --no-cache-dir -r requirements.txt\r\n\r\n# \u590d\u5236\u5b57\u4f53\u6587\u4ef6\u5230\u7cfb\u7edf\u5b57\u4f53\u76ee\u5f55\u5e76\u66f4\u65b0\u5b57\u4f53\u7f13\u5b58\r\nCOPY static\/fonts\/*.ttf \/usr\/share\/fonts\/\r\nRUN fc-cache -fv\r\n\r\n# \u66b4\u9732\u7aef\u53e3\r\nEXPOSE 5000\r\n\r\n# \u8fd0\u884c\u5e94\u7528\r\nCMD [\"python\", \"app.py\"]\r\n<\/pre>\n<h1>\u6784\u5efa\u57fa\u672c\u529f\u80fd<\/h1>\n<p>\u6211\u4eec\u8981\u5b9e\u73b0\u7684\u6700\u57fa\u7840\u7684\u529f\u80fd\u662f\uff1a\u7528\u6237\u63d0\u4ea4\u4e00\u4e2amd\u6587\u4ef6\uff0c\u70b9\u4e00\u4e0b\u6309\u94ae\uff0c\u83b7\u5f97\u4e00\u5f20\u56fe\u7247\u3002\u4e00\u65e6\u6211\u4eec\u5b9e\u73b0\u4e86\u8fd9\u4e2a\u529f\u80fd\uff0c\u5176\u4ed6\u90e8\u5206\u4e0d\u8fc7\u662f\u4fee\u4fee\u8865\u8865\u7f62\u4e86\u3002<\/p>\n<p>\u4e3a\u4e86\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff0cindex.html\u53ef\u4ee5\u975e\u5e38\u7b80\u6d01\uff1a<\/p>\n<pre class=\"code\"> &lt;!DOCTYPE html&gt;\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n&lt;title&gt;MD\u8f6c\u56fe\u7247&lt;\/title&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n&lt;h1&gt;\u4e0a\u4f20MD\u6587\u4ef6&lt;\/h1&gt;\r\n&lt;form action=\"\/upload\" method=\"post\" enctype=\"multipart\/form-data\"&gt;\r\n&lt;input type=\"file\" name=\"file\" required&gt;\r\n&lt;input type=\"text\" name=\"width\" placeholder=\"\u5bbd\u5ea6\"&gt;\r\n&lt;input type=\"submit\" value=\"\u5f00\u59cb\u8f6c\u6362\"&gt;\r\n&lt;\/form&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<p>app.py:<\/p>\n<pre class=\"code\">from flask import Flask, request, render_template, send_file\r\nimport mistune\r\nimport imgkit\r\n\r\napp = Flask(__name__)\r\n\r\n@app.route('\/', methods=['GET'])\r\ndef index():\r\n    return render_template('index.html')\r\n\r\n@app.route('\/upload', methods=['POST'])\r\ndef upload_file():\r\n    file = request.files['file']\r\n    width = request.form.get('width', '800')  # \u9ed8\u8ba4\u5bbd\u5ea6\u4e3a800\r\n    if file:\r\n        content = file.read().decode('utf-8')\r\n        markdown = mistune.create_markdown(plugins=['strikethrough','footnotes','table','url','task_lists','def_list','abbr','mark','insert','superscript','subscript','math','ruby','spoiler'])\r\n        html = markdown(content)\r\n        options = {\r\n            'format': 'png',\r\n            'quality': '100',\r\n            'width' : int(width)\r\n        }\r\n        img = imgkit.from_string(html, False, options=options)\r\n        with open(\"output.png\", \"wb\") as f:\r\n            f.write(img)\r\n        return send_file(\"output.png\", as_attachment=True)\r\n    return 'No file provided', 400\r\n\r\nif __name__ == '__main__':\r\n    app.run(debug=True, host='0.0.0.0')\r\n\r\n<\/pre>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cwidth\u53c2\u6570\u53ea\u63a5\u53d7int\u7c7b\u578b\u3002wkhtmltopdf\u8fd8\u6709\u4e00\u4e2a\u53c2\u6570\u662f\u201cdisable-smart-width\u201d\uff0c\u6b63\u5e38\u4e0d\u7528\u7ba1\u5373\u53ef\uff0c\u5373\u4f7f\u8fd9\u6837\u4f1a\u8ba9\u6700\u7ec8\u751f\u6210\u7684\u56fe\u7247\u7565\u5bbd\u4e00\u4e9b\u3002\u56e0\u4e3awkhtmltopdf\u4f1a\u667a\u80fd\u5224\u65ad\u6e32\u67d3\u5185\u5bb9\u9700\u8981\u591a\u5bbd\u7684\u8ddd\u79bb\uff0c\u6216\u591a\u6216\u5c11\u90fd\u4f1a\u6dfb\u4e00\u70b9\u907f\u514d\u51fa\u754c\u3002\u5982\u679c\u8bbe\u7f6e\u4e3a\u7a7a\u5b57\u7b26\u4e32\u7981\u7528\u667a\u80fd\u5bbd\u5ea6\u7684\u8bdd\uff0c\u4e00\u822c\u90fd\u4e0d\u4f1a\u6548\u679c\u66f4\u597d\u3002<\/p>\n<h1>\u56fe\u7247\u538b\u7f29\/\u81ea\u52a8\u5220\u9664<\/h1>\n<p>\u76f4\u63a5\u8ba9imgkit\u5c31\u8fd9\u4e48\u8f93\u51fa\u56fe\u7247\uff0c\u4f1a\u5f97\u5230\u4e00\u4e2a\u5de8\u5927\u7684\u65e0\u635fpng\u3002\u4ee5\u6211\u7528\u6765\u6d4b\u8bd5\u7684\u4e0d\u592a\u957f\u7684\u5f71\u8bc4MD\u6587\u4ef6\u800c\u8a00\uff0c\u751f\u6210\u4e8620\u591aM\u7684\u56fe\u7247\uff0c\u8fd9\u4e0d\u4ec5\u4e0d\u592a\u9002\u5408\u53d1\u5fae\u535a\uff0c\u5bf9\u7f51\u7edc\u548c\u670d\u52a1\u5668\u5b58\u50a8\u4e5f\u90fd\u4e0d\u592a\u53cb\u597d\u3002\u6240\u4ee5\u6211\u4eec\u8981\u8d81\u7740\u5e94\u7528\u7ed3\u6784\u8fd8\u5f88\u7b80\u5355\uff0c\u5148\u52a0\u5165\u4e24\u4e2a\u529f\u80fd\uff1a\u4f7f\u7528Pillow\u538b\u7f29\u6587\u4ef6\uff0c\u4e0d\u8981\u8ba9\u751f\u6210\u7684\u4e1c\u897f\u592a\u5413\u4eba\uff1b\u751f\u6210\u56fe\u7247\u4e00\u6bb5\u65f6\u95f4\u540e\uff0c\u628a\u6587\u4ef6\u5220\u9664\u3002<\/p>\n<p>\u5bf9\u4e8e\u524d\u8005\uff0c\u6211\u4eec\u5728\u751f\u6210\u4e86img\u5b9e\u4f8b\u540e\uff0c\u5c06\u5176\u4fdd\u5b58\u5230\/tmp\u6587\u4ef6\u5939\uff0c\u7136\u540e\u8c03\u7528Pillow\u7684Image\u5e93\u5c06\u5176\u538b\u7f29\uff1a<\/p>\n<pre class=\"code\">from PIL import Image\r\n\r\n# ...\u5df2\u7ecf\u751f\u6210\u4e86img\u5b9e\u4f8b\r\n\r\n# \u786e\u4fdd tmp \u76ee\u5f55\u5b58\u5728\r\nos.makedirs('tmp', exist_ok=True)\r\n# \u5c06\u56fe\u7247\u4fdd\u5b58\u5230\u78c1\u76d8\uff0c\u6253\u4e00\u4e2a\u65f6\u95f4\u6233\r\noutput_path = f'tmp\/output_{int(time.time())}.png'\r\nwith open(output_path, 'wb') as f:\r\n\tf.write(img)\r\n\r\n# \u8bfb\u53d6\u56fe\u7247\u5e76\u538b\u7f29\r\nimage = Image.open(output_path)\r\ncompress_png_path = output_path.replace('.png', '_compressed.png')\r\nimage.save(compress_png_path, 'PNG', optimize=True)  # \u4f7f\u7528optimize\u9009\u9879\u538b\u7f29PNG\r\n\r\n# \u8fd4\u56de\u6587\u4ef6\r\nreturn send_file(compress_png_path, mimetype='image\/png', as_attachment=True)<\/pre>\n<p>\u5bf9\u4e8e\u540e\u8005\uff0c\u53ef\u4ee5\u8c03\u7528Python\u7684threading\u6a21\u5757\uff0c\u5728\u751f\u6210\u56fe\u7247\u540e\u53d1\u8d77\u4e00\u4e2a\u7ebf\u7a0b\uff0c\u4ee5\u5220\u9664\u56fe\u7247\u3002\u7531\u4e8e\u6211\u4eec\u4e4b\u524d\u7684\u64cd\u4f5c\uff0c\u5e94\u7528\u4e8b\u5b9e\u4e0a\u751f\u6210\u4e86\u4e24\u4e2a\u56fe\u7247\u6587\u4ef6\uff0c\u4e24\u4e2a\u90fd\u8981\u5220\u9664\u3002<\/p>\n<pre class=\"code\">import threading\r\ndef delete_file(path):\r\n    \"\"\"\u7b49\u5f85\u6307\u5b9a\u65f6\u95f4\u540e\u5220\u9664\u6587\u4ef6\"\"\"\r\n    def delay_delete():\r\n        os.remove(path)\r\n    timer = threading.Timer(1800, delay_delete)  # \u8bbe\u7f6e\u5b9a\u65f6\u5668\uff0c1800\u79d2\u540e\u6267\u884cdelay_delete\u51fd\u6570\r\n    timer.start()\r\n\r\n# \u5728\u8fd4\u56de\u6587\u4ef6\u524d\u8c03\u7528\r\ndelete_file(output_path)  # \u8c03\u7528delete_file\u51fd\u6570\u5b89\u6392\u5220\u9664\r\ndelete_file(compress_png_path)\r\n<\/pre>\n<h1>\u9875\u9762\u7f8e\u5316\uff0c\u6dfb\u52a0\u529f\u80fd<\/h1>\n<p>\u63a5\u4e0b\u6765\u6211\u4eec\u5c31\u53ef\u4ee5\u8003\u8651\u4e00\u70b9\u7f8e\u5316\u7684\u4e8b\u4e86\u3002<\/p>\n<p>\u9996\u5148\uff0c\u628aindex.html\u4fee\u4e00\u4fee\uff0c\u52a0\u5165\u4e00\u4e9b\u6837\u5f0f\uff0c\u52a0\u5165\u8c03\u8282\u5b57\u4f53\u5927\u5c0f\u7684\u6587\u672c\u6846\u3001\u9ed1\u591c\u6a21\u5f0f\u5f00\u5173\u4ee5\u53ca\u8f93\u5165\u9644\u52a0CSS\u7684\u6587\u672c\u6846\u3002\u8fd8\u8981\u52a0\u5165\u62d6\u62fdmd\u6587\u4ef6\u7684\u529f\u80fd\uff0c\u907f\u514d\u6bcf\u6b21\u90fd\u6253\u5f00\u90a3\u4e2a\u5f31\u667a\u4e00\u6837\u7684\u6587\u4ef6\u5bf9\u8bdd\u6846\u3002<\/p>\n<pre class=\"code\">&lt;!DOCTYPE html&gt;\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n    &lt;title&gt;MD2IMG&lt;\/title&gt;\r\n    &lt;link rel=\"stylesheet\" type=\"text\/css\" href=\"\/static\/style.css\"&gt;\r\n    &lt;link rel=\"icon\" type=\"image\/x-ico\" href=\"\/static\/logo.ico\" \/&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n    &lt;div class=\"container\"&gt;\r\n        &lt;h1&gt;Markdown to IMG&lt;\/h1&gt;\r\n        &lt;form action=\"\/upload\" method=\"post\" enctype=\"multipart\/form-data\"&gt;\r\n            &lt;div class=\"file-drop-area\"&gt;\r\n                &lt;span class=\"custom-file-button\"&gt;\u9009\u62e9\/\u62d6\u62fd.md\u6587\u4ef6&lt;\/span&gt;\r\n                &lt;input type=\"file\" name=\"file\" class=\"file-input\" accept=\".md\" required&gt;\r\n            &lt;\/div&gt;\r\n\r\n            &lt;input type=\"text\" name=\"width\" placeholder=\"\u5bbd\u5ea6\uff08\u9ed8\u8ba4600px\uff09\"&gt;\r\n            &lt;input type=\"text\" name=\"font_size\" placeholder=\"\u5b57\u53f7\uff08\u9ed8\u8ba424px\uff09\"&gt;\r\n\r\n            &lt;div class=\"switch-container\"&gt;\r\n                &lt;label class=\"switch-label\"&gt;\u9ed1\u591c\u6a21\u5f0f&lt;\/label&gt;\r\n                &lt;label class=\"switch\"&gt;\r\n                    &lt;input type=\"checkbox\" name=\"mode\" checked&gt;\r\n                    &lt;span class=\"slider round\"&gt;&lt;\/span&gt;\r\n                &lt;\/label&gt;\r\n            &lt;\/div&gt;\r\n\r\n            &lt;textarea name=\"custom_css\" placeholder=\"\u81ea\u5b9a\u4e49CSS\"&gt;&lt;\/textarea&gt;\r\n\r\n            &lt;input type=\"submit\" value=\"\u5bfc\u51fa\u56fe\u7247\"&gt;\r\n        &lt;\/form&gt;\r\n    &lt;\/div&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n&lt;script&gt;\r\nconst fileInput = document.querySelector('.file-input');\r\nconst dropArea = document.querySelector('.file-drop-area');\r\nconst fileButton = dropArea.querySelector('.custom-file-button');\r\n\r\nfunction updateDropArea(file) {\r\n\tfileButton.textContent = file.name; \/\/ \u66f4\u65b0\u6587\u672c\u4e3a\u6587\u4ef6\u540d\r\n\tdropArea.style.backgroundColor = '#cce5ff'; \/\/ \u66f4\u6539\u4e3a\u4eae\u84dd\u8272\u80cc\u666f\r\n}\r\n\r\ndropArea.addEventListener('dragover', (event) =&gt; {\r\n\tevent.stopPropagation();\r\n\tevent.preventDefault();\r\n\tevent.dataTransfer.dropEffect = 'copy';\r\n});\r\n\r\ndropArea.addEventListener('dragleave', (event) =&gt; {\r\n\tdropArea.style.backgroundColor = '#f8f9fa'; \/\/ \u6062\u590d\u539f\u59cb\u80cc\u666f\u8272\r\n});\r\n\r\ndropArea.addEventListener('drop', (event) =&gt; {\r\n\tevent.stopPropagation();\r\n\tevent.preventDefault();\r\n\tconst files = event.dataTransfer.files;\r\n\tfileInput.files = files;\r\n\tif (files.length &gt; 0) {\r\n\t\tupdateDropArea(files[0]);\r\n\t}\r\n});\r\n\r\nfileInput.addEventListener('change', () =&gt; {\r\n\tif (fileInput.files.length &gt; 0) {\r\n\t\tupdateDropArea(fileInput.files[0]);\r\n\t}\r\n});\r\n&lt;\/script&gt;\r\n<\/pre>\n<p>\u52a0\u4e0a\u4e00\u4e2a\u80fd\u770b\u7684\u6837\u5f0f\u8868\uff1a<\/p>\n<pre class=\"code\">\/* static\/style.css *\/\r\nbody {\r\n    font-family: 'Arial', sans-serif;\r\n    background-color: #f4f4f9;\r\n    margin: 0;\r\n    padding: 0px 30px 0px;\r\n    display: flex;\r\n    justify-content: flex-start;\r\n    align-items: center;\r\n    height: 100vh;\r\n    flex-direction: column;\r\n}\r\n\r\n.container {\r\n    background: white;\r\n    padding: 10px;\r\n    border-radius: 8px;\r\n    box-shadow: 0 4px 6px rgba(0,0,0,0.1);\r\n    width: 60%; \/* \u8bbe\u7f6e\u5bbd\u5ea6\u4e3a\u5c4f\u5e55\u768460% *\/\r\n    max-width: 800px;\r\n    min-width: 400px;\r\n    margin-top: 2%; \/* \u5728\u9876\u90e8\u6dfb\u52a05%\u7684\u95f4\u8ddd *\/\r\n}\r\n\r\nh1 {\r\n    color: #333;\r\n    text-align: center;\r\n}\r\n\r\nform {\r\n    display: flex;\r\n    flex-direction: column;\r\n}\r\n\r\ninput[type=\"file\"] {\r\n    border: 2px solid #ddd;\r\n    padding: 10px;\r\n    border-radius: 5px;\r\n    margin-bottom: 20px;\r\n    cursor: pointer;\r\n}\r\n\r\ninput[type=\"text\"] {\r\n    border: 2px solid #ddd;\r\n    padding: 10px;\r\n    border-radius: 5px;\r\n    margin-bottom: 20px;\r\n}\r\n\r\ninput[type=\"submit\"] {\r\n    background-color: #007BFF;\r\n    color: white;\r\n    border: none;\r\n    padding: 12px 20px;\r\n    font-size: 16px;\r\n    border-radius: 5px;\r\n    cursor: pointer;\r\n    transition: background-color 0.3s;\r\n}\r\n\r\ninput[type=\"submit\"]:hover {\r\n    background-color: #0056b3;\r\n}\r\n\r\n.file-drop-area {\r\n    position: relative;\r\n    display: flex;\r\n    align-items: center;\r\n    justify-content: center;\r\n    width: 100%;\r\n    height: 50px;\r\n    border: 2px dashed #007BFF;\r\n    border-radius: 5px;\r\n    margin-bottom: 20px;\r\n    background-color: #f8f9fa;\r\n    transition: background-color 0.3s;\r\n}\r\n\r\n.file-drop-area:hover {\r\n    background-color: #e2e6ea;\r\n}\r\n\r\n.file-input {\r\n    position: absolute;\r\n    width: 100%;\r\n    height: 100%;\r\n    cursor: pointer;\r\n    opacity: 0;\r\n}\r\n\r\n.custom-file-button {\r\n    text-align: center;\r\n    pointer-events: none;\r\n    user-select: none;\r\n    padding: 10px;\r\n    font-weight: bold;\r\n    color: #007BFF;\r\n}\r\n\r\n\/* \u5f00\u5173\u6837\u5f0f *\/\r\n.switch {\r\n  position: relative;\r\n  display: inline-block;\r\n  width: 60px;\r\n  height: 34px;\r\n}\r\n\r\n.switch input {\r\n  opacity: 0;\r\n  width: 0;\r\n  height: 0;\r\n}\r\n\r\n.slider {\r\n  position: absolute;\r\n  cursor: pointer;\r\n  top: 0;\r\n  left: 0;\r\n  right: 0;\r\n  bottom: 0;\r\n  background-color: #ccc;\r\n  transition: .4s;\r\n  border-radius: 34px;\r\n}\r\n\r\n.slider:before {\r\n  position: absolute;\r\n  content: \"\";\r\n  height: 26px;\r\n  width: 26px;\r\n  left: 4px;\r\n  bottom: 4px;\r\n  background-color: white;\r\n  transition: .4s;\r\n  border-radius: 50%;\r\n}\r\n\r\ninput:checked + .slider {\r\n  background-color: #2196F3;\r\n}\r\n\r\ninput:checked + .slider:before {\r\n  transform: translateX(26px);\r\n}\r\n\r\ntextarea {\r\n    width: 100%; \/* \u8bbe\u7f6e\u5bbd\u5ea6\u4e3a100%\u4ee5\u586b\u6ee1\u5bb9\u5668 *\/\r\n    height: 200px; \/* \u8bbe\u7f6e\u4e00\u4e2a\u56fa\u5b9a\u9ad8\u5ea6 *\/\r\n    padding: 10px; \/* \u589e\u52a0\u5185\u8fb9\u8ddd *\/\r\n    border: 2px solid #ddd; \/* \u8bbe\u7f6e\u8fb9\u6846 *\/\r\n    border-radius: 5px; \/* \u5706\u89d2\u8fb9\u6846 *\/\r\n    box-sizing: border-box; \/* \u5305\u542b\u8fb9\u6846\u548c\u5185\u8fb9\u8ddd\u5728\u5185\u7684\u603b\u5bbd\u9ad8 *\/\r\n    margin-bottom: 20px; \/* \u5e95\u90e8\u5916\u8fb9\u8ddd *\/\r\n    transition: border-color 0.3s; \/* \u5e73\u6ed1\u8fc7\u6e21\u6548\u679c *\/\r\n    resize:none;\r\n}\r\n\r\ntextarea:focus {\r\n    border-color: #007BFF; \/* \u805a\u7126\u65f6\u8fb9\u6846\u989c\u8272\u53d8\u5316 *\/\r\n}\r\n\r\n.switch-container {\r\n    display: flex;\r\n    align-items: center; \/* \u5782\u76f4\u5c45\u4e2d *\/\r\n    margin-bottom: 20px; \/* \u5e95\u90e8\u5916\u8fb9\u8ddd *\/\r\n}\r\n\r\n\/* \u4e3a\u5f00\u5173\u589e\u52a0\u6807\u7b7e\u6837\u5f0f *\/\r\n.switch-label {\r\n    margin-right: 10px;\r\n    font-size: 16px;\r\n    color: #666;\r\n}\r\n\r\n\/* \u8c03\u6574\u5f00\u5173\u4f4d\u7f6e\u4f7f\u4e4b\u5782\u76f4\u5c45\u4e2d *\/\r\n.switch {\r\n    vertical-align: middle;\r\n    margin-left: 5px;\r\n}\r\n<\/pre>\n<p>\u8fd9\u6837\u4e00\u6765\uff0c\u6211\u4eec\u5c31\u6709\u4e86\u4e00\u4e2a\u6bd4\u8f83\u80fd\u770b\u7684\u9875\u9762\uff0c\u53ef\u4ee5\u5728\u4e0a\u9762\u4fee\u6539\u5b57\u53f7\u3001\u6253\u5f00\u9ed1\u591c\u6a21\u5f0f\uff0c\u751a\u81f3\u4e3a\u6e32\u67d3.md\u6587\u4ef6\u6dfb\u52a0\u81ea\u5b9a\u4e49CSS\u3002\u5f53\u7136\uff0c\u5b9e\u73b0\u8fd9\u4e9b\u529f\u80fd\u7684\u540e\u7aef\u8fd8\u6ca1\u6709\u5b8c\u6210\u3002<\/p>\n<h1>\u4e3a\u6e32\u67d3.md\u6dfb\u52a0\u6837\u5f0f<\/h1>\n<p>\u5b57\u53f7\u3001\u9ed1\u591c\u6a21\u5f0f\u3001\u81ea\u5b9a\u4e49CSS\u672c\u8d28\u4e0a\u90fd\u662f\u4e00\u4ef6\u4e8b\uff1a\u5982\u4f55\u5728\u6e32\u67d3.md\u6587\u4ef6\u65f6\u6dfb\u52a0\u4e00\u4e2a\u6837\u5f0f\u8868\u3002<\/p>\n<p>\u57fa\u672c\u7684\u601d\u8def\u662f\u8fd9\u6837\u7684\uff1a\u51c6\u5907\u4e24\u4e2aCSS\u6587\u4ef6\uff0c\u5206\u522b\u5bf9\u5e94\u767d\u5929\u548c\u9ed1\u591c\u6a21\u5f0f\uff0c\u901a\u8fc7\u83b7\u53d6\u5f00\u5173\u7684\u72b6\u6001\uff0c\u9009\u62e9\u5176\u4e2d\u4e00\u4e2a\u6210\u4e3a\u57fa\u7840css\u3002\u7136\u540e\u5206\u522b\u5c06\u81ea\u5b9a\u4e49CSS\u548c\u5b57\u4f53\u5927\u5c0f\u9644\u52a0\u5728\u5176\u540e\uff0c\u5f62\u6210\u4e00\u4e2a\u6574\u4f53\u7684\u6837\u5f0f\u8868\uff0c\u5c06\u5176\u5d4c\u5165\u751f\u6210\u7684html\u7ed3\u6784\u4e2d\uff0c\u5373\u53ef\u5b9e\u73b0\u5bf9.md\u6587\u4ef6\u6e32\u67d3\u6837\u5f0f\u7684\u63a7\u5236\u3002<\/p>\n<pre class=\"code\">@app.route('\/upload', methods=['POST'])\r\nfile = request.files['file']\r\n    width = request.form.get('width', '600')  # \u83b7\u53d6\u7528\u6237\u6307\u5b9a\u7684\u5bbd\u5ea6\r\n    if width == \"\":\r\n        width = \"600\"\r\n    font_size = request.form.get('font_size', '24')  # \u9ed8\u8ba4\u5b57\u4f53\u5927\u5c0f\u4e3a24px\r\n    if font_size == \"\":\r\n        font_size = \"24\"\r\n\r\n    mode = 'night' if request.form.get('mode') == 'on' else 'day'  # \u6839\u636e\u590d\u9009\u6846\u72b6\u6001\u8bbe\u5b9a\u6a21\u5f0f\r\n    custom_css = request.form.get('custom_css', '')  # \u83b7\u53d6\u7528\u6237\u81ea\u5b9a\u4e49CSS\r\n\r\n    if file:\r\n        content = file.read().decode('utf-8')\r\n        if not content.startswith('\\n'):\r\n            content = \"\\n\"+content\r\n        if mode == 'day':\r\n            css = get_css(\"default_light.css\")\r\n        else:\r\n            css = get_css(\"default_night.css\")\r\n\r\n        combined_css = css + custom_css + f\"body {{font-size:{font_size}px;}}\"\r\n        markdown = mistune.create_markdown(plugins=['strikethrough','footnotes','table','url','task_lists','def_list','abbr','mark','insert','superscript','subscript','math','ruby','spoiler'])\r\n        html = markdown(content)\r\n        # HTML\u6dfb\u52a0\u6837\u5f0f\r\n        html = f'&lt;html&gt;&lt;head&gt;&lt;style&gt;{combined_css}&lt;\/style&gt;&lt;\/head&gt;&lt;body&gt;&lt;div style=\"width: {width}px;\"&gt;{html}&lt;\/div&gt;&lt;\/body&gt;&lt;\/html&gt;'\r\n        \/* \u6e32\u67d3\u751f\u6210\u56fe\u7247 *\/\r\n<\/pre>\n<h1>\u6dfb\u52a0.md\u5b9e\u65f6\u9884\u89c8<\/h1>\n<p>\u4e3a\u4e86\u9694\u79bb\u9875\u9762CSS\u5bf9md\u9884\u89c8\u7684\u5f71\u54cd\uff0c\u5728\u9875\u9762\u4e2d\u52a0\u5165\u4e00\u4e2aiframe\u6846\u67b6\uff0c\u7528\u4e8e\u76db\u653e\u6839\u636emd\u751f\u6210\u7684html\u5185\u5bb9\uff1a<\/p>\n<pre class=\"code\">&lt;div style=\"padding-top:40px\" id=\"preview-area\"&gt;\r\n    &lt;iframe id=\"markdown-preview\"&gt;&lt;\/iframe&gt;\r\n&lt;\/div&gt;\r\n<\/pre>\n<p>\u540c\u65f6\u52a0\u5165\u76f8\u5e94\u7684js\u4ee3\u7801\uff0c\u5f53\u6587\u4ef6\/\u5bbd\u5ea6\/\u5b57\u53f7\/\u9ed1\u591c\u6a21\u5f0f\/\u81ea\u5b9a\u4e49CSS\u7b49\u5185\u5bb9\u53d1\u751f\u53d8\u52a8\u7684\u65f6\u5019\uff0c\u5c06\u4e00\u5e94\u4fe1\u606f\u63d0\u4ea4\u5230\u540e\u7aef\uff0c\u518d\u5c06\u8fd4\u56de\u7684html\u5d4c\u5165\u5230iframe\u5143\u7d20\u4e2d\uff0c\u540c\u65f6\uff0c\u8c03\u6574iframe\u5143\u7d20\u7684\u5bbd\u5ea6\u3002<\/p>\n<pre class=\"code\">\/\/ \u6e32\u67d3\u9884\u89c8\r\ndocument.addEventListener('DOMContentLoaded', function() {\r\nconst fileInput = document.querySelector('input[type=\"file\"]');\r\nconst inputs = document.querySelectorAll('input, textarea');\r\nconst previewFrame = document.getElementById('markdown-preview'); \/\/ iframe\u5143\u7d20\r\n\r\nlet markdownText = '';  \/\/ \u5b58\u50a8\u8bfb\u53d6\u7684Markdown\u6587\u672c\r\n\r\nfileInput.addEventListener('change', function() {\r\n    const file = fileInput.files[0];\r\n    if (file) {\r\n        const reader = new FileReader();\r\n        reader.onload = function(e) {\r\n            markdownText = e.target.result;\r\n            console.log(\"Markdown text loaded:\", markdownText); \/\/ \u8c03\u8bd5\u8f93\u51fa\r\n            updatePreview(); \/\/ \u66f4\u65b0\u9884\u89c8\r\n        };\r\n        reader.readAsText(file);\r\n    }\r\n});\r\n        \r\nasync function updatePreview() {\r\n    const formData = new FormData();\r\n    formData.append('markdown_text', markdownText);\r\n    formData.append('width', document.querySelector('input[name=\"width\"]').value);\r\n    formData.append('font_size', document.querySelector('input[name=\"font_size\"]').value);\r\n    formData.append('mode', document.querySelector('input[name=\"mode\"]').checked ? 'on' : 'off');\r\n    formData.append('custom_css', document.querySelector('textarea[name=\"custom_css\"]').value);\r\n\r\n    console.log(\"Attempting to send data to server.\"); \/\/ \u786e\u8ba4\u53d1\u9001\u524d\u7684\u8c03\u8bd5\u8f93\u51fa\r\n\r\n    const response = await fetch('\/preview', {\r\n        method: 'POST',\r\n        body: formData\r\n    });\r\n\r\n    if (response.ok) {\r\n        const text = await response.text();\r\n        const blob = new Blob([text], {type: 'text\/html'});\r\n        const url = URL.createObjectURL(blob);\r\n        previewFrame.style.width = '0px';  \/\/ \u91cd\u7f6e\u5bbd\u5ea6\r\n        previewFrame.style.height = '0px'; \/\/ \u91cd\u7f6e\u9ad8\u5ea6\r\n        previewFrame.src = url; \/\/ \u8bbe\u7f6eiframe\u7684src\u5c5e\u6027\r\n        previewFrame.onload = function() {\r\n            setTimeout(() =&gt; {\r\n                try {\r\n                    const body = previewFrame.contentWindow.document.body;\r\n                    const html = previewFrame.contentWindow.document.documentElement;\r\n                    const height = Math.max(body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight);\r\n                    const width = Math.max(body.scrollWidth, body.offsetWidth, html.clientWidth, html.scrollWidth, html.offsetWidth);\r\n                    previewFrame.style.height = height + 'px';\r\n                    previewFrame.style.width = width + 'px'; \/\/ \u8bbe\u7f6e\u5bbd\u5ea6\r\n                } catch (e) {\r\n                    console.error('Error adjusting iframe dimensions:', e);\r\n                }\r\n            }, 0);\r\n        };\r\n    } else {\r\n        previewFrame.src = \"data:text\/html,Error loading preview\";\r\n        console.error(\"Failed to fetch preview:\", response.statusText); \/\/ \u8c03\u8bd5\u8f93\u51fa\u9519\u8bef\r\n    }\r\n}\r\n\r\ninputs.forEach(input =&gt; input.addEventListener('change', updatePreview));\r\n});\r\n\r\n<\/pre>\n<p>\u5728\u540e\u7aefapp.py\u4e2d\u52a0\u5165\u76f8\u5e94\u7684\u8def\u7531\uff0c\u5904\u7406\u63d0\u4ea4\u7684\u6570\u636e\uff0c\u8fd4\u56dehtml\u3002<\/p>\n<pre class=\"code\">\r\n@app.route('\/preview', methods=['POST'])\r\ndef preview():\r\n    markdown_text = request.form.get('markdown_text', 'Default markdown text if not received')\r\n    width = request.form.get('width', '600')  \r\n    if width == \"\":\r\n        width = \"600\"\r\n    font_size = request.form.get('font_size', '24')\r\n    if font_size == \"\":\r\n        font_size = \"24\"\r\n    mode = 'night' if request.form.get('mode') == 'on' else 'day'\r\n    custom_css = request.form.get('custom_css', '')\r\n    if mode == 'day':\r\n        css = get_css(\"default_light.css\")\r\n    else:\r\n        css = get_css(\"default_night.css\")\r\n    \r\n    combined_css = css + custom_css + f\"body {{font-size:{font_size}px;}}\"\r\n\r\n    # \u6e32\u67d3Markdown\u4e3aHTML\r\n    markdown = mistune.create_markdown(plugins=['strikethrough','footnotes','table','url','task_lists','def_list','abbr','mark','insert','superscript','subscript','math','ruby','spoiler'])\r\n    html = markdown(markdown_text)\r\n    # \u8fd4\u56de\u5b8c\u6574\u7684HTML\u9875\u9762\u5185\u5bb9\r\n    return f&#39;&lt;html&gt;&lt;head&gt;&lt;style&gt;{combined_css}&lt;\/style&gt;&lt;\/head&gt;&lt;body&gt;&lt;div&nbsp;style=&quot;width:&nbsp;{width}px;&quot;&gt;{html}&lt;\/div&gt;&lt;\/body&gt;&lt;\/html&gt;&#39;\r\n<\/pre>\n<p>\u6253\u5305\u8fd0\u884c\uff0c\u5373\u5f97\u5230\u4e86\u4e00\u4e2a\u4e1d\u6ed1\u7684MD\u8f6c\u56fe\u7247\u5de5\u5177\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u8a00 \u8d77\u56e0\u662f\u6211\u60f3\u6574\u7406\u4e00\u4e0b\u4eca\u5e74\u770b\u7684\u957f\u7247\uff0c\u53d1\u4e2a\u957f\u5fae\u535a\uff0c\u51c6\u5907\u7a0d\u5fae\u4e0a\u70b9\u683c\u5f0f\uff0c\u4e8e\u662f\u7814\u7a76\u4e86\u4e00\u4e0b\u5728\u7ebfmd\u7f16\u8f91\u5668\uff08\u672c\u7740\u6298\u817e\u7684\u7cbe [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1669,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[173],"tags":[181,180,160],"class_list":["post-1633","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tech","tag-docker","tag-markdown","tag-160"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/blogsalt.trollbar.xyz\/wp-content\/uploads\/2024\/06\/DALL\u00b7E-2024-06-13-13.22.42-A-modern-abstract-style-painting-with-bright-colors-in-a-flat-rectangular-format.-The-artwork-depicts-a-man-contemplating-how-to-transform-an-HTML-do.webp","_links":{"self":[{"href":"https:\/\/blogsalt.trollbar.xyz\/index.php\/wp-json\/wp\/v2\/posts\/1633","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogsalt.trollbar.xyz\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogsalt.trollbar.xyz\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogsalt.trollbar.xyz\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blogsalt.trollbar.xyz\/index.php\/wp-json\/wp\/v2\/comments?post=1633"}],"version-history":[{"count":18,"href":"https:\/\/blogsalt.trollbar.xyz\/index.php\/wp-json\/wp\/v2\/posts\/1633\/revisions"}],"predecessor-version":[{"id":1695,"href":"https:\/\/blogsalt.trollbar.xyz\/index.php\/wp-json\/wp\/v2\/posts\/1633\/revisions\/1695"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blogsalt.trollbar.xyz\/index.php\/wp-json\/wp\/v2\/media\/1669"}],"wp:attachment":[{"href":"https:\/\/blogsalt.trollbar.xyz\/index.php\/wp-json\/wp\/v2\/media?parent=1633"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogsalt.trollbar.xyz\/index.php\/wp-json\/wp\/v2\/categories?post=1633"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogsalt.trollbar.xyz\/index.php\/wp-json\/wp\/v2\/tags?post=1633"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}