metalsmith / in-place

A metalsmith plugin for in-place templating
MIT License
57 stars 27 forks source link

Transforming NJK and MD with filename of index.md.njk markdown not processing #161

Closed jcrawford closed 5 years ago

jcrawford commented 5 years ago

I am not quite sure why this is happening but the output from debugging has shown this

  metalsmith-in-place rendering index.md.njk +0ms
  metalsmith-in-place rendering njk extension for index.md.njk +1ms
  metalsmith-in-place last extension reached, replacing last extension with html +1ms
  metalsmith-in-place rendering md extension for index.md.njk +8ms
  metalsmith-in-place done rendering index.md.njk, renamed to index.html +1ms

I am initiating metalsmith-in-place like so.

const templateConfig = {
  engineOptions: {
      highlight: utils.highlight,
      filters: {
          date: dateFilter
      }
  }
};

// in ms build code
.use(inplace(templateConfig))

The source of the index.md.njk is the following

---
title: A Developers Blog
layout: homepage.njk
---

The source for homepage.njk layout is this

{% extends "layout.njk" %}

{% block body %}
    ## My Contents

    {% for post in tags['technology'] %}
        ### {{ post.title }}
    {% endfor %}
{% endblock %}

The generated output for index.html is the following:

    ## My Contents

        ### Hello World

        ### Foo Bar

        ### Another Test

        ### Another Post
ismay commented 5 years ago

What happens if you change index.md.njk to index.njk.md?

ismay commented 5 years ago

Oh wait. The markdown is in a layout. Are you running in-place before layouts?

jcrawford commented 5 years ago

Yes in place is run before layouts

/*jslint es6 */

/*
 * Metalsmith build file
 * Build site with `node build`
 */

'use strict';

const devBuild = ((process.env.NODE_ENV || '').trim().toLowerCase() !== 'production')

const metalsmith    = require('metalsmith');
const serve         = require('metalsmith-serve');
const watch         = require('metalsmith-watch');
const layouts       = require('metalsmith-layouts');
const assets        = require('metalsmith-assets');
const inplace       = require('metalsmith-in-place');
const collections   = require('metalsmith-collections');
const debug         = (devBuild) ? require('metalsmith-debug-ui') : null;
const excerpts      = require('metalsmith-excerpts');
const paginate      = require('metalsmith-pager');
const publish       = require('metalsmith-publish');
const htmlmin       = (devBuild) ? null : require(' ');
const tags          = require('metalsmith-tags');

/* Local Plugins */
const utils = require('./lib/utils');
const articles = require('./lib/articles');
const permalinks = require('./lib/my-permalinks');
const search = (devBuild) ? null : require('./lib/search');

var dateFilter = require('nunjucks-date-filter');
dateFilter.setDefaultFormat('MMMM d, Y');

const templateConfig = {
  engineOptions: {
      highlight: utils.highlight,
      filters: {
          date: dateFilter
      }
  }
};

let ms = metalsmith(__dirname);

if(devBuild) debug.patch(ms);

    ms.metadata({
      site: {
        name: 'A Developers Blog',
        author: 'Joseph Crawford',
        description: 'A Developers Blog'
      }
    })
    .clean(true)
    .source('./src/')
    .destination('./build/')
    .use(collections({
      posts: {
        pattern: 'posts/**/*.njk',
        sortBy: 'date',
        reverse: true,
        refer: false
      },
      articles: {
        pattern: ['articles/*.njk', 'articles/**/*.njk'],
        refer: false
      },
      reviews: {
        pattern: ['reviews/*.njk', 'reviews/**/*.njk'],
        sortBy: 'date',
        reverse: true,
        refer: false
      }
    }))
    .use(tags({
      "handle": "tags",
      "path": "topics/:tag.html",
      "layout": "/tag.njk",
      /* Can also use deprecated template property.
      "template": "/partials/tag.hbt",
      */
      "sortBy": "date",
      "reverse": true,
      "skipMetadata": false,
      "slug": {
        "mode": "rfc3986"
      }
    }))
    .use(articles({
      collection: 'articles'
    }))
    .use(excerpts())
    .use(paginate({
      collection: 'posts',
      elementsPerPage: 1,
      pagePattern: 'page/:PAGE/index.html',
      index: 'index.html',
      paginationTemplatePath: '../layouts/partials/paginator.njk',
      layoutName: 'category.njk'
    }))
    .use(publish())
    .use(inplace(templateConfig));

    if(!devBuild) {
      ms.use(search({
        projectId: 'LCIGKBLVSG',
        privateKey: 'c490aa2a9f619c422d2ef63d2004d5c3',
        index: 'jcrawford.github.io',
        clearIndex: true,
        collections: ['posts', 'reviews', 'articles']
      }));
    }

    ms.use(layouts(templateConfig))
    .use(permalinks({
      relative: false,
      pattern: ":title",
      // each linkset defines a match, and any other desired option
      linksets: [{
        match: { collection: 'posts' },
        pattern: 'posts/:date/:title',
      },{
        match: { collection: 'articles' },
        pattern: 'articles/:series.name/:title'
      },{
        match: { collection: 'reviews' },
        pattern: 'reviews/:date/:title'
      }]
    }))
    .use(assets({
      source: __dirname + '/assets/',
      destination: '.'
    }));

    if(!devBuild) {
      ms.use(htmlmin());
    }

    ms.use(serve({
      port: 8080,
      verbose: true,
      http_error_files: {
        404: "/404/index.html"
      }
    }))
    .use(watch({
      paths: {
        "${source}/**/*": true,
        "lib/**/*": "**/*",
        "assets/**/*": "**/*",
        "layouts/**/*": "**/*"
      }
    }))
    .build(function (err) {
        if (err) {
          console.log(err);
            throw err;
        }
        console.log('Build finished!');
    });
ismay commented 5 years ago

Yeah that’s the problem. When in-place runs the file is empty as the layout hasn’t been applied yet.

I’ll close this as it’s not a bug or feature request.

jcrawford commented 5 years ago

So I have to run layouts before in place? Everything I have seen has shown in place before layouts?

jcrawford commented 5 years ago

Running layouts before in place gives me html that looks like this

<p>&lt;!doctype html&gt;
&lt;html dir=&quot;ltr&quot; lang=&quot;en-US&quot;&gt;
    &lt;head&gt;</p>

<pre><code> &lt;meta property=&quot;og:type&quot; content=&quot;blog&quot; /&gt;
    &lt;meta property=&quot;og:title&quot; content=&quot;A Developers Blog - A Developers Blog&quot; /&gt;
    &lt;meta property=&quot;og:url&quot; content=&quot;&quot; /&gt;

    &lt;title&gt;A Developers Blog - A Developers Blog&lt;/title&gt;

    &lt;meta http-equiv=&quot;content-type&quot; content=&quot;text/html; charset=UTF-8&quot; /&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1, maximum-scale=1&quot; /&gt;
    &lt;meta name=&quot;description&quot; content=&quot;Blog &amp;amp; Magazine Theme&quot;&gt;

It also still leads to this in the debugging output

metalsmith-in-place rendering njk extension for index.md.njk +0ms
  metalsmith-in-place last extension reached, replacing last extension with html +5ms
  metalsmith-in-place rendering md extension for index.md.njk +0ms
  metalsmith-in-place done rendering index.md.njk, renamed to index.html +3ms
jcrawford commented 5 years ago

I see the issue, I had markdown in my layout file but the layout file was named homepage.njk so it was missing the md extension. I also think it is best practice to keep the md out of the layout file but I may be wrong.