All files / lib/middlewares swigFilters.ts

30.3% Statements 10/33
0% Branches 0/12
20% Functions 2/10
31.25% Lines 10/32

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81              16x 1x       1x                                             1x         1x       1x               1x               1x         1x                       1x      
import { Express } from 'express'
import Crowi from 'server/crowi'
import swig from 'swig'
import swigFilters from 'swig/lib/filters'
import path2name from 'common/functions/path2name'
 
export default (crowi: Crowi, app: Express) => {
  return (req, res, next) => {
    swig.setFilter('path2name', function(path) {
      return path2name(path)
    })
 
    swig.setFilter('normalizeDateInPath', function(path) {
      var patterns = [
        [/20(\d{2})(\d{2})(\d{2})(.+)/g, '20$1/$2/$3/$4'],
        [/20(\d{2})(\d{2})(\d{2})/g, '20$1/$2/$3'],
        [/20(\d{2})(\d{2})(.+)/g, '20$1/$2/$3'],
        [/20(\d{2})(\d{2})/g, '20$1/$2'],
        [/20(\d{2})_(\d{1,2})_(\d{1,2})_?(.+)/g, '20$1/$2/$3/$4'],
        [/20(\d{2})_(\d{1,2})_(\d{1,2})/g, '20$1/$2/$3'],
        [/20(\d{2})_(\d{1,2})_?(.+)/g, '20$1/$2/$3'],
        [/20(\d{2})_(\d{1,2})/g, '20$1/$2'],
      ]
 
      for (var i = 0; i < patterns.length; i++) {
        var mat = patterns[i][0]
        var rep = patterns[i][1]
        if (path.match(mat)) {
          return path.replace(mat, rep)
        }
      }
 
      return path
    })
 
    swig.setFilter('datetz', function(input, format) {
      // timezone
      return swigFilters.date(input, format, app.get('tzoffset'))
    })
 
    swig.setFilter('nl2br', function(string) {
      return string.replace(/\n/g, '<br>')
    })
 
    swig.setFilter('insertSpaceToEachSlashes', function(string) {
      if (string == '/') {
        return string
      }
 
      return string.replace(/\//g, ' / ')
    })
 
    swig.setFilter('removeLastSlash', function(string) {
      if (string == '/') {
        return string
      }
 
      return string.substr(0, string.length - 1)
    })
 
    swig.setFilter('presentation', function(string) {
      // 手抜き
      return string.replace(/[\n]+#/g, '\n\n\n#').replace(/\s(https?.+(jpe?g|png|gif))\s/, '\n\n\n![]($1)\n\n\n')
    })
 
    swig.setFilter('picture', function(user) {
      if (!user) {
        return ''
      }
 
      if (user.image && user.image != '/images/userpicture.png') {
        return user.image
      } else {
        return '/images/userpicture.png'
      }
    })
 
    next()
  }
}