Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added new features #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ Display a short help message.
Listen on <port> rather than 8888. The default port can be changed
from 8888 by setting the PORT environment variable.

`-r`
`--readonly`
Disable read/write operations from the web interface

###Examples

`nodewiki`
Expand Down
24 changes: 14 additions & 10 deletions lib/getDir.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var fs = require("fs");
var allowedExtensions = require("./allowedExtensions");
var nodewiki = require("../nodewiki");

// get contents of current directory
function getDir(directory){
Expand Down Expand Up @@ -44,6 +45,19 @@ function parseLinks(dir, dirDepth){

var mdLinks = "";

mdLinks += '<div id="tri_buttons">';
if (nodewiki.readonly == false)
mdLinks += '<a href="#" id="new_file">New File</a>';
if (dirDepth > 0){
mdLinks += '<a href="#" id="go_back">Go Back</a>';
} else {
mdLinks += '<a href="#" id="go_back_inactive">Go Back</a>';
}
if (nodewiki.readonly == false)
mdLinks += '<a href="#" id="new_folder">New Folder</a>';
mdLinks += '<br style="clear: both;" />';
mdLinks += '</div>';

dir.forEach(function(fileName){
if (fileName.markdown == true){
mdLinks += '<a href="#" class="link"><code>' + fileName.name + '</code></a>\n';
Expand All @@ -54,16 +68,6 @@ function parseLinks(dir, dirDepth){
}
});

mdLinks += '<div id="tri_buttons">'
mdLinks += '<a href="#" id="new_file">New File</a>';
if (dirDepth > 0){
mdLinks += '<a href="#" id="go_back">Go Back</a>';
} else {
mdLinks += '<a href="#" id="go_back_inactive">Go Back</a>';
}
mdLinks += '<a href="#" id="new_folder">New Folder</a>';
mdLinks += '</div>';

return mdLinks;

} catch (err) {
Expand Down
2 changes: 1 addition & 1 deletion lib/mdserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function sendFile(file, directory, socket){
}

function saveFile(file, directory, socket){
if (allowedExtensions.checkExtension(file.name) == true && typeof file.content != 'undefined'){
if (allowedExtensions.checkExtension(file.name) == true && typeof file.content != 'undefined' && nodewiki.readonly == false){
// only allow markdown files to be saved
console.log('saving file');
fs.writeFile(directory + file.name, file.content, function(err, data){
Expand Down
11 changes: 9 additions & 2 deletions nodewiki.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ var getDir = require("./lib/getDir");
var portNumberDefault = process.env.PORT || 8888;
var listenAddr = process.env.NW_ADDR || ""; // "" ==> INADDR_ANY
exports.gitMode = false; // exported for lib/mdserver.js
exports.readonly = false;

var portNumber = portNumberDefault;

// Process command line
var parser, option;
parser = new mod_getopt.BasicParser('a:(addr)g(git)h(help)l(local)p:(port)', process.argv);
parser = new mod_getopt.BasicParser('a:(addr)g(git)h(help)l(local)p:(port)r(readonly)', process.argv);

while ((option = parser.getopt()) !== undefined) {

Expand Down Expand Up @@ -64,6 +65,10 @@ while ((option = parser.getopt()) !== undefined) {
}
break;

case 'r':
exports.readonly = true;
break;

default:
/* error message already emitted by getopt() */
console.assert('?' == option.option);
Expand Down Expand Up @@ -129,7 +134,9 @@ io = socketio.listen(server);
io.set('log level', 2);

io.sockets.on('connection', function (socket){
var currentPath = process.cwd() + '/';
socket.emit('config', exports);

var currentPath = './';
var dir = getDir.getDir(currentPath);
var links = getDir.parseLinks(dir);
var directoryDepth = 0;
Expand Down
24 changes: 23 additions & 1 deletion static/socketio.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,22 @@ $(document).ready(function(){

var rawMd, fileName;
var editingAllowed = false; //dont allow editing until something is loaded
var config;
window.location.hash = window.location.hash || '/';

var socket = io.connect();
socket.on('connect', function(){
// open the hash given by the browser
var parts = window.location.hash.match(/[^\/]+\/?/g);
parts.shift();
parts.forEach(function(part) {
socket.emit('readFile', {name: part});
});

socket.on('config', function (data){
config = data;
});

socket.on('navLinks', function (data){
$('#navigation').html(data.links);
changeContentHeight();
Expand All @@ -19,6 +32,10 @@ $(document).ready(function(){
if (canSendReadFile == true){
canSendReadFile = false;
socket.emit('readFile', {name: $(a.currentTarget).text()});
if (!window.location.hash.match(/\/$/)) {
window.location.hash = dirname(window.location.hash) + '/';
}
window.location.hash += $(a.currentTarget).text();
$('#navigation').children().attr('class', 'link');
$('#content #markdown_content').html('<em>Loading...</em>');
$('#content_header h1').html('Node Wiki');
Expand All @@ -29,6 +46,7 @@ $(document).ready(function(){
cancelNewFile();
}
}
return false;
});

socket.on('readFileReply', function(data){
Expand Down Expand Up @@ -65,6 +83,7 @@ $(document).ready(function(){

$(document).on('click', '#navigation a#go_back', function(){
socket.emit('goBackFolder');
window.location.hash = dirname(window.location.hash) + '/';
$('#content #markdown_content').html('');
$('#content_header h1').html('Node Wiki');
//editingAllowed = true;
Expand Down Expand Up @@ -250,7 +269,7 @@ $(document).ready(function(){

function showButtons(show, newFile){
var buttons = '<a id="edit" href="#">Edit</a>\n<a id="save" href="#">Save</a>';
if (show){
if (show && !config.readonly){
if (newFile){
$('#edit_save_buttons').html('<a id="cancel" href="#">Cancel</a>\n<a id="save" href="#">Save</a>');
} else {
Expand All @@ -275,5 +294,8 @@ $(document).ready(function(){
}
}

function dirname(path) {
return path.replace(/\\/g,'/').replace(/\/[^\/]*$/, '');;
}

});