feat: delete a page
This commit is contained in:
@@ -288,4 +288,29 @@ router.put('/*', (req, res, next) => {
|
||||
})
|
||||
})
|
||||
|
||||
/**
|
||||
* Delete document
|
||||
*/
|
||||
router.delete('/*', (req, res, next) => {
|
||||
if (!res.locals.rights.write) {
|
||||
return res.json({
|
||||
ok: false,
|
||||
error: lang.t('errors:forbidden')
|
||||
})
|
||||
}
|
||||
|
||||
let safePath = entryHelper.parsePath(req.path)
|
||||
|
||||
entries.remove(safePath, req.user).then(() => {
|
||||
res.json({
|
||||
ok: true
|
||||
})
|
||||
}).catch((err) => {
|
||||
res.json({
|
||||
ok: false,
|
||||
error: err.message
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
module.exports = router
|
||||
|
@@ -388,6 +388,32 @@ module.exports = {
|
||||
})
|
||||
},
|
||||
|
||||
/**
|
||||
* Delete a document
|
||||
*
|
||||
* @param {String} entryPath The current entry path
|
||||
* @param {Object} author The author user object
|
||||
* @return {Promise} Promise of the operation
|
||||
*/
|
||||
remove(entryPath, author) {
|
||||
if (_.isEmpty(entryPath) || entryPath === 'home') {
|
||||
return Promise.reject(new Error(lang.t('errors:invalidpath')))
|
||||
}
|
||||
|
||||
return git.deleteDocument(entryPath, author).then(() => {
|
||||
// Delete old cache version
|
||||
|
||||
let oldEntryCachePath = entryHelper.getCachePath(entryPath)
|
||||
fs.unlinkAsync(oldEntryCachePath).catch((err) => { return true }) // eslint-disable-line handle-callback-err
|
||||
|
||||
// Delete old index entry
|
||||
search.delete(entryPath)
|
||||
|
||||
// Delete entry
|
||||
return db.Entry.deleteOne({ _id: entryPath })
|
||||
})
|
||||
},
|
||||
|
||||
/**
|
||||
* Generate a starter page content based on the entry path
|
||||
*
|
||||
|
@@ -245,6 +245,29 @@ module.exports = {
|
||||
})
|
||||
},
|
||||
|
||||
/**
|
||||
* Delete a document.
|
||||
*
|
||||
* @param {String} entryPath The entry path
|
||||
* @return {Promise<Boolean>} Resolve on success
|
||||
*/
|
||||
deleteDocument(entryPath, author) {
|
||||
let self = this
|
||||
let gitFilePath = entryPath + '.md'
|
||||
|
||||
return this._git.exec('rm', [gitFilePath]).then((cProc) => {
|
||||
let out = cProc.stdout.toString()
|
||||
if (_.includes(out, 'fatal')) {
|
||||
let errorMsg = _.capitalize(_.head(_.split(_.replace(out, 'fatal: ', ''), ',')))
|
||||
throw new Error(errorMsg)
|
||||
}
|
||||
let commitUsr = securityHelper.sanitizeCommitUser(author)
|
||||
return self._git.exec('commit', ['-m', lang.t('git:deleted', { path: gitFilePath }), '--author="' + commitUsr.name + ' <' + commitUsr.email + '>"']).catch((err) => {
|
||||
if (_.includes(err.stdout, 'nothing to commit')) { return true }
|
||||
})
|
||||
})
|
||||
},
|
||||
|
||||
/**
|
||||
* Commits uploads changes.
|
||||
*
|
||||
|
@@ -77,6 +77,8 @@
|
||||
"delete": "Delete",
|
||||
"deletefiletitle": "Delete?",
|
||||
"deletefilewarn": "Are you sure you want to delete",
|
||||
"deletepagewarning": "Are you sure you want to delete this page? This action cannot be undone!",
|
||||
"deletepagetitle": "Delete this page?",
|
||||
"deleteusertitle": "Delete User Account?",
|
||||
"deleteuserwarning": "Are you sure you want to delete this user account? This action cannot be undone!",
|
||||
"discard": "Discard",
|
||||
@@ -113,4 +115,4 @@
|
||||
"placeholder": "Search...",
|
||||
"results": "Search Results"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -12,6 +12,8 @@ block rootNavRight
|
||||
loading-spinner
|
||||
.nav-item
|
||||
if rights.write && pageData.meta.path !== 'home'
|
||||
a.button.is-outlined.is-icon-only(@click='$store.dispatch("modalDeletePage/open")')
|
||||
i.nc-icon-outline.ui-1_trash
|
||||
a.button.is-outlined(v-on:click='$store.dispatch("modalMovePage/open")')
|
||||
i.nc-icon-outline.arrows-1_shuffle-98
|
||||
span= t('nav.move')
|
||||
@@ -83,4 +85,5 @@ block content
|
||||
|
||||
modal-create-page(basepath=pageData.meta.path)
|
||||
modal-move-page(current-path=pageData.meta.path)
|
||||
modal-delete-page(current-path=pageData.meta.path)
|
||||
anchor
|
||||
|
Reference in New Issue
Block a user