2018-07-30 02:23:33 +00:00
|
|
|
const fs = require('fs-extra')
|
|
|
|
const path = require('path')
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get file extension based on content type
|
|
|
|
*/
|
|
|
|
const getFileExtension = (contentType) => {
|
|
|
|
switch (contentType) {
|
|
|
|
case 'markdown':
|
|
|
|
return 'md'
|
|
|
|
case 'html':
|
|
|
|
return 'html'
|
|
|
|
default:
|
|
|
|
return 'txt'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Inject page metadata into contents
|
|
|
|
*/
|
|
|
|
const injectMetadata = (page) => {
|
|
|
|
let meta = [
|
|
|
|
['title', page.title],
|
|
|
|
['description', page.description]
|
|
|
|
]
|
|
|
|
let metaFormatted = ''
|
|
|
|
switch (page.contentType) {
|
|
|
|
case 'markdown':
|
|
|
|
metaFormatted = meta.map(mt => `[//]: # ${mt[0]}: ${mt[1]}`).join('\n')
|
|
|
|
break
|
|
|
|
case 'html':
|
|
|
|
metaFormatted = meta.map(mt => `<!-- ${mt[0]}: ${mt[1]} -->`).join('\n')
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
metaFormatted = meta.map(mt => `#WIKI ${mt[0]}: ${mt[1]}`).join('\n')
|
|
|
|
break
|
|
|
|
}
|
|
|
|
return `${metaFormatted}\n\n${page.content}`
|
|
|
|
}
|
|
|
|
|
2018-07-08 05:12:43 +00:00
|
|
|
module.exports = {
|
2018-07-22 20:25:39 +00:00
|
|
|
async activated() {
|
2018-07-30 02:23:33 +00:00
|
|
|
// not used
|
2018-07-08 05:12:43 +00:00
|
|
|
},
|
2018-07-22 20:25:39 +00:00
|
|
|
async deactivated() {
|
2018-07-30 02:23:33 +00:00
|
|
|
// not used
|
2018-07-08 05:12:43 +00:00
|
|
|
},
|
2018-07-22 20:25:39 +00:00
|
|
|
async init() {
|
2019-02-03 22:08:06 +00:00
|
|
|
WIKI.logger.info('(STORAGE/DISK) Initializing...')
|
2018-07-30 02:23:33 +00:00
|
|
|
await fs.ensureDir(this.config.path)
|
2019-02-03 22:08:06 +00:00
|
|
|
WIKI.logger.info('(STORAGE/DISK) Initialization completed.')
|
2018-07-08 05:12:43 +00:00
|
|
|
},
|
2019-02-03 22:08:06 +00:00
|
|
|
async sync() {
|
|
|
|
// not used
|
|
|
|
},
|
|
|
|
async created(page) {
|
|
|
|
WIKI.logger.info(`(STORAGE/DISK) Creating file ${page.path}...`)
|
|
|
|
const filePath = path.join(this.config.path, `${page.path}.${getFileExtension(page.contentType)}`)
|
|
|
|
await fs.outputFile(filePath, injectMetadata(page), 'utf8')
|
2018-07-08 05:12:43 +00:00
|
|
|
},
|
2019-02-03 22:08:06 +00:00
|
|
|
async updated(page) {
|
|
|
|
WIKI.logger.info(`(STORAGE/DISK) Updating file ${page.path}...`)
|
|
|
|
const filePath = path.join(this.config.path, `${page.path}.${getFileExtension(page.contentType)}`)
|
|
|
|
await fs.outputFile(filePath, injectMetadata(page), 'utf8')
|
2018-07-08 05:12:43 +00:00
|
|
|
},
|
2019-02-03 22:08:06 +00:00
|
|
|
async deleted(page) {
|
|
|
|
WIKI.logger.info(`(STORAGE/DISK) Deleting file ${page.path}...`)
|
|
|
|
const filePath = path.join(this.config.path, `${page.path}.${getFileExtension(page.contentType)}`)
|
2018-07-30 02:23:33 +00:00
|
|
|
await fs.unlink(filePath)
|
2018-07-08 05:12:43 +00:00
|
|
|
},
|
2019-02-03 22:08:06 +00:00
|
|
|
async renamed(page) {
|
|
|
|
WIKI.logger.info(`(STORAGE/DISK) Renaming file ${page.sourcePath} to ${page.destinationPath}...`)
|
|
|
|
const sourceFilePath = path.join(this.config.path, `${page.sourcePath}.${getFileExtension(page.contentType)}`)
|
|
|
|
const destinationFilePath = path.join(this.config.path, `${page.destinationPath}.${getFileExtension(page.contentType)}`)
|
2018-07-30 02:23:33 +00:00
|
|
|
await fs.move(sourceFilePath, destinationFilePath, { overwrite: true })
|
2018-07-08 05:12:43 +00:00
|
|
|
}
|
|
|
|
}
|