diff --git a/gruntfile.js b/gruntfile.js index 58e2eb5..9bb829b 100644 --- a/gruntfile.js +++ b/gruntfile.js @@ -14,7 +14,9 @@ module.exports = function(grunt) { //proxy : "http://someurl.com" openBrowser : true, customPages: { - "/custom": "README.md" + "/custom": "README.md", + "/^\/foo/(\\w+)$/": "README.md", + "/baz/quux": "LICENCE" } } } diff --git a/tasks/libs/CustomPagesMiddleware.js b/tasks/libs/CustomPagesMiddleware.js index d018949..db94741 100644 --- a/tasks/libs/CustomPagesMiddleware.js +++ b/tasks/libs/CustomPagesMiddleware.js @@ -16,14 +16,43 @@ var _ = require("lodash"), * */ var CustomPagesMiddleware = function(basePath, rules){ - basePath = Divhide.Safe.string(basePath); rules = Divhide.Safe.object(rules); + // workaround for https://github.com/divhide/node-divhide/issues/39 + var RegExpFormat = /^\/.*\/([gimuy]*)$/; + var isRegExpStr = function (value){ + if(!Divhide.Type.isString(value)){ + return false; + } + + return !!RegExpFormat.exec(value); + }; + + var reducer = function(acc, regExpStr){ + var re = Divhide.Safe.regexp(regExpStr); + var match = Divhide.Safe.string(rules[regExpStr]); + acc.push([re, match]); + return acc; + }; + var regexpRules = Object.keys(rules) + .filter(isRegExpStr) + .reduce(reducer, []); + return function(req, res){ var url = req.url; var relFilePath = Divhide.Safe.string(rules[url]); + // check for possible string regexp + if(!relFilePath){ + for(var i = 0, l = regexpRules.length; i < l; i++){ + if(regexpRules[i][0].test(url)){ + relFilePath = regexpRules[i][1]; + break; + } + } + } + // ignore if no rule is defined if(!relFilePath){ res.emit("next");