wiki/server/helpers/page.js
/**
   * Parse raw url path and make it safe
   */
Parse raw url path and make it safe
Parse raw url path and make it safe
parsePath (rawPath, opts = {}) {
    let pathObj = {
      locale: WIKI.config.lang.code,
      path: 'home',
      private: false,
      privateNS: '',
      explicitLocale: false
    }
    rawPath = _.trim(qs.unescape(rawPath))
    if (_.startsWith(rawPath, '/')) { rawPath = rawPath.substring(1) }
    rawPath = rawPath.replace(unsafeCharsRegex, '')
    if (rawPath === '') { rawPath = 'home' }
    let pathParts = _.filter(_.split(rawPath, '/'), p => {
      p = _.trim(p)
      return !_.isEmpty(p) && p !== '..' && p !== '.'
    })
    if (pathParts[0].length === 1) {
      pathParts.shift()
    }
    if (localeSegmentRegex.test(pathParts[0])) {
      pathObj.locale = pathParts[0]
      pathObj.explicitLocale = true
      pathParts.shift()
    }
    if (opts.stripExt && pathParts.length > 0) {
      const lastPart = _.last(pathParts)
      if (lastPart.indexOf('.') > 0) {
        pathParts.pop()
        const lastPartMeta = path.parse(lastPart)
        pathParts.push(lastPartMeta.name)
      }
    }
    pathObj.path = _.join(pathParts, '/')
    return pathObj
  }
parsePath ( rawPath , opts = { } ) { let pathObj = { locale : WIKI . config . lang . code , path : 'home' , private : false , privateNS : '' , explicitLocale : false } rawPath = _ . trim ( qs . unescape ( rawPath ) ) if ( _ . startsWith ( rawPath , '/' ) ) { rawPath = rawPath . substring ( 1 ) } rawPath = rawPath . replace ( unsafeCharsRegex , '' ) if ( rawPath === '' ) { rawPath = 'home' } let pathParts = _ . filter ( _ . split ( rawPath , '/' ) , p => { p = _ . trim ( p ) return ! _ . isEmpty ( p ) && p !== '..' && p !== '.' } ) if ( pathParts [ 0 ] . length === 1 ) { pathParts . shift ( ) } if ( localeSegmentRegex . test ( pathParts [ 0 ] ) ) { pathObj . locale = pathParts [ 0 ] pathObj . explicitLocale = true pathParts . shift ( ) } if ( opts . stripExt && pathParts . length > 0 ) { const lastPart = _ . last ( pathParts ) if ( lastPart . indexOf ( '.' ) > 0 ) { pathParts . pop ( ) const lastPartMeta = path . parse ( lastPart ) pathParts . push ( lastPartMeta . name ) } } pathObj . path = _ . join ( pathParts , '/' ) return pathObj }
Parse raw url path and make it safe
Parse raw url path and make it safe
// Clean Path // Extract Info // Strip extension