用 AI 把 Markdown 文件夹一键转成静态网站——适合文档、博客、知识库手头有一堆 Markdown 文件想快速生成一个网站。这个工具扫描文件夹AI 自动生成导航、索引页面一键输出静态 HTML。核心代码#!/usr/bin/env python3# md2site.pyimportos,shutilfrompathlibimportPathfromdatetimeimportdatetimefromopenaiimportOpenAIimportmarkdownfromdotenvimportload_dotenv load_dotenv()clientOpenAI(api_keyos.getenv(DEEPSEEK_API_KEY),base_urlhttps://api.deepseek.com/v1)TEMPLATE!DOCTYPE html html langzh-CNheadmeta charsetutf-8 meta nameviewport contentwidthdevice-width,initial-scale1 title{title}/title style *{{margin:0;padding:0;box-sizing:border-box}} body{{font-family:-apple-system,sans-serif;max-width:900px;margin:0 auto;padding:20px;line-height:1.7;color:#334155}} nav a{{color:#3b82f6;text-decoration:none;margin-right:16px;font-size:14px}} nav a:hover{{text-decoration:underline}} h1,h2,h3{{margin:16px 0 8px;color:#1e293b}} pre{{background:#1e293b;color:#e2e8f0;padding:16px;border-radius:8px;overflow-x:auto;font-size:13px}} code{{background:#f1f5f9;padding:2px 6px;border-radius:4px;font-size:13px}} pre code{{background:none;padding:0}} .footer{{margin-top:40px;padding-top:16px;border-top:1px solid #e2e8f0;font-size:12px;color:#94a3b8}} /style/headbody nav{nav}/nav main{content}/main div classfooterGenerated by md2site | {date}/div /body/htmldefbuild_site(srcdocs,out_site):将 Markdown 文件夹转为静态网站。srcPath(src)outPath(out)out.mkdir(exist_okTrue)(out/css).mkdir(exist_okTrue)md_filessorted(src.rglob(*.md))nav_html .join(fa href{f.with_suffix(.html).name}{f.stem}/aforfinmd_files)formd_fileinmd_files:contentmd_file.read_text(encodingutf-8)html_bodymarkdown.markdown(content,extensions[fenced_code,tables])pageTEMPLATE.format(titlemd_file.stem,navnav_html,contenthtml_body,datedatetime.now().strftime(%Y-%m-%d))out_fileout/md_file.with_suffix(.html).name out_file.write_text(page,encodingutf-8)# 生成索引页generate_index(md_files,nav_html,out)print(f✅ 网站已生成到{out.resolve()}({len(md_files)}页))defgenerate_index(md_files,nav_html,out):AI 生成索引页面。titles\n.join(f-{f.stem}forfinmd_files)respclient.chat.completions.create(modeldeepseek-chat,messages[{role:user,content:f根据以下文件列表生成一个简短的网站首页介绍100字内\n{titles}}],temperature0.5,max_tokens200,)introresp.choices[0].message.content links.join(flia href{f.with_suffix(.html).name}{f.stem}/a/liforfinmd_files)indexTEMPLATE.format(title首页,navnav_html,contentfh1 文档中心/h1p{intro}/ph2目录/h2ul{links}/ul,datedatetime.now().strftime(%Y-%m-%d))(out/index.html).write_text(index,encodingutf-8)if__name____main__:build_site(sys.argv[1]iflen(sys.argv)1elsedocs)使用方式pipinstallmarkdown python-dotenv openai python md2site.py ./my-docs# ✅ 网站已生成到 _site/ (12 页)效果_site/ ├── index.html # AI 生成的首页 ├── 快速开始.html ├── API文档.html └── ...每个 Markdown 文件对应一个带导航、代码高亮的页面。打开_site/index.html即可浏览。总结一个轻量的 Markdown 转网站工具几十行代码适合把文档、笔记、知识库快速变成可分享的网页。