diff --git a/lib/plugins/console/new.js b/lib/plugins/console/new.js index 21103c9f5e..a579434427 100644 --- a/lib/plugins/console/new.js +++ b/lib/plugins/console/new.js @@ -8,10 +8,11 @@ var moment = require('moment'), util = require('../../util'), file = util.file; +// http://en.wikipedia.org/wiki/Filename#Reserved_characters_and_words var escape = function(str){ return str.toString().toLowerCase() .replace(/\s/g, '-') - .replace(/\\|\/|<|>|:|"|\||\?|\*|\#/g, ''); + .replace(/\/|\\|\?|%|\*|:|\||"|<|>|\./g, ''); }; // Default scaffolds diff --git a/lib/plugins/processor/index.js b/lib/plugins/processor/index.js index fc4cfbc72b..2333b7bda2 100644 --- a/lib/plugins/processor/index.js +++ b/lib/plugins/processor/index.js @@ -66,33 +66,12 @@ var getInfoFromFilename = function(str){ return result; }; -// Percent-encoding: http://en.wikipedia.org/wiki/Percent-encoding#Percent-encoding_reserved_characters -var escapeRule = { - ' ': '-', - '!': '%21', - '#': '%23', - '$': '%24', - '&': '%26', - "'": '%27', - '(': '%28', - ')': '%29', - '*': '%2A', - '+': '%2B', - ',': '%2C', - '/': '%2F', - ':': '%3A', - ';': '%3B', - '=': '%3D', - '?': '%3F', - '@': '%40', - '[': '%5B', - ']': '%5D' -}; - +// http://tools.ietf.org/html/rfc3986#section-2.2 +// http://en.wikipedia.org/wiki/Filename#Reserved_characters_and_words var escape = function(str){ - var str = str.toString().replace(/\s|!|#|\$|&|'|\(|\)|\*|\+|,|\/|:|;|\*|\?|@|\[|\]/g, function(match){ - return escapeRule[match]; - }); + var str = str.toString() + .replace(/\s/g, '-') + .replace(/:|\/|\?|#|\[|\]|@|!|\$|&|'|\(|\)|\*|\+|,|;|=|\\|%|<|>|\./g, ''); if (filenameCaps == 1){ str = str.toLowerCase(); @@ -311,7 +290,20 @@ extend.processor.register(/^_posts\/([^_](?:(?!\/_).)*)$/, function(file, callba dbTags.update(data._id, {posts: {$addToSet: _id}}); tags.push(data._id); } else { - dbTags.insert({name: tag, slug: escape(tagMap[tag] || tag), posts: [_id]}, function(tag){ + var slug = escape(tagMap[tag] || tag), + related = dbTags.find({slug: new RegExp('^' + slug)}); + + if (related.length){ + var match = related.last().slug.match(/-(\d+)$/); + + if (match){ + slug += '-' + (parseInt(match[1]) + 1); + } else { + slug += '-1'; + } + } + + dbTags.insert({name: tag, slug: slug, posts: [_id]}, function(tag){ tags.push(tag._id); }); } @@ -344,6 +336,18 @@ extend.processor.register(/^_posts\/([^_](?:(?!\/_).)*)$/, function(file, callba dbCats.update(data._id, {posts: {$addToSet: _id}}); cats.push(data._id); } else { + var related = dbTags.find({slug: new RegExp('^' + slug)}); + + if (related.length){ + var match = related.last().slug.match(/-(\d+)$/); + + if (match){ + slug += '-' + (parseInt(match[1]) + 1); + } else { + slug += '-1'; + } + } + dbCats.insert({name: item, slug: slug, posts: [_id]}, function(cat){ cats.push(cat._id); });