blakeembrey / metalsmith-pagination

A Metalsmith plugin for paginating arrays and collections
MIT License
34 stars 10 forks source link

Contents writing fully templated data? #28

Closed patdavid closed 7 years ago

patdavid commented 7 years ago

I've run into an odd problem here. I've got a very simple project setup, using only (in this order):

Metalsmith(__dirname)

    .metadata({
        sitename: "Site Name",
        siteurl: "siteurl"
    })
    .source('./src')
    .destination('./build')
    .clean(true)
    .use(collections({
        posts: 'posts/**/*.md',
        sortBy: 'date',
        reverse: true
    }))
    .use(markdown())
    .use(permalinks({
        pattern: ':date/:title',
        date: 'YYYY/MM',
        relative: false
    }))
    .use( paginate({
        'collections.posts': {
            perPage: 10,
            layout: 'main.hbt',
            first: 'index.html',
            path: ':num.html'
        }
    }))
    .use( layouts({
        engine: 'handlebars',
        directory: 'templates'
    }))
    .build( function(err) {
        if (err) throw err;
    });

The template for paginated files looks like this:

<!DOCTYPE html>
<html lang='en'>
    <head>
        <title>{{{ sitename }}}</title>
    </head>
    <body>
        {{#each pagination.files}}
        <article>
            <h1 class='post.title'><a href="/{{path}}">{{title}}</a></h1>
            <!-- CONTENTS -->
            {{{ contents }}}
            <!-- /CONTENTS -->
        </article>
        <hr>
        {{/each}}

    </body>
</html>

I am expecting that {{{ contents }}} writes only the contents of each post, but it appears to be writing the fully templated version of each post.

In the html output, I expected:

<!DOCTYPE html>
<html lang='en'>
    <head>
        <title>Site Name</title> 
    </head>
    <body>
        <article>
            <h1 class='post.title'><a href="/2015/01/test-deux-2015">Test Deux (2015)</a></h1>
            <!-- CONTENTS -->
                    <h2>Test Deux (2015)</h2>
                    <div>
                        <h2 id="second-heading-2015-">SEcond heading (2015)</h2>
                        <p>some paragraph stuff</p>
                    </div>
            <!-- /CONTENTS -->
        </article>
    </body>
</html>

Instead I am seeing what appears to be the fully templated contents (notice the extra html elements):

<!DOCTYPE html>
<html lang='en'>
    <head>
        <title>Site Name</title> 
    </head>
    <body>
        <article>
            <h1 class='post.title'><a href="/2015/01/test-deux-2015">Test Deux (2015)</a></h1>
            <!-- CONTENTS -->
            <html lang='en'>
            <body class='postpage-from-post-layouts'>
                    <h2>Test Deux (2015)</h2>
                    <div>
                        <h2 id="second-heading-2015-">SEcond heading (2015)</h2>
                        <p>some paragraph stuff</p>
                    </div>
            </body>
            </html>
            <!-- /CONTENTS -->
        </article>
    </body>
</html>
blakeembrey commented 7 years ago

Try logging out the file so you can see if there's another property you can use to access the pre-rendered content. This is actually expected, as is, because this plugin doesn't render anything. It just generates the pages with the correct metadata and your layouts function is rendering the page itself. Layouts is rendering the other pages before the pages this plugin has added.

If there's no existing pre-layout content property, you'll need to add your own plugin to move contents to another property before render and use that in your pagination template.

patdavid commented 7 years ago

@blakeembrey ty for the quick response.

I just noticed that subsequent rebuilds exhibit random behavior that seems to jibe with what your saying. Previously I had stored this content as part of the metalsmith-more plugin and used that to output the paginated content. I think that may be a better path and will hopefully solve this.

Thanks!