From df9ebc770643ab7cc34fe975ba113620e5b1c3e2 Mon Sep 17 00:00:00 2001 From: Ro Date: Mon, 29 Jun 2020 13:45:35 -0700 Subject: [PATCH] when title changes when editing a page, remove old html and markdown pages --- brain/api/v1/pages.js | 19 +++++++++++++++++++ brain/data/Render.js | 6 +++--- brain/views/page-edit.pug | 2 +- src/com/actions/PageActions.js | 4 ++++ 4 files changed, 27 insertions(+), 4 deletions(-) diff --git a/brain/api/v1/pages.js b/brain/api/v1/pages.js index a66c4cc..d9dcd12 100644 --- a/brain/api/v1/pages.js +++ b/brain/api/v1/pages.js @@ -67,6 +67,25 @@ router.post('/write/:task?', feature_upload, (req, res) => { } body.feature = feature; body.deleted = false; + //if title changes, get rid of a pages with old title + if (body.current_title !== body.slug) { + let path = + moment(body.created).format('YYYY') + '/' + moment(body.created).format('MM'); + + //remove html page + fs.unlink('public/' + path + '/' + body.current_title + '.html') + .then() + .catch(() => { + //console.log('HTML ERROR', err); + }); + + //remove markdown + fs.unlink('content/pages/' + path + '/' + body.current_title + '.md') + .then() + .catch(() => { + //console.log('MD ERROR', err); + }); + } book.editPage(body, body.page_uuid, task, req.session.user) .then(result => { if (result.type === DataEvent.PAGE_ADDED) { diff --git a/brain/data/Render.js b/brain/data/Render.js index d9b9d13..aa2ff73 100644 --- a/brain/data/Render.js +++ b/brain/data/Render.js @@ -123,7 +123,6 @@ export default class Render { }; resolve(response); } - //don't render delete pages //check to see if deleted pages have been renderered and delete them if (page.metadata.layout !== 'index') { fs.unlink( @@ -134,8 +133,8 @@ export default class Render { '.html' ) .then() - .catch(err => { - console.log('ERROR', err); + .catch(() => { + //console.log('ERROR', err); }); } } @@ -160,6 +159,7 @@ export default class Render { for (let i = 0; i < pages.length; i++) { let page = pages[i]; + //TODO: filter for deleted and unpublished pages if (_.includes(page.metadata.tags, tag.tag_name)) { pageList.push({ title: page.metadata.title, diff --git a/brain/views/page-edit.pug b/brain/views/page-edit.pug index 98b36ac..1f30294 100644 --- a/brain/views/page-edit.pug +++ b/brain/views/page-edit.pug @@ -1,6 +1,6 @@ extends frame block main-content - #post-edit-index(data-index=id data-uuid=uuid data-layout=layout) + #post-edit-index(data-index=id data-uuid=uuid data-layout=layout data-slug=page.slug) #post-edit-index-wrapper //h2 EDIT =post_title diff --git a/src/com/actions/PageActions.js b/src/com/actions/PageActions.js index 0015881..4eafca9 100644 --- a/src/com/actions/PageActions.js +++ b/src/com/actions/PageActions.js @@ -27,6 +27,10 @@ export default class PostActions { 'layout', document.getElementById('post-edit-index').getAttribute('data-layout') ); + pageInfo.append( + 'current_title', + document.getElementById('post-edit-index').getAttribute('data-slug') + ); pageInfo.append('content', html); pageInfo.append('title', document.getElementById('post_title').value); pageInfo.append(