Search code examples
handlebars.jsmetalsmith

Metalsmith-collection not finding markdown files


I have a static website using handlebars and metalsmith. I can create a collection called carriers from my metalsmith config file but the plugin pattern ignores the markdown files so carriers is always empty

My JS file has metalsmith config as follows

      ...//other metalsmith plugins
      .use(
        collections({
          carriers: { pattern: '**/*.md' }
        })
      )
      .use(markdown())
      .use(
        layouts({
          engine: 'handlebars',
          directory: './src/layouts',
          partials: './src/partials'
        })
      )
      ...//other metalsmith plugins

And my carriers.hbs contains the following

    <section id="carriers" class="integrations">
      <div class="cards">
        {{log collections}}
        {{#each collections.carriers }}
          <h5>{{this.title}}</h5>
        {{/each}}
      </div>
    </section>

The {{log collections}} Handlebars built-in helper logs this { carriers: [ metadata: undefined ] } and the generated html file looks like this

<main>
  <section id="carriers" class="integrations">
    <div class="cards">

    </div>
  </section>
</main>

So what am I missing here?


Solution

  • Update
    So I got more experience with Metalsmith and now got to know why the markdown files were not available. This is because of the Metalsmith.source('my-directory') did not contain the markdown files and they were in another folder.


    Sadly, I don't have a solution for this because my project was too big to track down with my little experience in Metalsmith. However, the main points that I believe causing the problems and to watch out for are

    1. Internationalization plugin structure is important to keep in mind how it works with other plugins
    2. The order of plugins in the build process (e.g. markdown() must be before layouts()) otherwise you might get an error or unexpected outcomes
    3. The pattern of the collections plugin