metalsmith

metalsmith config to read only markdown files


I have a (deeply nested) ./src directory with a mix of various files, including some *.md files.

I want Metalsmith to recursively find ONLY the markdown files.

But at the moment the different configs I’ve tried copies ALL files into the static site, or none.

This is what I have at the moment:

{
    "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/collections": { "posts": "*.md" } },
      { "@metalsmith/drafts": true },
      { "@metalsmith/markdown": true },
      { "@metalsmith/permalinks": "posts/:title" }
    ]
  }


Solution

  • Usually, what is in your metalsmith.source() is intended to be copied & processed in full (else you wouldn't put it in metalsmith.source()).

    If you want metalsmith to completely ignore some files you can use metalsmith.ignore(). If you want to use those files, but discard them from the build output, you can use @metalsmith/remove. You can specify a negated glob to remove/ignore all files but markdown: !**/*.md. See it in action

    Mind you that the pattern *.md will only match markdown files at the root of the source, if you want to match all markdown files in subfolders too, you should specify **/*.md. See it in action

    With that, your config becomes:

    {
        "source": "./src",
        "destination": "static-site",
        "clean": true,
        "ignore": ["!**/*.md"],
        "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/collections": { "posts": "**/*.md" } },
          { "@metalsmith/drafts": true },
          { "@metalsmith/markdown": true },
          { "@metalsmith/permalinks": {
              "relative": false,
              "pattern": "posts/:title"
          }
        ]
      }
    

    PS: it is highly likely that you want @metalsmith/permalinks ' relative option to be false.