@@ -93,67 +93,6 @@ ghostBookshelf.Model = ghostBookshelf.Model.extend({
9393
9494 sanitize : function ( attr ) {
9595 return sanitize ( this . get ( attr ) ) . xss ( ) ;
96- } ,
97-
98- // #### generateSlug
99- // Create a string act as the permalink for an object.
100- generateSlug : function ( Model , base , readOptions ) {
101- var slug ,
102- slugTryCount = 1 ,
103- // Look for a post with a matching slug, append an incrementing number if so
104- checkIfSlugExists = function ( slugToFind ) {
105- var args = { slug : slugToFind } ;
106- //status is needed for posts
107- if ( readOptions && readOptions . status ) {
108- args . status = readOptions . status ;
109- }
110- return Model . findOne ( args , readOptions ) . then ( function ( found ) {
111- var trimSpace ;
112-
113- if ( ! found ) {
114- return when . resolve ( slugToFind ) ;
115- }
116-
117- slugTryCount += 1 ;
118-
119- // If this is the first time through, add the hyphen
120- if ( slugTryCount === 2 ) {
121- slugToFind += '-' ;
122- } else {
123- // Otherwise, trim the number off the end
124- trimSpace = - ( String ( slugTryCount - 1 ) . length ) ;
125- slugToFind = slugToFind . slice ( 0 , trimSpace ) ;
126- }
127-
128- slugToFind += slugTryCount ;
129-
130- return checkIfSlugExists ( slugToFind ) ;
131- } ) ;
132- } ;
133-
134- // Remove URL reserved chars: `:/?#[]@!$&'()*+,;=` as well as `\%<>|^~£"`
135- slug = base . trim ( ) . replace ( / [: \/ \? # \[ \] @ ! $ & ' ( ) * + , ; = \\ % < > \| \^ ~ £ " ] / g, '' )
136- // Replace dots and spaces with a dash
137- . replace ( / ( \s | \. ) / g, '-' )
138- // Convert 2 or more dashes into a single dash
139- . replace ( / - + / g, '-' )
140- // Make the whole thing lowercase
141- . toLowerCase ( ) ;
142-
143- // Remove trailing hyphen
144- slug = slug . charAt ( slug . length - 1 ) === '-' ? slug . substr ( 0 , slug . length - 1 ) : slug ;
145- // Remove non ascii characters
146- slug = unidecode ( slug ) ;
147- // Check the filtered slug doesn't match any of the reserved keywords
148- slug = / ^ ( g h o s t | g h o s t \- a d m i n | a d m i n | w p \- a d m i n | w p \- l o g i n | d a s h b o a r d | l o g o u t | l o g i n | s i g n i n | s i g n u p | s i g n o u t | r e g i s t e r | a r c h i v e | a r c h i v e s | c a t e g o r y | c a t e g o r i e s | t a g | t a g s | p a g e | p a g e s | p o s t | p o s t s | u s e r | u s e r s ) $ / g
149- . test ( slug ) ? slug + '-post' : slug ;
150-
151- //if slug is empty after trimming use "post"
152- if ( ! slug ) {
153- slug = 'post' ;
154- }
155- // Test for duplicate slugs.
156- return checkIfSlugExists ( slug ) ;
15796 }
15897
15998} , {
@@ -236,6 +175,67 @@ ghostBookshelf.Model = ghostBookshelf.Model.extend({
236175
237176 'delete' : function ( ) {
238177 return this . destroy . apply ( this , arguments ) ;
178+ } ,
179+
180+ // #### generateSlug
181+ // Create a string act as the permalink for an object.
182+ generateSlug : function ( Model , base , readOptions ) {
183+ var slug ,
184+ slugTryCount = 1 ,
185+ // Look for a post with a matching slug, append an incrementing number if so
186+ checkIfSlugExists = function ( slugToFind ) {
187+ var args = { slug : slugToFind } ;
188+ //status is needed for posts
189+ if ( readOptions && readOptions . status ) {
190+ args . status = readOptions . status ;
191+ }
192+ return Model . findOne ( args , readOptions ) . then ( function ( found ) {
193+ var trimSpace ;
194+
195+ if ( ! found ) {
196+ return when . resolve ( slugToFind ) ;
197+ }
198+
199+ slugTryCount += 1 ;
200+
201+ // If this is the first time through, add the hyphen
202+ if ( slugTryCount === 2 ) {
203+ slugToFind += '-' ;
204+ } else {
205+ // Otherwise, trim the number off the end
206+ trimSpace = - ( String ( slugTryCount - 1 ) . length ) ;
207+ slugToFind = slugToFind . slice ( 0 , trimSpace ) ;
208+ }
209+
210+ slugToFind += slugTryCount ;
211+
212+ return checkIfSlugExists ( slugToFind ) ;
213+ } ) ;
214+ } ;
215+
216+ // Remove URL reserved chars: `:/?#[]@!$&'()*+,;=` as well as `\%<>|^~£"`
217+ slug = base . trim ( ) . replace ( / [: \/ \? # \[ \] @ ! $ & ' ( ) * + , ; = \\ % < > \| \^ ~ £ " ] / g, '' )
218+ // Replace dots and spaces with a dash
219+ . replace ( / ( \s | \. ) / g, '-' )
220+ // Convert 2 or more dashes into a single dash
221+ . replace ( / - + / g, '-' )
222+ // Make the whole thing lowercase
223+ . toLowerCase ( ) ;
224+
225+ // Remove trailing hyphen
226+ slug = slug . charAt ( slug . length - 1 ) === '-' ? slug . substr ( 0 , slug . length - 1 ) : slug ;
227+ // Remove non ascii characters
228+ slug = unidecode ( slug ) ;
229+ // Check the filtered slug doesn't match any of the reserved keywords
230+ slug = / ^ ( g h o s t | g h o s t \- a d m i n | a d m i n | w p \- a d m i n | w p \- l o g i n | d a s h b o a r d | l o g o u t | l o g i n | s i g n i n | s i g n u p | s i g n o u t | r e g i s t e r | a r c h i v e | a r c h i v e s | c a t e g o r y | c a t e g o r i e s | t a g | t a g s | p a g e | p a g e s | p o s t | p o s t s | u s e r | u s e r s ) $ / g
231+ . test ( slug ) ? slug + '-post' : slug ;
232+
233+ //if slug is empty after trimming use "post"
234+ if ( ! slug ) {
235+ slug = 'post' ;
236+ }
237+ // Test for duplicate slugs.
238+ return checkIfSlugExists ( slug ) ;
239239 }
240240
241241} ) ;
0 commit comments