Skip to content

Commit

Permalink
Add support for index.html as posts end to match WP blog post locations
Browse files Browse the repository at this point in the history
  • Loading branch information
mixu committed Oct 6, 2014
1 parent 35acab3 commit c8da518
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 9 deletions.
25 changes: 23 additions & 2 deletions bin/ghost-render
Expand Up @@ -112,6 +112,25 @@ glob.stream(config.input + '/**')
md.highlightJs(),
md.convertMd(),

// this would make a good example of why pipeFirst is not always what you want when you
// are going from doing one operation to doing two operations, since it
// would return the first item, which would leave a hanging pipe for the latter item
// ... what you'd want is to pipe into the first item but pipe out of the last item.
// Maybe pipe-iterators.pipe should do the right thing given
// .pipe([ foo, [ first, ..., last], bar)

// make sure that the path extension is adjusted to html before anything else happens

// This creates '/foo.html' style links and files
// pi.mapKey('path', function(p) { return path.dirname(p) + '/' + path.basename(p, path.extname(p)) + '.html'; }),
//
// TODO: document this and make it configurable.
//
// This leaves the '.html' part off from links. When the filename generation in the posts pipeline
// detects no extname, it will append '/index.html', resulting in '/foo/index.html' style output
// which will match the correct output on servers which support a index page.
pi.mapKey('path', function(p) { return path.dirname(p) + '/' + path.basename(p, path.extname(p)); }),

stream.fileToPost(meta, config.input),
stream.parsePublishedAt(),
stream.parseTags(),
Expand Down Expand Up @@ -151,8 +170,10 @@ glob.stream(assetPath + '/**' )
.pipe(pi.forEach(function(obj) { console.log('write theme asset:', obj.path); }))
.pipe(stream.dest());

glob.stream(__dirname + '/public/assets/**' )
var ghostAssetPath = path.normalize(__dirname + '/../public/assets');

glob.stream(ghostAssetPath + '/**' )
.pipe(stream.read())
.pipe(pi.mapKey('path', function(p) { return path.normalize(p.replace(__dirname + '/public/assets/', config.output + '/assets/')); }))
.pipe(pi.mapKey('path', function(p) { return path.normalize(p.replace(ghostAssetPath + '/', config.output + '/assets/')); }))
.pipe(pi.forEach(function(obj) { console.log('write base asset:', obj.path); }))
.pipe(stream.dest());
10 changes: 9 additions & 1 deletion lib/config-shim.js
Expand Up @@ -56,7 +56,7 @@ module.exports = function getConfig(meta) {
// this will become really big
knownPaths = {
home: '/',
rss: '/rss/',
rss: '/rss/index.xml',
api: '/ghost/api/v0.1'
};

Expand Down Expand Up @@ -133,6 +133,14 @@ module.exports = function getConfig(meta) {
// - post - a json object representing a post
// - permalinks - a json object containing the permalinks setting
function urlPathForPost(post, permalinks) {
if (post.relativeUrl) {
return post.relativeUrl;
} else {
console.error('Post object is missing the relativeUrl property! This is expected in ' +
'ghost-render, because we calculate blog urls in advance.');
throw new Error('Post object is missing the relativeUrl property!');
}

var output = '',
tags = {
year: function() { return moment(post.published_at).format('YYYY'); },
Expand Down
18 changes: 14 additions & 4 deletions lib/pipelines.js
Expand Up @@ -12,8 +12,13 @@ module.exports = function(theme, inPath, outPath) {
// render pipeline for individual posts
stream.renderPost(theme.post.bind(theme)),
pi.mapKey('path', function(p) {
p = path.dirname(p) + '/' + path.basename(p, path.extname(p)) + '.html';
return path.normalize(p.replace(inPath, outPath));
p = p.replace(inPath, outPath);
// if the path has no extname, then assume that links also have no extname and
// hence that you should generate /index.html in a folder with the basename
if (path.extname(p) === '') {
p += '/index.html';
}
return path.normalize(p);
}),
pi.forEach(function(obj) { console.log('write post:', obj.path); }),
stream.dest()
Expand All @@ -24,8 +29,13 @@ module.exports = function(theme, inPath, outPath) {
pi.filter(function(post) { return post.page; }),
stream.renderPost(theme.post.bind(theme)),
pi.mapKey('path', function(p) {
p = path.dirname(p) + '/' + path.basename(p, path.extname(p)) + '.html';
return path.normalize(p.replace(inPath, outPath));
p = p.replace(inPath, outPath);
// if the path has no extname, then assume that links also have no extname and
// hence that you should generate /index.html in a folder with the basename
if (path.extname(p) === '') {
p += '/index.html';
}
return path.normalize(p);
}),
pi.forEach(function(obj) { console.log('write page:', obj.path); }),
stream.dest()
Expand Down
5 changes: 3 additions & 2 deletions lib/stream/file-to-post.js
@@ -1,4 +1,5 @@
var pi = require('pipe-iterators');
var pi = require('pipe-iterators'),
path = require('path');

function toBoolean(value) {
switch((value || '').toString()) {
Expand Down Expand Up @@ -26,7 +27,7 @@ module.exports = function(meta, inPath) {
slug: function(val, file) { return file.post_name; },
// more: (these are needed for schema isPost)
markdown: 'placeholder',
relativeUrl: function(val, file) { return file.path.replace(inPath, '/'); },
relativeUrl: function(val, file) { return path.normalize(file.path.replace(inPath, '/')); },
// extra metadata
draft: function(val) { return toBoolean(val); },
page: function(val) { return toBoolean(val); }
Expand Down

0 comments on commit c8da518

Please sign in to comment.