فهرست منبع

Page name normalizer: Fix spaces around slash

Sotaro KARASAWA 9 سال پیش
والد
کامیت
2d7a57249e
3فایلهای تغییر یافته به همراه23 افزوده شده و 0 حذف شده
  1. 3 0
      lib/models/page.js
  2. 5 0
      lib/routes/page.js
  3. 15 0
      test/models/page.test.js

+ 3 - 0
lib/models/page.js

@@ -342,6 +342,8 @@ module.exports = function(crowi) {
       path = '/' + path;
     }
 
+    path = path.replace(/\/\s+?/g, '/').replace(/\s+\//g, '/');
+
     return path;
   };
 
@@ -384,6 +386,7 @@ module.exports = function(crowi) {
       /^\/user\/[^\/]+\/(bookmarks|comments|activities|pages|recent-create|recent-edit)/, // reserved
       /^\/?https?:\/\/.+$/, // avoid miss in renaming
       /\/{2,}/,             // avoid miss in renaming
+      /\s+\/\s+/,           // avoid miss in renaming
       /.+\/edit$/,
       /.+\.md$/,
       /^\/(installer|register|login|logout|admin|me|files|trash|paste|comments)(\/.*|$)/,

+ 5 - 0
lib/routes/page.js

@@ -307,6 +307,11 @@ module.exports = function(crowi, app) {
       return renderPage(page, req, res);
     }).catch(function(err) {
 
+      const normalizedPath = Page.normalizePath(path);
+      if (normalizedPath !== path) {
+        return res.redirect(normalizedPath);
+      }
+
       // pageShow は /* にマッチしてる最後の砦なので、creatableName でない routing は
       // これ以前に定義されているはずなので、こうしてしまって問題ない。
       if (!Page.isCreatableName(path)) {

+ 15 - 0
test/models/page.test.js

@@ -145,6 +145,7 @@ describe('Page', function () {
       expect(Page.isCreatableName('http://demo.crowi.wiki/user/sotarok/hoge')).to.be.false;
       expect(Page.isCreatableName('https://demo.crowi.wiki/user/sotarok/hoge')).to.be.false;
 
+      expect(Page.isCreatableName('/ the / path / with / space')).to.be.false;
 
       var forbidden = ['installer', 'register', 'login', 'logout', 'admin', 'files', 'trash', 'paste', 'comments'];
       for (var i = 0; i < forbidden.length ; i++) {
@@ -264,4 +265,18 @@ describe('Page', function () {
     });
   });
 
+  describe('Normalize path', function () {
+    context('Normalize', function() {
+      it('should start with slash', function(done) {
+        expect(Page.normalizePath('hoge/fuga')).to.equal('/hoge/fuga');
+        done();
+      });
+
+      it('should trim spaces of slash', function(done) {
+        expect(Page.normalizePath('/ hoge / fuga')).to.equal('/hoge/fuga');
+        done();
+      });
+    });
+  });
+
 });