Skip to content

Commit

Permalink
fix #440
Browse files Browse the repository at this point in the history
  • Loading branch information
alsotang committed Nov 2, 2014
1 parent 135d785 commit 464861a
Show file tree
Hide file tree
Showing 16 changed files with 8,431 additions and 1,391 deletions.
8 changes: 4 additions & 4 deletions api/v1/topic.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var config = require('../../config');
var eventproxy = require('eventproxy');
var _ = require('lodash');
var at = require('../../common/at');
var renderHelpers = require('../../common/render_helpers');
var renderHelper = require('../../common/render_helper');
var validator = require('validator');

var index = function (req, res, next) {
Expand All @@ -32,7 +32,7 @@ var index = function (req, res, next) {
topics.forEach(function (topic) {
UserModel.findById(topic.author_id, ep.done(function (author) {
if (mdrender) {
topic.content = renderHelpers.markdown(at.linkUsers(topic.content));
topic.content = renderHelper.markdown(at.linkUsers(topic.content));
}
topic.author = _.pick(author, ['loginname', 'avatar_url']);
ep.emit('author');
Expand Down Expand Up @@ -67,13 +67,13 @@ var show = function (req, res, next) {
'good', 'top', 'author']);

if (mdrender) {
topic.content = renderHelpers.markdown(at.linkUsers(topic.content));
topic.content = renderHelper.markdown(at.linkUsers(topic.content));
}
topic.author = _.pick(author, ['loginname', 'avatar_url']);

topic.replies = replies.map(function (reply) {
if (mdrender) {
reply.content = renderHelpers.markdown(at.linkUsers(reply.content));
reply.content = renderHelper.markdown(at.linkUsers(reply.content));
}
reply.author = _.pick(reply.author, ['loginname', 'avatar_url']);
reply = _.pick(reply, ['id', 'author', 'content', 'ups', 'create_at']);
Expand Down
2 changes: 1 addition & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ _.extend(app.locals, {
assets: assets
});

_.extend(app.locals, require('./common/render_helpers'));
_.extend(app.locals, require('./common/render_helper'));
app.use(function (req, res, next) {
res.locals.csrf = req.csrfToken ? req.csrfToken() : '';
next();
Expand Down
84 changes: 84 additions & 0 deletions common/render_helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*!
* nodeclub - common/render_helpers.js
* Copyright(c) 2013 fengmk2 <fengmk2@gmail.com>
* MIT Licensed
*/

"use strict";

/**
* Module dependencies.
*/

var Remarkable = require('remarkable');
var _ = require('lodash');
var config = require('../config');
var validator = require('validator');
var multiline = require('multiline');

// Set default options
var md = new Remarkable();

md.set({
html: false, // Enable HTML tags in source
xhtmlOut: false, // Use '/' to close single tags (<br />)
breaks: true, // Convert '\n' in paragraphs into <br>
linkify: false, // Autoconvert URL-like text to links
typographer: false, // Enable smartypants and other sweet transforms
});

md.renderer.rules.fence = function (tokens, idx) {
var token = tokens[idx];
var language = token.params && ('language-' + token.params) || '';
language = validator.escape(language);
return '<pre class="prettyprint ' + language + '">'
+ '<code>' + validator.escape(token.content) + '</code>'
+ '</pre>';
};


// renderer.code = function (code, lang) {
// var language = lang && ('language-' + lang) || '';
// language = validator.escape(language);
// return '<pre class="prettyprint ' + language + '">'
// + '<code>' + validator.escape(code) + '</code>'
// + '</pre>';
// };

// marked.setOptions({
// renderer: renderer,
// gfm: true,
// tables: true,
// breaks: true,
// pedantic: false,
// sanitize: true,
// smartLists: true,
// smartypants: false,
// });

exports.markdown = function (text) {
return '<div class="markdown-text">' + md.render(text || '') + '</div>';
};

exports.multiline = multiline;

exports.escapeSignature = function (signature) {
return signature.split('\n').map(function (p) {
return _.escape(p);
}).join('<br>');
};

exports.staticFile = function (filePath) {
return config.site_static_host + filePath;
};

exports.tabName = function (tab) {
var pair = _.find(config.tabs, function (pair) {
return pair[0] === tab;
});
if (pair) {
return pair[1];
}
};

exports._ = _;
66 changes: 0 additions & 66 deletions common/render_helpers.js

This file was deleted.

4 changes: 2 additions & 2 deletions controllers/rss.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var config = require('../config');
var convert = require('data2xml')();
var Topic = require('../proxy').Topic;
var cache = require('../common/cache');
var marked = require('marked');
var renderHelper = require('../common/render_helper');
var eventproxy = require('eventproxy');

exports.index = function (req, res, next) {
Expand Down Expand Up @@ -40,7 +40,7 @@ exports.index = function (req, res, next) {
title: topic.title,
link: config.rss.link + '/topic/' + topic._id,
guid: config.rss.link + '/topic/' + topic._id,
description: marked(topic.content),
description: renderHelper.markdown(topic.content),
author: topic.author.loginname,
pubDate: topic.create_at.toUTCString()
});
Expand Down
4 changes: 2 additions & 2 deletions controllers/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var config = require('../config');
var eventproxy = require('eventproxy');
var cache = require('../common/cache');
var xmlbuilder = require('xmlbuilder');
var renderHelpers = require('../common/render_helpers');
var renderHelper = require('../common/render_helper');

// 主页的缓存工作。主页是需要主动缓存的
function indexCache() {
Expand Down Expand Up @@ -117,7 +117,7 @@ exports.index = function (req, res, next) {
}
}));

var tabName = renderHelpers.tabName(tab);
var tabName = renderHelper.tabName(tab);
proxy.all('topics', 'tops', 'no_reply_topics', 'pages',
function (topics, tops, no_reply_topics, pages) {
res.render('index', {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
"express-session": "1.9.1",
"loader": "0.1.4",
"lodash": "2.4.1",
"marked": "0.3.2",
"memory-cache": "0.0.5",
"method-override": "1.0.2",
"moment": "2.8.3",
Expand All @@ -38,6 +37,7 @@
"pm2": "0.11.1",
"qn": "1.0.1",
"ready": "0.1.1",
"remarkable": "1.3.0",
"response-time": "2.2.0",
"utility": "1.0.0",
"validator": "3.22.0",
Expand Down
4 changes: 2 additions & 2 deletions public/libs/editor/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -7129,9 +7129,9 @@ Editor.toolbar = toolbar;
* Default markdown render.
*/
Editor.markdown = function(text) {
if (window.marked) {
if (window.remarkable) {
// use marked as markdown parser
return marked(text);
return remarkable.render(text);
}
};

Expand Down
33 changes: 14 additions & 19 deletions public/libs/editor/ext.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
(function(Editor, marked, WebUploader){
// configure marked
var renderer = new marked.Renderer();
renderer.code = function (code, lang) {
var ret = '<pre class="prettyprint language-' + lang + '">';
ret += '<code>' + code.replace(/</g, '&lt;').replace(/>/g, '&gt;') + '</code>';
ret += '</pre>';
return ret;
};
marked.setOptions({
renderer: renderer,
gfm: true,
tables: true,
breaks: true,
pedantic: false,
sanitize: true,
smartLists: true,
smartypants: false,
(function(Editor, Remarkable, WebUploader){
// Set default options
var md = new Remarkable();

md.set({
html: false, // Enable HTML tags in source
xhtmlOut: false, // Use '/' to close single tags (<br />)
breaks: true, // Convert '\n' in paragraphs into <br>
langPrefix: 'language-', // CSS language prefix for fenced blocks
linkify: false, // Autoconvert URL-like text to links
typographer: false, // Enable smartypants and other sweet transforms
});

window.remarkable = md;

var toolbar = Editor.toolbar;

var replaceTool = function(name, callback){
Expand Down Expand Up @@ -248,4 +243,4 @@
var line = cm.lastLine();
cm.setLine(line, cm.getLine(line) + txt);
};
})(window.Editor, window.marked, window.WebUploader);
})(window.Editor, window.Remarkable, window.WebUploader);
Loading

0 comments on commit 464861a

Please sign in to comment.