Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/highlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// https://github.com/lepture/nico/blob/master/lib/sdk/highlight.js
'use strict'

var format = require('util').format;
var hl = require('highlight.js');
const format = require('util').format;
const hl = require('highlight.js');


var escape = function(html) {
Expand All @@ -23,7 +23,7 @@ exports.render = function(code, language) {
if (language === 'html') {
language = 'xml';
}
code = hl.highlight(language, code).value;
code = hl.highlight(code, { language }).value;
return format(
'<div class="highlight"><pre><code class="%s">%s</code></pre></div>',
language, code
Expand Down
3 changes: 2 additions & 1 deletion lib/parsers/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ function Page(attrs) {
this.slug = path.basename(fpath, this.ext)
this.path = path.relative(this.site.cwd, fpath).replaceAll(path.sep, '/')
this.title = util.capitalize(this.slug)
this.type = 'pages';

if (this.validFormat && fs.existsSync(fpath)) {
var content = fs.readFileSync(fpath, this.site.encoding)
Expand All @@ -33,7 +34,7 @@ function Page(attrs) {
}
}

this.url = `/${this.path}`.replace(/\/index\.(?:md|html)$/, '')
this.url = `/${this.path}`.replace(/\/index\.(?:md|html)$/, '').replace(/\.md$/, '.html')

this.dest = path.join(this.site.dest, this.site.baseurl.slice(1),
this.path.replace(/\.\w+$/, this.ext == '.md' ? '.html' : this.ext))
Expand Down
1 change: 1 addition & 0 deletions lib/parsers/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ function Post(attrs) {
parseInt(date[2], 10)
)
this.title = util.capitalize(this.slug)
this.type = fpath.includes('_drafts/') ? 'drafts' : 'posts';

if (fs.existsSync(fpath)) {
var content = fs.readFileSync(fpath, this.site.encoding)
Expand Down
82 changes: 47 additions & 35 deletions lib/writers/templated.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const path = require('path')
const yaml = require('yaml-js')
const mkdirp = require('mkdirp')
const debug = require('debug')('darko')
const fs = require('fs')
const fs = require('fs/promises')

const md = require('../markdown')
const engine = require('../liquid')
Expand All @@ -19,7 +19,6 @@ const mkdirpAsync = function(dir) {
})
}


function liquid(tpl, site) {
if (!engine.site) {
engine.registerFileSystem(
Expand All @@ -36,21 +35,21 @@ function markup(page) {
return page
}

var _layouts = {}
const _layouts = {}

async function layout(page, data) {
const site = page.site
const { layout: layoutName } = data || page;
if (!layoutName || layoutName == 'nil') {
return page
}

const site = page.site
// support .html layouts only, for now.
const lpath = path.join(site.cwd, '_layouts', layoutName) + '.html'
let lcache = _layouts[lpath]

if (!lcache) {
let lsource = fs.readFileSync(lpath, site.encoding)
let lsource = await fs.readFile(lpath, site.encoding)
const parts = lsource.split('---')
let matter

Expand All @@ -68,47 +67,60 @@ async function layout(page, data) {
const template = await lcache.promise
const res = await template.render({
site: site,
page: page,
page: {...page, ...data},
content: page.output
})
page.output = res
if (lcache.matter) return layout(page, lcache.matter)
return page
}

function render(page) {
fs.writeFileSync(page.dest, page.output)
async function render(page) {
await fs.writeFile(page.dest, page.output)
debug('Generated ' + page.path)
}

function getDefaults(page) {
const { site } = page;
if (!site.defaults) return;

const defaults = site.defaults.filter(function({ scope }) {
if (!scope) return true;
let match = true;
if (scope.path) {
if (scope.path.includes('*')) {
match = new RegExp(`^${scope.path.replace('*', '.*?')}`).test(page.path);
} else {
const scopePath = scope.path ? scope.path + '/' : '';
match = page.path.startsWith(scopePath) || page.path === scope.path;
}
}
if (scope.type && match) {
match = scope.type === page.type;
}
return match;
})
return Object.assign({}, ...defaults.map(d => d.values));
}

module.exports = function writeTemplated(page) {
module.exports = async function writeTemplated(page) {
debug('Generating ' + page.path)

return mkdirpAsync(path.dirname(page.dest))
.then(function() {
return liquid(page.content, page.site)
})
.then(function(template) {
return template.render({
site: page.site,
page: page
})
})
.then(function(res) {
page.contentWas = page.content
page.content = res
return page
})
.then(markup)
.then(layout)
.then(render)
.then(function() {
page.content = page.contentWas
return page
})
.catch(function(err) {
console.error('Failed to generate ' + page.path + ':')
throw err
try {
await mkdirpAsync(path.dirname(page.dest))
const template = await liquid(page.content, page.site)
const res = await template.render({
site: page.site,
page: page
})
let content = page.content;
page.content = res;
await markup(page)
await layout(page, getDefaults(page))
await render(page)
page.content = content;
return page
} catch (err) {
console.error('Failed to generate ' + page.path + ':')
throw err
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"darko-serve": "bin/darko-serve"
},
"scripts": {
"test": "mocha test/*.js"
"test": "mocha --recursive test"
},
"devDependencies": {
"expect.js": "^0.3.1",
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
11 changes: 11 additions & 0 deletions test/fixture/defaults/_config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
defaults:
- scope:
path: "" # An empty string means all files in the project
values:
layout: default
- scope:
path: "projects" # All files in the projects folder
type: pages
values:
layout: project
author: "Jane Smith"
4 changes: 4 additions & 0 deletions test/fixture/defaults/_layouts/default.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<style>
body { font-family: Arial, sans-serif; margin: 2em; }
</style>
{{ content }}
7 changes: 7 additions & 0 deletions test/fixture/defaults/_layouts/project.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
layout: default
---

<h1>{{ page.title }} - Project Detail</h1>
{{ page.author }}
{{ content }}
7 changes: 7 additions & 0 deletions test/fixture/defaults/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
hunter x hunter

<ul>
{% for page in site.pages %}
<li><a href="{{ page.url }}">{{ page.title }}</a></li>
{% endfor %}
</ul>
4 changes: 4 additions & 0 deletions test/fixture/defaults/projects/bamboo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
---

毛竹 yum yum
92 changes: 0 additions & 92 deletions test/site.test.js

This file was deleted.

4 changes: 2 additions & 2 deletions test/liquid.test.js → test/unit/liquid.test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
'use strict'

var expect = require('expect.js')
var engine = require('..').Liquid
var engine = require('../..').Liquid


function liquid(tpl, data) {
return engine.parseAndRender(tpl, data)
}

describe('Liquid', function() {
describe('test/unit/liquid.test.js', function() {
const timezoneOffset = new Date().getTimezoneOffset();
const timezone = [
timezoneOffset > 0 ? '-' : '+',
Expand Down
30 changes: 30 additions & 0 deletions test/unit/parsers/page.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';

const { Site } = require('../../..');
const expect = require('expect.js');
const path = require('path');

describe('test/unit/parsers/page.test.js', function() {
let site;
before(function() {
site = new Site({ cwd: path.resolve(__dirname, '../../fixture/basic') });
site.parse();
});

describe('page.url', function() {
it('should generate correct url for pages', function() {
expect(site.pages).not.to.be.empty()

const pageLinks = site.pages.map(function(page) {
return page.url
})

// index.md should be parsed as the homepage. That is, a page with its url
// set to empty string because the /index.html part is removed.
expect(pageLinks).to.contain('')

// felis/index.md should be parse as a page with its url set to /felis
expect(pageLinks).to.contain('/felis')
});
});
});
Loading