Skip to content

Commit

Permalink
Added editor on write routes
Browse files Browse the repository at this point in the history
  • Loading branch information
Rackover committed Feb 9, 2020
1 parent 017ab09 commit e6aeb6f
Show file tree
Hide file tree
Showing 19 changed files with 321 additions and 51 deletions.
2 changes: 1 addition & 1 deletion app/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module.exports = function(){
function updateContents(){
logger.debug("Updating contents...")
contents = {
"css": "/style.css",
"css": "/res/css/style.css",
"favicon": "/res/img/favicon.png",
"header": false,
"background": false
Expand Down
54 changes: 40 additions & 14 deletions app/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,58 @@ module.exports = function(port){
app.set('views', './app/views')

const routes = [
"read/*"
{name:"read/*", isProtected: false},
{name:"write/*", isProtected: true}
]

// All routes
app.get('/', function (req, res) {
res.redirect(routes[0])
res.redirect(routes[0].name)
})
for (k in routes){
const route = routes[k]
const route = routes[k].name
const isProtected = routes[k].isProtected
const cleanName = route.replace("/*", "")
app.get('/'+route, async function (req, res, next) {
res.render(cleanName, await require('./routes/'+cleanName+'.js')
(
req,
getPageInfo(req)

// Access denied
if (isProtected && !permissions.canWrite(req)){
res.status(403).send({ auth: false, message: 'This page requires identification.' });
logger.info("Refused access to "+req.path+" to "+req.ip+" because of insufficient permission")
}

// OK 200
else{
res.render(cleanName, await require('./routes/'+cleanName+'.js')
(
req,
getPageInfo(req)
)
)
)
}
})
logger.debug('Registered route '+route);
logger.debug('Registered '+(isProtected?"protected ":"")+'route '+route);
}

const apiRoutes = [
{name:"submit", isProtected: true}
]
for (k in routes){
const route = routes[k].name
const isProtected = routes[k].isProtected
const cleanName = route.replace("/*", "")
app.get('/'+route, async function (req, res, next) {

}
}

// CSS faking route
app.get('/style.css', function (req, res) {
// CSS faking route
const cssPath = '/res/css/style.css';
app.get(cssPath, function (req, res) {
const colors = require("./theme.js")("#AAAADD")
res.set('Content-Type', 'text/css');
res.send(
fs.readFileSync(path.join(APPLICATION_ROOT, "public", "style.css"))
fs.readFileSync(path.join(APPLICATION_ROOT, "public", cssPath))
.toString()
.replace(/darkest_grey/g, colors.darkest_grey.rgb().string())
.replace(/light_grey/g, colors.light_grey.rgb().string())
Expand Down Expand Up @@ -76,7 +101,8 @@ function getPageInfo(req){
links: wikiContents
},
user: {
canWrite: permissions.canWrite(req)
canWrite: permissions.canWrite(req),
ip: req.ip
}
}
}
Expand All @@ -97,6 +123,6 @@ function getFooterInfo(){
function getNavigationInfo(req){
return {
arborescence: wikiMap.getTree(),
current: req.path.replace("/read", ""),
current: req.path.replace(req.path.split("/")[1]+"/", ""),
}
}
3 changes: 3 additions & 0 deletions app/layout/default.pug
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ doctype html
html
head
title #{website.name}
block head
link(rel="stylesheet", href=website.links.css)
link(rel="icon", type="image/png", href=website.links.favicon)
body
.header(style=(website.links.background ? "background-image:url('"+website.links.background+"')" : ""))
.userBar
i #{user.ip}
include elements/header.pug
.mainContainer
.navigation
Expand Down
2 changes: 1 addition & 1 deletion app/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ async function scanDirectory(dirPath){
.replace(":", "/")
.replace("git@", "https://")
.replace(".git", "/blob/"+process.env.GIT_REPO_BRANCH),
cleanName+".md"),
virtualPath),
name: markdown.parseMeta(contents.toString()).title,
children: fs.existsSync(fullPathNoExt) ? await scanDirectory(fullPathNoExt) : false
}
Expand Down
55 changes: 28 additions & 27 deletions app/markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,28 @@
const md = require('markdown-it')
const hljs = require('highlight.js')
const cheerio = require('cheerio')

const mdContainer = require('markdown-it-container')
const mdAnchor = require('markdown-it-anchor')

/*
const mdEmoji = require('markdown-it-emoji')
const mdTaskLists = require('markdown-it-task-lists')
const mdAbbr = require('markdown-it-abbr')
const mdAnchor = require('markdown-it-anchor')
const mdFootnote = require('markdown-it-footnote')
const mdExternalLinks = require('markdown-it-external-links')
const mdExpandTabs = require('markdown-it-expand-tabs')
const mdAttrs = require('markdown-it-attrs')
const mdContainer = require('markdown-it-container')
const mdMathjax = require('markdown-it-mathjax')()
const mathjax = require('mathjax-node')
const mdExpandTabs = require('markdown-it-expand-tabs')
*/

const _ = require('lodash')
const mdRemove = require('remove-markdown')

// containers correspondances
const containers = [
{name: "info", entry:'<blockquote class="is-info"><span class="title"><i class="nc-icon-outline travel_info"></i>Note</span><br>\n', exit: '</blockquote>\n'},
{name: "warning", entry:'<blockquote class="is-warning"><span class="title"><i class="nc-icon-outline ui-2_alert-circle"></i>Warning</span><br>\n', exit: '</blockquote>\n'},
{name: "danger", entry:'<blockquote class="is-danger"><span class="title"><i class="nc-icon-outline ui-2_alert-circle-"></i>Danger</font></span><br>\n', exit: '</blockquote>\n'}
{name: "info", entry:'<blockquote class="info"><p class="title">Note</p>', exit: '</blockquote>\n'},
{name: "warning", entry:'<blockquote class="warning"><p class="title">⚠️ Warning</p>', exit: '</blockquote>\n'},
{name: "danger", entry:'<blockquote class="danger"><p class="title">Danger</p>', exit: '</blockquote>\n'}
];

// Load plugins
Expand All @@ -45,16 +45,30 @@ var mkdown = md({
return '<pre><code>' + str + '</code></pre>'
}
})
.use(mdAnchor, {
slugify: _.kebabCase,
permalink: true,
permalinkClass: 'toc-anchor icon-link2',
permalinkSymbol: ''
})

for (let k in containers){
const container = containers[k];
mkdown.use(mdContainer, container.name, {
render: function (tokens, idx) {
if (tokens[idx].nesting === 1) {
return container.entry+"\n";
} else {
return container.exit+"\n";
}
}
});
}

/*
.use(mdEmoji)
.use(mdTaskLists)
.use(mdAbbr)
.use(mdAnchor, {
slugify: _.kebabCase,
permalink: true,
permalinkClass: 'toc-anchor icon-link2',
permalinkSymbol: ''
})
.use(mdFootnote)
.use(mdExternalLinks, {
externalClassName: 'external-link',
Expand All @@ -64,19 +78,6 @@ var mkdown = md({
tabWidth: 4
})
.use(mdAttrs);
for (let k in containers){
const container = containers[k];
mkdown.use(mdContainer, container.name, {
render: function (tokens, idx) {
if (tokens[idx].nesting === 1) {
return container.entry+"\n";
} else {
return container.exit+"\n";
}
}
});
}
*/

// Rendering rules
Expand Down
7 changes: 6 additions & 1 deletion app/page.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
const fs = require("fs")

module.exports = {
getFormattedContents: getFormattedContents
getFormattedContents: getFormattedContents,
getContents: getContents
}

function getFormattedContents(diskPath){
return markdown.parse(fs.readFileSync(diskPath).toString())
}

function getContents(diskPath){
return fs.readFileSync(diskPath).toString()
}
7 changes: 5 additions & 2 deletions app/permissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ module.exports = {
canWrite: canWrite
}

const whitelist = ["2a01:cb0c:882:1400:30e6:9ecb:e3c:7aeb"]
const whitelist = [
"2a01:cb0c:882:1400:30e6:9ecb:e3c:7aeb", // 970 HOME VAL
"2a01:cb0c:882:1400:ccb7:504:79dc:cc0", // TWONK HOME VAL
]

function canWrite(req){
// Temp, waiting for API support
const exploded = req.ip.split(".");
const exploded = req.ip.replace("::ffff:", "").split(".");
return exploded[0] === "192" && exploded[1] === "168" && exploded[2] === "1"
|| whitelist.includes(req.ip);
}
5 changes: 2 additions & 3 deletions app/routes/read.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module.exports = async function (req, response){

response.prefix = "/read";
const pagePath = req.path.replace(response.prefix, "")
const pagePath = response.navigation.current
const page = wikiMap.getPage(pagePath)

if (!page){
Expand All @@ -13,7 +12,7 @@ module.exports = async function (req, response){

const diskPath = page.filePath;

const md = diskPath ? await wikiPage.getFormattedContents(diskPath, req.get('host')) : 404
const md = diskPath ? await wikiPage.getFormattedContents(diskPath) : 404

response.meta = md.meta
response.data = md.html
Expand Down
23 changes: 23 additions & 0 deletions app/routes/write.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module.exports = async function (req, response){

const pagePath = response.navigation.current
const page = wikiMap.getPage(pagePath)

if (!page){
response.error = {
data: "404"
};
return response;
}

const diskPath = page.filePath;

const data = diskPath ? await wikiPage.getContents(diskPath) : 404

response.data = data
response.hierarchy = wikiMap.getHierarchyInfo(pagePath)
response.page = page

return response;
}

2 changes: 1 addition & 1 deletion app/views/read.pug
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ block content
.pageTitle #{meta.title}
.pageToolbar
if (user.canWrite)
a.pageEditButton(href="/edit"+navigation.current) 🖋️ Edit...
a.pageEditButton(href="/write"+navigation.current) 🖋️ Edit...
a.pageEditButton(onclick="return confirm('YOU ARE GOING TO DELETE "+navigation.current+". Are you SURE you want to do this?')", href="/delete"+navigation.current) ❌ Delete...
a.pageEditButton(href=page.repoPath, target="_blank") 🐙 View on github

Expand Down
26 changes: 26 additions & 0 deletions app/views/write.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
extends ../layout/default.pug

block head
// Editor
link(rel="stylesheet", href="/res/css/simplemde.min.css")
link(rel="stylesheet", href="/res/css/editor.css")
script(src="/res/js/simplemde.min.js")
script(src="/res/js/editor.js")


block content
if (hierarchy.length > 1)
.pageAddress
each val, index in hierarchy
a(href=prefix+val.url, class=(val.url === page.url ? "selected" : "")) #{val.name}
if (val.url != page.url)
span 🡆
div
textarea#editor
| !{data}
//
.pageToolbar
if (user.canWrite)
a.pageEditButton(href="/edit"+navigation.current) 🖋️ Edit...
a.pageEditButton(onclick="return confirm('YOU ARE GOING TO DELETE "+navigation.current+". Are you SURE you want to do this?')", href="/delete"+navigation.current) ❌ Delete...
a.pageEditButton(href=page.repoPath, target="_blank") 🐙 View on github
10 changes: 10 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
"description": "",
"main": "app.js",
"dependencies": {
"body-parser": "^1.19.0",
"cheerio": "^1.0.0-rc.3",
"color": "^3.1.2",
"express": "^4.17.1",
"git-wrapper2-promise": "^0.2.9",
"highlight.js": "^9.18.0",
"lodash": "^4.17.15",
"markdown-it": "^10.0.0",
"markdown-it-anchor": "^5.2.5",
"markdown-it-container": "^2.0.0",
"pug": "^2.0.4",
"remove-markdown": "^0.3.0",
"winston": "^3.2.1",
Expand Down
20 changes: 20 additions & 0 deletions public/res/css/editor.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
a.saveButton:after{
content: "Save";
font-family: "Noto sans";
margin-left: 6px;
}

a.saveButton{
width:auto;
border:1px dotted grey;
padding-left: 6px;
padding-right: 6px;
background:linear-gradient(#00DD44, #006622);
color:white !important;
font-weight:bold;
}

a.saveButton:hover{
border:1px solid grey;
background:linear-gradient(#00FF55, #00AA66);
}
Loading

0 comments on commit e6aeb6f

Please sign in to comment.