VSCode任务配置:如何在mkdocs服务就绪后自动打开浏览器
解决MkDocs自动启动服务器并同步打开浏览器的时机问题
我用MkDocs编写项目文档,希望自动运行mkdocs serve(编译文档并启动本地Web服务器),同时自动打开浏览器窗口(使用simpleBrowser.show)。
启动服务器的配置不难,但浏览器的启动时机一直没搞定:
- 立即打开浏览器的话,服务器尚未就绪,页面无法正常显示;
- 将浏览器任务设置为依赖服务器任务,浏览器永远不会打开——因为服务器需要持续运行,不会终止;
- 手动终止服务器后,浏览器虽会打开,但服务器已停止,页面仍无法显示。
我当前的tasks配置如下:
{ "version": "2.0.0", "tasks": [ { "label": "Server", "type": "shell", "command": "mkdocs serve", "group": "none", "presentation": { "reveal": "silent", "panel": "new" }, }, { "label": "Open browser in panel", "command": "${input:openSimpleBrowser}", "problemMatcher": [], "runOptions": { "runOn": "folderOpen" }, "dependsOrder": "sequence", "dependsOn": ["Server"] } ], "inputs": [ { "id": "openSimpleBrowser", "type": "command", "command": "simpleBrowser.show", "args": [ "http://localhost:8000" ] } ] }
我还尝试为Server任务添加问题匹配配置,但没有效果:
"problemMatcher":[ { "base": "$tsc", "background": { "activeOnStart": true, "beginsPattern":"Building documentation", "endsPattern":"Serving" }, }
我判断这些模式应该能匹配服务器输出,但并未成功。
内容的提问来源于stack exchange,提问作者Tadeáš Pilař
相关产品推荐
相关产品推荐

