8
8
'use strict' ;
9
9
10
10
var path = require ( 'path' ) ;
11
- var through = require ( 'through2' ) ;
12
11
var utils = require ( './utils' ) ;
13
12
14
13
/**
@@ -25,21 +24,24 @@ module.exports = function permalinksPlugin(pattern, config) {
25
24
var args = [ ] . slice . call ( arguments ) ;
26
25
27
26
return function appPlugin ( app ) {
28
- if ( ! app . isApp ) {
27
+ if ( ! utils . isValid ( app , 'assemble-permalinks' ) ) {
29
28
return collectionPlugin . apply ( this , arguments ) ;
30
29
}
31
- app . emit ( 'plugin' , 'assemble-permalinks' ) ;
32
30
33
31
app . define ( 'permalink' , pipeline ( pattern ) ) ;
34
32
35
33
function collectionPlugin ( collection ) {
36
34
if ( collection . isView || collection . isItem ) {
37
35
return viewPlugin . apply ( this , arguments ) ;
38
36
}
39
- collection . emit ( 'plugin' , 'assemble-permalinks' ) ;
37
+
38
+ if ( ! utils . isValid ( collection , 'assemble-permalinks' , [ 'collection' , 'views' ] ) ) {
39
+ return collectionPlugin ;
40
+ }
41
+
40
42
collection . define ( 'permalink' , pipeline ( pattern ) ) ;
41
43
42
- app . onLoad ( / ./ , function ( file , next ) {
44
+ collection . onLoad ( / ./ , function ( file , next ) {
43
45
if ( collection . options . plural !== file . options . collection ) {
44
46
return next ( ) ;
45
47
}
@@ -51,7 +53,10 @@ module.exports = function permalinksPlugin(pattern, config) {
51
53
} ) ;
52
54
53
55
function viewPlugin ( view ) {
54
- view . emit ( 'plugin' , 'assemble-permalinks' ) ;
56
+ if ( ! utils . isValid ( view , 'assemble-permalinks' , [ 'view' , 'item' ] ) ) {
57
+ return ;
58
+ }
59
+
55
60
this . define ( 'permalink' , function ( dest , opts ) {
56
61
if ( typeof dest !== 'string' ) {
57
62
opts = dest ;
@@ -104,7 +109,7 @@ module.exports = function permalinksPlugin(pattern, config) {
104
109
105
110
function pipeline ( pattern ) {
106
111
return function ( viewPattern , data ) {
107
- return through . obj ( function ( view , enc , next ) {
112
+ return utils . through . obj ( function ( view , enc , next ) {
108
113
var structure = viewPattern || pattern ;
109
114
try {
110
115
view . permalink ( structure , data ) ;
0 commit comments