Skip to content

Commit

Permalink
add gray-matter to pug render
Browse files Browse the repository at this point in the history
  • Loading branch information
lunelson committed May 11, 2018
1 parent 270915c commit 1090838
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 3 deletions.
12 changes: 9 additions & 3 deletions lib/render-pug.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@

// node
const { relative, dirname } = require('path');
const { readFileSync } = require('fs');

// npm
const Pug = require('pug');
const grayMatter = require('gray-matter');

// local
const { pugLogger } = require('./loggers');
Expand All @@ -27,11 +29,15 @@ module.exports = function({srcDir, loggerFn, options}) {
return function(srcFile) {

return new Promise((resolve, reject) => {
Pug.renderFile(
srcFile,
const srcStr = readFileSync(srcFile, 'utf8');
const { data: $page, content, excerpt, isEmpty, empty } = grayMatter(readFileSync(srcFile, 'utf8'));
Pug.render(
content,
Object.assign(pugLocals(), {
pretty: isDev,
basedir: srcDir
basedir: srcDir,
filename: srcFile,
$page
}),
(err, data) => {
if (err) reject(err);
Expand Down
39 changes: 39 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"eslint-plugin-import": "^2.10.0",
"faker": "^4.1.0",
"filter-files": "^0.4.0",
"gray-matter": "^4.0.1",
"image-size": "^0.6.2",
"js-string-escape": "^1.0.1",
"lodash": "^4.17.10",
Expand Down
19 changes: 19 additions & 0 deletions test-src/matter.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
title: this is the title
other: this is the title
slug: home
---

extends /_layout

block body

h1 hello world
p= $page.title
p= $page.other
p= $page.slug

pre: code= pretty
pre: code= basedir
pre: code= filename
pre: code= dirname
18 changes: 18 additions & 0 deletions test/gray-matter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const fs = require('fs');
const path = require('path');
const matter = require('gray-matter');

console.log(path.resolve(__dirname, '../test-src/matter.pug'));

const str = fs.readFileSync(path.resolve(__dirname, '../test-src/matter.pug'), 'utf8');

/*
data
content
excerpt
empty
isEmpty
*/

const { data: srcData, content: srcContent } = matter(str);
console.log({ srcData, srcContent });

0 comments on commit 1090838

Please sign in to comment.