You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何配置Metalsmith以递归读取src目录中的仅Markdown文件?

解决Metalsmith递归处理仅Markdown文件的问题

你的核心问题有两个:一是当前配置只匹配根目录的Markdown文件,无法递归子目录;二是默认Metalsmith会复制所有源文件到目标目录,导致非MD文件也被输出。以下是修正后的配置和关键说明:

修正后的配置

首先确保安装了@metalsmith/ignore插件(如果没装,执行npm install @metalsmith/ignore),然后修改配置为:

{
  "source": "./src",
  "destination": "static-site",
  "clean": true,
  "metadata": {
    "sitename": "My Static Site & Blog",
    "siteurl": "https://example.com/",
    "description": "It's about saying »Hello« to the world.",
    "generatorname": "Metalsmith",
    "generatorurl": "https://metalsmith.io/"
  },
  "plugins": [
    { "@metalsmith/ignore": ["!**/*.md"] },
    { "@metalsmith/collections": { "posts": "**/*.md" } },
    { "@metalsmith/drafts": true },
    { "@metalsmith/markdown": true },
    { "@metalsmith/permalinks": "posts/:title" }
  ]
}

关键修改点说明

  • 过滤非MD文件:@metalsmith/ignore插件的["!**/*.md"]规则表示「仅保留所有子目录下的.md文件」,!是取反操作,**/*.md递归匹配所有层级的Markdown文件,这样非MD文件会被直接排除,不会进入后续处理或被复制到目标目录。
  • 递归匹配MD文件:将@metalsmith/collections的posts匹配模式从*.md改为**/*.md,这样能捕获src目录下所有嵌套层级的Markdown文件,纳入posts集合统一处理。
  • 插件顺序:@metalsmith/ignore必须放在插件列表最前面,确保先过滤文件,再执行collections、markdown等后续处理逻辑。

内容的提问来源于stack exchange,提问作者Ashley Coolman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 05:40:42