2018-08-19 05:22:59 +00:00
|
|
|
const qs = require('querystring')
|
|
|
|
const _ = require('lodash')
|
2018-09-16 00:58:23 +00:00
|
|
|
const crypto = require('crypto')
|
2019-05-14 04:53:19 +00:00
|
|
|
const path = require('path')
|
2018-08-19 05:22:59 +00:00
|
|
|
|
2019-05-10 02:46:23 +00:00
|
|
|
const localeSegmentRegex = /^[A-Z]{2}(-[A-Z]{2})?$/i
|
2019-04-20 01:08:15 +00:00
|
|
|
|
2019-04-27 03:59:35 +00:00
|
|
|
/* global WIKI */
|
|
|
|
|
2018-08-19 05:22:59 +00:00
|
|
|
module.exports = {
|
|
|
|
/**
|
|
|
|
* Parse raw url path and make it safe
|
|
|
|
*/
|
2019-05-14 04:53:19 +00:00
|
|
|
parsePath (rawPath, opts = {}) {
|
2018-08-19 05:22:59 +00:00
|
|
|
let pathObj = {
|
|
|
|
locale: 'en',
|
|
|
|
path: 'home',
|
|
|
|
private: false,
|
|
|
|
privateNS: ''
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clean Path
|
|
|
|
rawPath = _.trim(qs.unescape(rawPath))
|
|
|
|
if (_.startsWith(rawPath, '/')) { rawPath = rawPath.substring(1) }
|
|
|
|
if (rawPath === '') { rawPath = 'home' }
|
|
|
|
|
|
|
|
// Extract Info
|
|
|
|
let pathParts = _.filter(_.split(rawPath, '/'), p => !_.isEmpty(p))
|
2018-09-16 22:36:15 +00:00
|
|
|
if (pathParts[0].length === 1) {
|
|
|
|
pathParts.shift()
|
|
|
|
}
|
2019-04-20 01:08:15 +00:00
|
|
|
if (localeSegmentRegex.test(pathParts[0])) {
|
2018-09-16 22:36:15 +00:00
|
|
|
pathObj.locale = pathParts[0]
|
2018-08-19 05:22:59 +00:00
|
|
|
pathParts.shift()
|
|
|
|
}
|
2019-05-14 04:53:19 +00:00
|
|
|
|
|
|
|
// Strip extension
|
2019-05-31 16:34:30 +00:00
|
|
|
if (opts.stripExt && pathParts.length > 0) {
|
2019-05-14 04:53:19 +00:00
|
|
|
const lastPart = _.last(pathParts)
|
|
|
|
if (lastPart.indexOf('.') > 0) {
|
|
|
|
pathParts.pop()
|
|
|
|
const lastPartMeta = path.parse(lastPart)
|
|
|
|
pathParts.push(lastPartMeta.name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-19 05:22:59 +00:00
|
|
|
pathObj.path = _.join(pathParts, '/')
|
|
|
|
return pathObj
|
2018-09-16 00:58:23 +00:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Generate unique hash from page
|
|
|
|
*/
|
|
|
|
generateHash(opts) {
|
|
|
|
return crypto.createHash('sha1').update(`${opts.locale}|${opts.path}|${opts.privateNS}`).digest('hex')
|
2019-04-06 23:05:47 +00:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Inject Page Metadata
|
|
|
|
*/
|
|
|
|
injectPageMetadata(page) {
|
|
|
|
let meta = [
|
|
|
|
['title', page.title],
|
|
|
|
['description', page.description],
|
|
|
|
['published', page.isPublished.toString()],
|
|
|
|
['date', page.updatedAt],
|
|
|
|
['tags', '']
|
|
|
|
]
|
|
|
|
switch (page.contentType) {
|
|
|
|
case 'markdown':
|
|
|
|
return '---\n' + meta.map(mt => `${mt[0]}: ${mt[1]}`).join('\n') + '\n---\n\n' + page.content
|
|
|
|
case 'html':
|
|
|
|
return '<!--\n' + meta.map(mt => `${mt[0]}: ${mt[1]}`).join('\n') + '\n-->\n\n' + page.content
|
|
|
|
default:
|
|
|
|
return page.content
|
|
|
|
}
|
2019-04-20 00:45:05 +00:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Check if path is a reserved path
|
|
|
|
*/
|
2019-04-27 03:59:35 +00:00
|
|
|
isReservedPath(rawPath) {
|
|
|
|
const firstSection = _.head(rawPath.split('/'))
|
2019-05-31 16:34:30 +00:00
|
|
|
if (firstSection.length <= 1) {
|
2019-05-13 05:15:27 +00:00
|
|
|
return true
|
|
|
|
} else if (localeSegmentRegex.test(firstSection)) {
|
|
|
|
return true
|
|
|
|
} else if (
|
|
|
|
_.some(WIKI.data.reservedPaths, p => {
|
|
|
|
return p === firstSection
|
|
|
|
})) {
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
2018-08-19 05:22:59 +00:00
|
|
|
}
|
|
|
|
}
|