Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

correctly calculates asset paths for index pages #25

Merged
merged 4 commits into from Oct 13, 2013
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 24 additions & 1 deletion Gruntfile.js
Expand Up @@ -37,7 +37,7 @@ module.exports = function(grunt) {
assemble: {
options: {
plugins: ['./permalinks.js'],
assets: 'test/actual/assets',
assets: 'assets',
layout: 'test/fixtures/default.hbs'
},
// Should not modify dest path.
Expand Down Expand Up @@ -71,6 +71,17 @@ module.exports = function(grunt) {
{expand: true, cwd: 'test/fixtures/pages', src: ['**/*.hbs'], dest: 'test/actual/structure', ext: '.html'}
]
},
// Should use a long date format for the path
dates: {
options: {
permalinks: {
structure: ':YYYY/:MM/:DD/:basename/index:ext'
}
},
files: [
{expand: true, cwd: 'test/fixtures/pages', src: ['**/*.hbs'], dest: 'test/actual/dates', ext: '.html'}
]
},
// Should modify dest path using a built-in property from context
builtin_property_from_context: {
options: {
Expand Down Expand Up @@ -106,6 +117,18 @@ module.exports = function(grunt) {
{expand: true, cwd: 'test/fixtures/pages', src: ['**/*.hbs'], dest: 'test/actual/yfm_custom_property', ext: '.html'}
]
},
// Should modify dest path using a custom property from YAML front matter
assets_path: {
options: {
permalinks: {
// 'slug' is a custom property in YAML front matter
structure: ':slug/index:ext'
}
},
files: [
{expand: true, cwd: 'test/fixtures/pages', src: ['**/*.hbs'], dest: 'test/actual/yfm_custom_property', ext: '.html'}
]
},
// Should modify dest path using a custom replacement pattern
custom_replacement_pattern: {
options: {
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -528,7 +528,7 @@ Released under the MIT license

***

_This file was generated on Saturday, October 12, 2013._
_This file was generated on Sunday, October 13, 2013._


[moment]: http://momentjs.com/ "Moment.js Permalinks"
2 changes: 1 addition & 1 deletion package.json
@@ -1,7 +1,7 @@
{
"name": "permalinks",
"description": "Permalinks plugin for Assemble, the static site generator for Grunt.js, Yeoman and Node.js. This plugin enables powerful and configurable URI patterns, [Moment.js](http://momentjs.com/) for parsing dates, much more.",
"version": "0.1.5",
"version": "0.1.6",
"homepage": "https://github.com/assemble/permalinks",
"author": {
"name": "Jon Schlinkert",
Expand Down
36 changes: 25 additions & 11 deletions permalinks.js
Expand Up @@ -24,26 +24,35 @@ module.exports = function(config, callback) {

'use strict';

var context = config.context;
var grunt = config.grunt;
var context = config.context;
var grunt = config.grunt;

var permalinks = context.permalinks;
var pages = context.pages;
var page = context.page;
var originalAssets = context.originalAssets;

var async = grunt.util.async;
var _ = grunt.util._;

var permalinks = context.permalinks;
var pages = context.pages;
var page = context.page;

var async = grunt.util.async;
var _ = grunt.util._;
var normalizePath = function(str) {
return str.replace(/\\/g, '/');
};


// Skip over the plugin if it isn't defined in the options.
if(!_.isUndefined(permalinks)) {

pages.forEach(function(file) {

if (page.src !== file.src || file.basename === 'index') {
return;
if (file.basename === 'index') {
file.assets = normalizePath(path.relative(path.join(file.dirname, '../'), originalAssets));
}

if (page.src !== file.src) {
return;
}

// Get the permalink pattern to use from options.permalinks.structure.
// If one isn't defined, don't change anything.
Expand Down Expand Up @@ -73,7 +82,8 @@ module.exports = function(config, callback) {

/**
* REPLACEMENT PATTERNS
* Replacement variables for permalink structure.
* Replacement variables for permalink structure.
* Fromat the date from the YAML front matter of a page.
*/
var format = function(date) {
return moment(yfm.date).format(date);
Expand Down Expand Up @@ -191,7 +201,11 @@ module.exports = function(config, callback) {
if(_.isUndefined(permalinks.structure) && _.isUndefined(permalinks.preset)) {
file.dest = file.dest;
} else {
file.dest = path.join(page.dirname, permalink).replace(/\\/g, '/');
if (file.basename === 'index') {
file.dest = file.dest;
} else {
file.dest = normalizePath(path.join(page.dirname, permalink));
}
}
grunt.verbose.ok('Generated permalink to:'.yellow, file.dest);
});
Expand Down
Expand Up @@ -3,6 +3,7 @@
<head>
<meta charset="UTF-8">
<title>Arctic Ice is Really Cold</title>
<link rel="stylesheet" href="../../../../assets/styles.css">
</head>
<body>

Expand Down
1 change: 1 addition & 0 deletions test/actual/built_in_property/articles/index.html
Expand Up @@ -3,6 +3,7 @@
<head>
<meta charset="UTF-8">
<title>Some Category</title>
<link rel="stylesheet" href="../../../assets/styles.css">
</head>
<body>

Expand Down
13 changes: 13 additions & 0 deletions test/actual/built_in_property/foo/index.html
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>foo</title>
<link rel="stylesheet" href="../../../assets/styles.css">
</head>
<body>

<h1>foo</h1>

</body>
</html>
2 changes: 2 additions & 0 deletions test/actual/built_in_property/index.html
Expand Up @@ -3,10 +3,12 @@
<head>
<meta charset="UTF-8">
<title>Home</title>
<link rel="stylesheet" href="../../assets/styles.css">
</head>
<body>

<h1>Home</h1>
<p>This is the home page!</p>

</body>
</html>
13 changes: 13 additions & 0 deletions test/actual/dates/2013/10/02/foo/index.html
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>foo</title>
<link rel="stylesheet" href="../../../assets/styles.css">
</head>
<body>

<h1>foo</h1>

</body>
</html>
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Arctic Ice is Really Cold</title>
<link rel="stylesheet" href="../../../../assets/styles.css">
</head>
<body>

<h1>Arctic Ice is Really Cold</h1>
<p>Suprising discovery! Contrary to popular belief, ice in the arctic tundra was found to be below freezing temperatures.</p>

</body>
</html>
14 changes: 14 additions & 0 deletions test/actual/dates/articles/index.html
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Some Category</title>
<link rel="stylesheet" href="../../../assets/styles.css">
</head>
<body>

<h1>Some Category</h1>
<p>Index page nested in a directory.</p>

</body>
</html>
14 changes: 14 additions & 0 deletions test/actual/dates/index.html
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Home</title>
<link rel="stylesheet" href="../../assets/styles.css">
</head>
<body>

<h1>Home</h1>
<p>This is the home page!</p>

</body>
</html>
Expand Up @@ -3,6 +3,7 @@
<head>
<meta charset="UTF-8">
<title>Arctic Ice is Really Cold</title>
<link rel="stylesheet" href="../../../../assets/styles.css">
</head>
<body>

Expand Down
1 change: 1 addition & 0 deletions test/actual/index_pages/articles/index.html
Expand Up @@ -3,6 +3,7 @@
<head>
<meta charset="UTF-8">
<title>Some Category</title>
<link rel="stylesheet" href="../../../assets/styles.css">
</head>
<body>

Expand Down
13 changes: 13 additions & 0 deletions test/actual/index_pages/foo/index.html
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>foo</title>
<link rel="stylesheet" href="../../../assets/styles.css">
</head>
<body>

<h1>foo</h1>

</body>
</html>
2 changes: 2 additions & 0 deletions test/actual/index_pages/index.html
Expand Up @@ -3,10 +3,12 @@
<head>
<meta charset="UTF-8">
<title>Home</title>
<link rel="stylesheet" href="../../assets/styles.css">
</head>
<body>

<h1>Home</h1>
<p>This is the home page!</p>

</body>
</html>
Expand Up @@ -3,6 +3,7 @@
<head>
<meta charset="UTF-8">
<title>Arctic Ice is Really Cold</title>
<link rel="stylesheet" href="../../../../assets/styles.css">
</head>
<body>

Expand Down
1 change: 1 addition & 0 deletions test/actual/no_opts/articles/index.html
Expand Up @@ -3,6 +3,7 @@
<head>
<meta charset="UTF-8">
<title>Some Category</title>
<link rel="stylesheet" href="../../../assets/styles.css">
</head>
<body>

Expand Down
13 changes: 13 additions & 0 deletions test/actual/no_opts/foo.html
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>foo</title>
<link rel="stylesheet" href="../../../assets/styles.css">
</head>
<body>

<h1>foo</h1>

</body>
</html>
2 changes: 2 additions & 0 deletions test/actual/no_opts/index.html
Expand Up @@ -3,10 +3,12 @@
<head>
<meta charset="UTF-8">
<title>Home</title>
<link rel="stylesheet" href="../../assets/styles.css">
</head>
<body>

<h1>Home</h1>
<p>This is the home page!</p>

</body>
</html>
Expand Up @@ -3,6 +3,7 @@
<head>
<meta charset="UTF-8">
<title>Arctic Ice is Really Cold</title>
<link rel="stylesheet" href="../../../../assets/styles.css">
</head>
<body>

Expand Down
1 change: 1 addition & 0 deletions test/actual/preset/articles/index.html
Expand Up @@ -3,6 +3,7 @@
<head>
<meta charset="UTF-8">
<title>Some Category</title>
<link rel="stylesheet" href="../../../assets/styles.css">
</head>
<body>

Expand Down
13 changes: 13 additions & 0 deletions test/actual/preset/foo/index.html
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>foo</title>
<link rel="stylesheet" href="../../../assets/styles.css">
</head>
<body>

<h1>foo</h1>

</body>
</html>
2 changes: 2 additions & 0 deletions test/actual/preset/index.html
Expand Up @@ -3,10 +3,12 @@
<head>
<meta charset="UTF-8">
<title>Home</title>
<link rel="stylesheet" href="../../assets/styles.css">
</head>
<body>

<h1>Home</h1>
<p>This is the home page!</p>

</body>
</html>
1 change: 1 addition & 0 deletions test/actual/replacement_pattern/articles/index.html
Expand Up @@ -3,6 +3,7 @@
<head>
<meta charset="UTF-8">
<title>Some Category</title>
<link rel="stylesheet" href="../../../assets/styles.css">
</head>
<body>

Expand Down
Expand Up @@ -3,6 +3,7 @@
<head>
<meta charset="UTF-8">
<title>Arctic Ice is Really Cold</title>
<link rel="stylesheet" href="../../../../assets/styles.css">
</head>
<body>

Expand Down