Skip to content

Commit

Permalink
Merge pull request #13 from songz/s4
Browse files Browse the repository at this point in the history
refactor
  • Loading branch information
songz authored Mar 8, 2020
2 parents d059fad + 9286411 commit ba30334
Show file tree
Hide file tree
Showing 22 changed files with 437 additions and 308 deletions.
19 changes: 1 addition & 18 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,8 @@ app.get(['/', '/notes'], (req, res) => {
}
}).sort((a, b) => b.mtimeMs - a.mtimeMs)

const content = Buffer.from(JSON.stringify(notesInfo)).toString('base64')
const content = JSON.stringify(notesInfo)
res.render('notes', { data: {
name: 'All Files',
content,
path: 'home'
} })
Expand All @@ -95,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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"setup": "./scripts/setup.sh",
"server": "webpack --config webpack.config.js && node app.js",
"dev": "webpack --config webpack.config.js --watch && nodemon app.js"
"server": "webpack --config webpack.config.js && nodemon app.js",
"dev": "webpack --config webpack.config.js --watch"
},
"keywords": [],
"author": "",
Expand Down
71 changes: 0 additions & 71 deletions public/edit.js

This file was deleted.

4 changes: 4 additions & 0 deletions public/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,7 @@
.resultContainer p {
margin: 0;
}

#rawData {
display: none;
}
44 changes: 0 additions & 44 deletions public/new/index.html

This file was deleted.

27 changes: 0 additions & 27 deletions public/new/index.js

This file was deleted.

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
56 changes: 43 additions & 13 deletions routes/views/notes.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
const express = require('express')
const fs = require('fs')
const md = require('markdown-it')()
const path = require('path')
const {getGit} = require('../../lib/git')
const {renderErrorPage} = require('../../lib/render')

const router = express.Router()

router.get('/new', async (req, res) => {
const scripts = `
<script src="/edit.js"></script>
`
res.render('edit', { data: { name: 'Create New', content: '', scripts, path: 'new' } })
res.render('notes', { data: {
content: '', path: 'new'
} })
})

router.get('/:name', async (req, res) => {
Expand All @@ -20,10 +18,13 @@ router.get('/:name', async (req, res) => {
if (renderErrorPage(err, res)) {
return
}
res.render('note', {
const content = { name: req.params.name }
res.render('notes', {
data: {
name: req.params.name, content: data.toString('base64'), path: 'view'
}
path: 'view',
content: JSON.stringify(content),
rawData: encodeURIComponent(data.toString())
},
})
})
})
Expand Down Expand Up @@ -51,11 +52,40 @@ router.get('/:name/edit', async (req, res) => {
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, path: 'edit' } })
const content = {
name: req.params.name
}
res.render('notes', {
data: {
path: 'edit',
content: JSON.stringify(content),
rawData: encodeURIComponent(data.toString())
}
})
})
})

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())
}
})
})
})

Expand Down
17 changes: 11 additions & 6 deletions public/changeLogs.js → 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 Expand Up @@ -45,7 +44,13 @@ const startLogDisplay = () => {
})
}

const logDisplay = document.querySelector('#logDisplay')
if (logDisplay) {
startLogDisplay()
const setupChangeLogs = () => {
const logDisplay = document.querySelector('#logDisplay')
if (logDisplay) {
startLogDisplay()
}
}

module.exports = {
setupChangeLogs
}
Loading

0 comments on commit ba30334

Please sign in to comment.