From 36c166d10b4a363549348030d4574f709e6bc8f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20M=C3=BCller?= Date: Fri, 25 Sep 2015 22:57:44 +0200 Subject: [PATCH] markdown enabled --- resources/public/index.html | 4 ++-- resources/template.html | 6 ++---- server.js | 10 ++++++++-- src/page.js | 13 +++---------- src/storage.js | 7 ++++--- 5 files changed, 19 insertions(+), 21 deletions(-) diff --git a/resources/public/index.html b/resources/public/index.html index f16b90d..0cd8c05 100644 --- a/resources/public/index.html +++ b/resources/public/index.html @@ -4,7 +4,7 @@ NoteHub — Free Pastebin for One-Off Markdown Publishing - +
@@ -12,7 +12,7 @@

Free and Hassle-free Pastebin for Markdown Notes.


See Demo Note - New Page + New Note
diff --git a/resources/template.html b/resources/template.html index d564c15..c3c4111 100644 --- a/resources/template.html +++ b/resources/template.html @@ -4,13 +4,11 @@ NoteHub — %TITLE% - - +
%CONTENT%
- \ No newline at end of file + diff --git a/server.js b/server.js index 2921b75..56f7555 100644 --- a/server.js +++ b/server.js @@ -1,7 +1,10 @@ var express = require('express'); var page = require('./src/page'); var storage = require('./src/storage'); +var LRU = require("lru-cache"); + var app = express(); +var CACHE = new LRU(30); app.use(express.static(__dirname + '/resources/public')); @@ -11,8 +14,11 @@ app.get('/new', function (req, res) { app.get(/(.*)\??.*/, function (req, res) { var link = req.params["0"].slice(1); - storage.getNote(link).then(note => { - res.send("opening note " + note.text) + if (CACHE.has(link)) res.send(CACHE.get(link)); + else storage.getNote(link).then(note => { + var content = page.build(note); + CACHE.set(link, content); + res.send(content); }); }); diff --git a/src/page.js b/src/page.js index 69550c9..a3d070a 100644 --- a/src/page.js +++ b/src/page.js @@ -1,16 +1,9 @@ -var LRU = require("lru-cache"), - marked = require("marked"), - fs = require("fs"); +var marked = require("marked"); +var fs = require("fs"); -var CACHE = new LRU(30); // create LRU cache of size 30 var template = fs.readFileSync("resources/template.html", "utf-8"); var buildHTML = (title, content) => template .replace("%TITLE%", title) .replace("%CONTENT%", content); -module.exports.build = id => { - if (CACHE.has(id)) return CACHE.get(id); - var content = "This is page " + id; - CACHE.set(id, content); - return content; -}; +module.exports.build = note => buildHTML(note.title, marked(note.text)); diff --git a/src/storage.js b/src/storage.js index 79f0803..f1b5cf4 100644 --- a/src/storage.js +++ b/src/storage.js @@ -28,6 +28,7 @@ var Link = sequelize.define('Link', { Note.hasMany(Link); Link.belongsTo(Note); -module.exports.getNote = linkId => Link.findById(linkId).then(link => { - return Note.findById(link.NoteId); -}); +module.exports.getNote = linkId => { + console.log("resolving note", linkId); + return Link.findById(linkId).then(link => Note.findById(link.NoteId)); +}