Răsfoiți Sursa

Improve page path detection

Sotaro KARASAWA 10 ani în urmă
părinte
comite
4371f6573a
1 a modificat fișierele cu 12 adăugiri și 1 ștergeri
  1. 12 1
      lib/util/middlewares.js

+ 12 - 1
lib/util/middlewares.js

@@ -34,7 +34,18 @@ exports.swigFilters = function(app, swig) {
   return function(req, res, next) {
 
     swig.setFilter('path2name', function(string) {
-      return string.replace(/.+\/(.+)?$/, '$1');
+      var name = string;
+      if (string.match(/.+\/([^/]+\/\d{4}\/\d{2}\/\d{2})$/)) { // /.../hoge/YYYY/MM/DD 形式のページ
+        return string.replace(/.+\/([^/]+\/\d{4}\/\d{2}\/\d{2})$/, '$1');
+      }
+      if (string.match(/.+\/([^/]+\/\d{4}\/\d{2})$/)) { // /.../hoge/YYYY/MM 形式のページ
+        return string.replace(/.+\/([^/]+\/\d{4}\/\d{2})$/, '$1');
+      }
+      if (string.match(/.+\/([^/]+\/\d{4})$/)) { // /.../hoge/YYYY 形式のページ
+        return string.replace(/.+\/([^/]+\/\d{4})$/, '$1');
+      }
+
+      return string.replace(/.+\/(.+)?$/, '$1'); // ページの末尾を拾う
     });
 
     swig.setFilter('datetz', function(input, format) {