wikijs-fork/server/helpers/page.js

154 lines
3.9 KiB
JavaScript
Raw Permalink Normal View History

const qs = require('querystring')
const _ = require('lodash')
const crypto = require('crypto')
const path = require('path')
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
// eslint-disable-next-line no-control-regex
const unsafeCharsRegex = /[\x00-\x1f\x80-\x9f\\"|<>:*?]/
const contentToExt = {
markdown: 'md',
asciidoc: 'adoc',
html: 'html'
}
const extToContent = _.invert(contentToExt)
2019-04-27 03:59:35 +00:00
/* global WIKI */
module.exports = {
/**
* Parse raw url path and make it safe
*/
parsePath (rawPath, opts = {}) {
let pathObj = {
2019-06-22 03:39:04 +00:00
locale: WIKI.config.lang.code,
path: 'home',
private: false,
2019-07-02 05:48:19 +00:00
privateNS: '',
explicitLocale: false
}
// Clean Path
rawPath = _.trim(qs.unescape(rawPath))
if (_.startsWith(rawPath, '/')) { rawPath = rawPath.substring(1) }
rawPath = rawPath.replace(unsafeCharsRegex, '')
if (rawPath === '') { rawPath = 'home' }
rawPath = rawPath.replace(/\\/g, '').replace(/\/\//g, '').replace(/\.\.+/ig, '')
2021-12-04 03:30:22 +00:00
// Extract Info
let pathParts = _.filter(_.split(rawPath, '/'), p => {
p = _.trim(p)
return !_.isEmpty(p) && p !== '..' && p !== '.'
})
2018-09-16 22:36:15 +00:00
if (pathParts[0].length === 1) {
pathParts.shift()
}
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
pathParts.shift()
}
// Strip extension
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
},
/**
* Generate unique hash from page
*/
generateHash(opts) {
return crypto.createHash('sha1').update(`${opts.locale}|${opts.path}|${opts.privateNS}`).digest('hex')
},
/**
* 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(', ') : ''],
['editor', page.editorKey],
['dateCreated', page.createdAt]
]
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
}
},
/**
* Check if path is a reserved path
*/
2019-04-27 03:59:35 +00:00
isReservedPath(rawPath) {
const firstSection = _.head(rawPath.split('/'))
if (firstSection.length <= 1) {
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) {
return _.get(contentToExt, contentType, 'txt')
},
/**
* 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('/', ''),
path: result[2]
}
2019-09-27 18:17:12 +00:00
}
return meta
}
}