Skip to content

Commit 4e19713

Browse files
ajpianoscottgonzalez
authored andcommitted
Add support for assigning WordPress menu_order field based on a YAML file of slugs to optionally control article display order.
1 parent bb9d9f9 commit 4e19713

File tree

1 file changed

+57
-4
lines changed

1 file changed

+57
-4
lines changed

tasks/build.js

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,73 @@ grunt.registerHelper( "wordpress-parse-post-flex", function( path ) {
4545
return grunt.helper( "wordpress-parse-post", path );
4646
});
4747

48+
// Process a YAML order file and return an object of page slugs and their ordinal indices
49+
grunt.registerHelper( "read-order", function( orderFile ) {
50+
var order,
51+
map = {},
52+
index = 0;
53+
54+
try {
55+
order = yaml.load( grunt.file.read( orderFile ) );
56+
} catch( error ) {
57+
grunt.warn( "Invalid order file: " + orderFile );
58+
return null;
59+
}
60+
61+
order.forEach(function(chapter) {
62+
var article, title;
63+
64+
if ( grunt.utils._.isObject( chapter ) ) {
65+
title = Object.keys( chapter )[ 0 ];
66+
map[ title ] = ++index;
67+
68+
chapter[ title ].forEach(function( article ) {
69+
map[ title + "/" + article ] = ++index;
70+
});
71+
} else {
72+
map[ title ] = ++index;
73+
}
74+
});
75+
return map;
76+
});
77+
4878
grunt.registerMultiTask( "build-pages", "Process html and markdown files as pages, include @partials and syntax higlight code snippets", function() {
49-
var content,
79+
var content, orderMap,
5080
task = this,
5181
taskDone = task.async(),
5282
files = this.data,
53-
targetDir = grunt.config( "wordpress.dir" ) + "/posts/page/";
83+
targetDir = grunt.config( "wordpress.dir" ) + "/posts/page/",
84+
orderFile = grunt.config( "wordpress.order" );
85+
86+
if ( orderFile ) {
87+
orderMap = grunt.helper( "read-order", orderFile );
88+
if ( !orderMap ) {
89+
taskDone( false );
90+
return;
91+
}
92+
}
5493

5594
grunt.file.mkdir( targetDir );
5695

5796
grunt.utils.async.forEachSeries( files, function( fileName, fileDone ) {
58-
var post = grunt.helper( "wordpress-parse-post-flex", fileName ),
97+
var menuOrder,
98+
post = grunt.helper( "wordpress-parse-post-flex", fileName ),
5999
content = post.content,
60100
fileType = /\.(\w+)$/.exec( fileName )[ 1 ],
61-
targetFileName = targetDir + fileName.replace( /^.+?\/(.+)\.\w+$/, "$1" ) + ".html";
101+
targetSlug = fileName.replace( /^.+?\/(.+)\.\w+$/, "$1" ),
102+
targetFileName = targetDir + targetSlug + ".html";
103+
104+
// If an order file was specified, set the menuOrder,
105+
// unless the page being processed isn't in the order file,
106+
// in which case it shouldn't be published
107+
if ( orderMap ) {
108+
menuOrder = orderMap[ targetSlug ];
109+
if ( menuOrder ) {
110+
post.menuOrder = menuOrder;
111+
} else {
112+
post.status = "draft";
113+
}
114+
}
62115

63116
grunt.verbose.write( "Processing " + fileName + "..." );
64117
delete post.content;

0 commit comments

Comments
 (0)