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
|
2020-01-05 20:04:16 +00:00
|
|
|
const localeFolderRegex = /^([a-z]{2}(?:-[a-z]{2})?\/)?(.*)/i
|
2019-10-15 03:44:37 +00:00
|
|
|
|
|
|
|
const contentToExt = {
|
|
|
|
markdown: 'md',
|
|
|
|
html: 'html'
|
|
|
|
}
|
|
|
|
const extToContent = _.invert(contentToExt)
|
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 = {
|
2019-06-22 03:39:04 +00:00
|
|
|
locale: WIKI.config.lang.code,
|
2018-08-19 05:22:59 +00:00
|
|
|
path: 'home',
|
|
|
|
private: false,
|
2019-07-02 05:48:19 +00:00
|
|
|
privateNS: '',
|
|
|
|
explicitLocale: false
|
2018-08-19 05:22:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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]
|
2019-07-02 05:48:19 +00:00
|
|
|
pathObj.explicitLocale = true
|
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],
|
2020-05-30 20:34:09 +00:00
|
|
|
['tags', page.tags ? page.tags.map(t => t.tag).join(', ') : ''],
|
2020-08-31 15:28:55 +00:00
|
|
|
['editor', page.editorKey],
|
|
|
|
['dateCreated', page.createdAt],
|
2019-04-06 23:05:47 +00:00
|
|
|
]
|
2020-02-25 02:10:43 +00:00
|
|
|
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
|
2020-05-30 20:34:09 +00:00
|
|
|
case 'json':
|
|
|
|
return {
|
|
|
|
...page.content,
|
|
|
|
_meta: _.fromPairs(meta)
|
|
|
|
}
|
2020-02-25 02:10:43 +00:00
|
|
|
default:
|
|
|
|
return page.content
|
2019-04-06 23:05:47 +00:00
|
|
|
}
|
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
|
|
|
|
}
|
2019-09-27 18:17:12 +00:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Get file extension from content type
|
|
|
|
*/
|
|
|
|
getFileExtension(contentType) {
|
2019-10-19 00:23:10 +00:00
|
|
|
return _.get(contentToExt, contentType, 'txt')
|
2019-10-15 03:44:37 +00:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Get content type from file extension
|
|
|
|
*/
|
|
|
|
getContentType (filePath) {
|
|
|
|
const ext = _.last(filePath.split('.'))
|
|
|
|
return _.get(extToContent, ext, false)
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Get Page Meta object from disk path
|
|
|
|
*/
|
|
|
|
getPagePath (filePath) {
|
|
|
|
let fpath = filePath
|
|
|
|
if (process.platform === 'win32') {
|
|
|
|
fpath = filePath.replace(/\\/g, '/')
|
|
|
|
}
|
|
|
|
let meta = {
|
|
|
|
locale: WIKI.config.lang.code,
|
|
|
|
path: _.initial(fpath.split('.')).join('')
|
|
|
|
}
|
|
|
|
const result = localeFolderRegex.exec(meta.path)
|
|
|
|
if (result[1]) {
|
|
|
|
meta = {
|
2020-01-05 20:04:16 +00:00
|
|
|
locale: result[1].replace('/', ''),
|
2019-10-15 03:44:37 +00:00
|
|
|
path: result[2]
|
|
|
|
}
|
2019-09-27 18:17:12 +00:00
|
|
|
}
|
2019-10-15 03:44:37 +00:00
|
|
|
return meta
|
2018-08-19 05:22:59 +00:00
|
|
|
}
|
|
|
|
}
|