From ba4ab957f2eec7924711b5d6ae243c309e585107 Mon Sep 17 00:00:00 2001 From: Song Zheng Date: Sat, 7 Mar 2020 21:43:14 -0800 Subject: [PATCH] change logs fix --- app.js | 16 ---------------- routes/api/notes.js | 6 ++++++ routes/views/notes.js | 24 ++++++++++++++++++++++++ src/changeLogs.js | 5 ++--- 4 files changed, 32 insertions(+), 19 deletions(-) diff --git a/app.js b/app.js index 753735d..9f5e120 100644 --- a/app.js +++ b/app.js @@ -94,22 +94,6 @@ app.get('/raw/:name', async (req, res) => { }) }) -app.get('/show/:commit/:name/edit', async (req, res) => { - await simpleGit.checkout(req.params.commit) - const name = req.params.name - fs.readFile(`./data/${name}`, (err, data) => { - simpleGit.checkout('master') - if (renderErrorPage(err, res)) { - return - } - const content = data.toString() - const scripts = ` - - ` - res.render('edit', { data: { name: req.params.name, content, scripts } }) - }) -}) - app.use('/notes', noteViewRouter) app.use('/api/notes', noteApiRouter) diff --git a/routes/api/notes.js b/routes/api/notes.js index 51a5592..a2b8358 100644 --- a/routes/api/notes.js +++ b/routes/api/notes.js @@ -41,4 +41,10 @@ router.put('/:note', async (req, res) => { }) }) +router.get('/:name/logs', async (req, res) => { + const simpleGit = getGit() + const logs = await simpleGit.log({ file: req.params.name }) + res.json(logs) +}) + module.exports = router diff --git a/routes/views/notes.js b/routes/views/notes.js index 11aab79..665815b 100644 --- a/routes/views/notes.js +++ b/routes/views/notes.js @@ -65,5 +65,29 @@ router.get('/:name/edit', async (req, res) => { }) }) +router.get('/:name/edit/:commit', async (req, res) => { + const simpleGit = getGit() + await simpleGit.checkout(req.params.commit) + const name = req.params.name + const notePath = `./data/${req.params.name}` + fs.readFile(notePath, (err, data) => { + // checkout master MUST be the FIRST command to never mess up `data` folder + simpleGit.checkout('master') + if (renderErrorPage(err, res)) { + return + } + const content = { + name: req.params.name + } + res.render('notes', { + data: { + path: 'edit', + content: JSON.stringify(content), + rawData: encodeURIComponent(data.toString()) + } + }) + }) +}) + module.exports = router diff --git a/src/changeLogs.js b/src/changeLogs.js index 4de57dd..d475573 100644 --- a/src/changeLogs.js +++ b/src/changeLogs.js @@ -1,20 +1,19 @@ /* global fetch moment */ const startLogDisplay = () => { const [fileName] = window.location.pathname.split('/').splice(2) - fetch(`/api/logs/${fileName}`).then(r => r.json()).then(data => { + fetch(`/api/notes/${fileName}/logs`).then(r => r.json()).then(data => { const commits = data.all || [] if (!commits.length) return let startContent = `
` - console.log('commits', commits) startContent = commits.reduce((acc, commit) => { const { hash, message, date, body } = commit const [dDate, dTime, dDiff] = date.split(' ') const ago = moment(`${dDate} ${dTime}${dDiff}`).fromNow() const hashDisplay = `${hash.substr(0, 20)}...` return acc + ` - +
${hashDisplay}
${ago}