Skip to content

Commit

Permalink
change logs fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Song Zheng committed Mar 8, 2020
1 parent 1da433d commit ba4ab95
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 19 deletions.
16 changes: 0 additions & 16 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `
<script src="/edit.js"></script>
`
res.render('edit', { data: { name: req.params.name, content, scripts } })
})
})

app.use('/notes', noteViewRouter)
app.use('/api/notes', noteApiRouter)

Expand Down
6 changes: 6 additions & 0 deletions routes/api/notes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
24 changes: 24 additions & 0 deletions routes/views/notes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

5 changes: 2 additions & 3 deletions src/changeLogs.js
Original file line number Diff line number Diff line change
@@ -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 = `
<div class="logContainer list-group">
`
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 + `
<a href='/show/${hash}/${fileName}/edit' class='list-group-item list-group-item-action flex-column align-items-start'>
<a href='/notes/${fileName}/edit/${hash}' class='list-group-item list-group-item-action flex-column align-items-start'>
<div class='d-flex w-100 justify-content-between'>
<h5 class='mb-1'>${hashDisplay}</h5>
<small>${ago}</small>
Expand Down

0 comments on commit ba4ab95

Please sign in to comment.