mirror of
				https://github.com/NodeBB/NodeBB.git
				synced 2025-10-31 19:15:58 +01:00 
			
		
		
		
	issue #214, preparing for addition of post.parse hook by renaming markdownToHTML to just "toHTML", and making it asynchronous.
This commit is contained in:
		| @@ -66,20 +66,28 @@ var	RDB = require('./redis.js'), | |||||||
| 				postSearch.index(content, pid); | 				postSearch.index(content, pid); | ||||||
| 			}); | 			}); | ||||||
|  |  | ||||||
| 			posts.getPostField(pid, 'tid', function(tid) { | 			async.parallel([ | ||||||
| 				PostTools.isMain(pid, tid, function(isMainPost) { | 				function(next) { | ||||||
| 					if (isMainPost) { | 					posts.getPostField(pid, 'tid', function(tid) { | ||||||
| 						topics.setTopicField(tid, 'title', title); | 						PostTools.isMain(pid, tid, function(isMainPost) { | ||||||
| 						topicSearch.remove(tid, function() { | 							if (isMainPost) { | ||||||
| 							topicSearch.index(title, tid); | 								topics.setTopicField(tid, 'title', title); | ||||||
|  | 								topicSearch.remove(tid, function() { | ||||||
|  | 									topicSearch.index(title, tid); | ||||||
|  | 									next(null, tid); | ||||||
|  | 								}); | ||||||
|  | 							} | ||||||
| 						}); | 						}); | ||||||
| 					} |  | ||||||
|  |  | ||||||
| 					io.sockets.in('topic_' + tid).emit('event:post_edited', { |  | ||||||
| 						pid: pid, |  | ||||||
| 						title: title, |  | ||||||
| 						content: PostTools.markdownToHTML(content) |  | ||||||
| 					}); | 					}); | ||||||
|  | 				}, | ||||||
|  | 				function(next) { | ||||||
|  | 					PostTools.toHTML(content, next); | ||||||
|  | 				} | ||||||
|  | 			], function(err, results) { | ||||||
|  | 				io.sockets.in('topic_' + results[0]).emit('event:post_edited', { | ||||||
|  | 					pid: pid, | ||||||
|  | 					title: title, | ||||||
|  | 					content: results[1] | ||||||
| 				}); | 				}); | ||||||
| 			}); | 			}); | ||||||
| 		}; | 		}; | ||||||
| @@ -161,7 +169,7 @@ var	RDB = require('./redis.js'), | |||||||
| 		}); | 		}); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	PostTools.markdownToHTML = function(md, isSignature) { | 	PostTools.toHTML = function(raw, callback) { | ||||||
| 		var	marked = require('marked'), | 		var	marked = require('marked'), | ||||||
| 			cheerio = require('cheerio'); | 			cheerio = require('cheerio'); | ||||||
|  |  | ||||||
| @@ -169,8 +177,8 @@ var	RDB = require('./redis.js'), | |||||||
| 			breaks: true | 			breaks: true | ||||||
| 		}); | 		}); | ||||||
|  |  | ||||||
| 		if (md && md.length > 0) { | 		if (raw && raw.length > 0) { | ||||||
| 			var	parsedContentDOM = cheerio.load(marked(md)); | 			var	parsedContentDOM = cheerio.load(marked(raw)); | ||||||
| 			var	domain = nconf.get('url'); | 			var	domain = nconf.get('url'); | ||||||
|  |  | ||||||
| 			parsedContentDOM('a').each(function() { | 			parsedContentDOM('a').each(function() { | ||||||
| @@ -179,17 +187,14 @@ var	RDB = require('./redis.js'), | |||||||
|  |  | ||||||
| 				if (href && !href.match(domain) && !utils.isRelativeUrl(href)) { | 				if (href && !href.match(domain) && !utils.isRelativeUrl(href)) { | ||||||
| 					this.attr('href', domain + 'outgoing?url=' + encodeURIComponent(href)); | 					this.attr('href', domain + 'outgoing?url=' + encodeURIComponent(href)); | ||||||
| 					if (!isSignature) this.append(' <i class="icon-external-link"></i>'); |  | ||||||
| 				} | 				} | ||||||
| 			}); | 			}); | ||||||
|  |  | ||||||
|  |  | ||||||
| 			html = parsedContentDOM.html(); | 			callback(null, parsedContentDOM.html()); | ||||||
| 		} else { | 		} else { | ||||||
| 			html = '<p></p>'; | 			callback(null, '<p></p>'); | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		return html; |  | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  |  | ||||||
|   | |||||||
							
								
								
									
										117
									
								
								src/posts.js
									
									
									
									
									
								
							
							
						
						
									
										117
									
								
								src/posts.js
									
									
									
									
									
								
							| @@ -34,28 +34,29 @@ var	RDB = require('./redis.js'), | |||||||
|  |  | ||||||
| 	Posts.addUserInfoToPost = function(post, callback) { | 	Posts.addUserInfoToPost = function(post, callback) { | ||||||
| 		user.getUserFields(post.uid, ['username', 'userslug', 'reputation', 'postcount', 'picture', 'signature', 'banned'], function(err, userData) { | 		user.getUserFields(post.uid, ['username', 'userslug', 'reputation', 'postcount', 'picture', 'signature', 'banned'], function(err, userData) { | ||||||
| 			if(err) | 			if(err) return callback(); | ||||||
| 				return callback(); |  | ||||||
|  |  | ||||||
| 			post.username = userData.username || 'anonymous'; | 			postTools.toHTML(userData.signature, function(err, signature) { | ||||||
| 			post.userslug = userData.userslug || ''; | 				post.username = userData.username || 'anonymous'; | ||||||
| 			post.user_rep = userData.reputation || 0; | 				post.userslug = userData.userslug || ''; | ||||||
| 			post.user_postcount = userData.postcount || 0; | 				post.user_rep = userData.reputation || 0; | ||||||
| 			post.user_banned = userData.banned || '0'; | 				post.user_postcount = userData.postcount || 0; | ||||||
| 			post.picture = userData.picture || require('gravatar').url('', {}, https=nconf.get('https')); | 				post.user_banned = userData.banned || '0'; | ||||||
| 			post.signature = postTools.markdownToHTML(userData.signature, true); | 				post.picture = userData.picture || require('gravatar').url('', {}, https=nconf.get('https')); | ||||||
|  | 				post.signature = signature; | ||||||
|  |  | ||||||
| 			if(post.editor !== '') { | 				if(post.editor !== '') { | ||||||
| 				user.getUserFields(post.editor, ['username', 'userslug'], function(err, editorData) { | 					user.getUserFields(post.editor, ['username', 'userslug'], function(err, editorData) { | ||||||
| 					if(err) | 						if(err) return callback(); | ||||||
| 						return callback(); |  | ||||||
| 					post.editorname = editorData.username; | 						post.editorname = editorData.username; | ||||||
| 					post.editorslug = editorData.userslug; | 						post.editorslug = editorData.userslug; | ||||||
|  | 						callback(); | ||||||
|  | 					}); | ||||||
|  | 				} else { | ||||||
| 					callback(); | 					callback(); | ||||||
| 				}); | 				} | ||||||
| 			} else { | 			}); | ||||||
| 				callback(); |  | ||||||
| 			} |  | ||||||
| 		}); | 		}); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| @@ -64,28 +65,41 @@ var	RDB = require('./redis.js'), | |||||||
| 		var posts = []; | 		var posts = []; | ||||||
|  |  | ||||||
| 		function getPostSummary(pid, callback) { | 		function getPostSummary(pid, callback) { | ||||||
| 			Posts.getPostFields(pid, ['pid', 'tid', 'content', 'uid', 'timestamp', 'deleted'], function(postData) { | 			async.waterfall([ | ||||||
| 				if(postData.deleted === '1') { | 				function(next) { | ||||||
| 					return callback(null); | 					Posts.getPostFields(pid, ['pid', 'tid', 'content', 'uid', 'timestamp', 'deleted'], function(postData) { | ||||||
| 				} | 						if (postData.deleted === '1') return callback(null); | ||||||
|  | 						else { | ||||||
| 				Posts.addUserInfoToPost(postData, function() { | 							postData.relativeTime = utils.relativeTime(postData.timestamp); | ||||||
| 					topics.getTopicFields(postData.tid, ['slug', 'deleted'], function(err, topicData) { | 							next(null, postData); | ||||||
| 						if(err) | 						} | ||||||
| 							return callback(err); |  | ||||||
|  |  | ||||||
| 						if(topicData.deleted === '1') |  | ||||||
| 							return callback(null); |  | ||||||
|  |  | ||||||
| 						if(postData.content) |  | ||||||
| 							postData.content = utils.strip_tags(postTools.markdownToHTML(postData.content)); |  | ||||||
|  |  | ||||||
| 						postData.relativeTime = utils.relativeTime(postData.timestamp); |  | ||||||
| 						postData.topicSlug = topicData.slug; |  | ||||||
| 						posts.push(postData); |  | ||||||
| 						callback(null); |  | ||||||
| 					}); | 					}); | ||||||
| 				}); | 				}, | ||||||
|  | 				function(postData, next) { | ||||||
|  | 					Posts.addUserInfoToPost(postData, function() { | ||||||
|  | 						next(null, postData); | ||||||
|  | 					}); | ||||||
|  | 				}, | ||||||
|  | 				function(postData, next) { | ||||||
|  | 					topics.getTopicFields(postData.tid, ['slug', 'deleted'], function(err, topicData) { | ||||||
|  | 						if (err) return callback(err); | ||||||
|  | 						else if (topicData.deleted === '1') return callback(null); | ||||||
|  |  | ||||||
|  | 						postData.topicSlug = topicData.slug; | ||||||
|  | 						next(null, postData); | ||||||
|  | 					}); | ||||||
|  | 				}, | ||||||
|  | 				function(postData, next) { | ||||||
|  | 					if (postData.content) { | ||||||
|  | 						postTools.toHTML(postData.content, function(err, content) { | ||||||
|  | 							if (!err) postData.content = utils.strip_tags(content); | ||||||
|  | 							next(err, postData); | ||||||
|  | 						}); | ||||||
|  | 					} else next(null, postData); | ||||||
|  | 				} | ||||||
|  | 			], function(err, postData) { | ||||||
|  | 				if (!err) posts.push(postData); | ||||||
|  | 				callback(err); | ||||||
| 			}); | 			}); | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| @@ -144,7 +158,7 @@ var	RDB = require('./redis.js'), | |||||||
| 	Posts.getPostsByPids = function(pids, callback) { | 	Posts.getPostsByPids = function(pids, callback) { | ||||||
| 		var posts = []; | 		var posts = []; | ||||||
|  |  | ||||||
| 		function iterator(pid, callback) { | 		async.eachSeries(pids, function (pid, callback) { | ||||||
| 			Posts.getPostData(pid, function(postData) { | 			Posts.getPostData(pid, function(postData) { | ||||||
| 				if(postData) { | 				if(postData) { | ||||||
| 					postData.relativeTime = utils.relativeTime(postData.timestamp); | 					postData.relativeTime = utils.relativeTime(postData.timestamp); | ||||||
| @@ -152,8 +166,6 @@ var	RDB = require('./redis.js'), | |||||||
| 					postData['edited-class'] = postData.editor !== '' ? '' : 'none'; | 					postData['edited-class'] = postData.editor !== '' ? '' : 'none'; | ||||||
| 					postData['relativeEditTime'] = postData.edited !== '0' ? utils.relativeTime(postData.edited) : ''; | 					postData['relativeEditTime'] = postData.edited !== '0' ? utils.relativeTime(postData.edited) : ''; | ||||||
|  |  | ||||||
| 					postData.content = postTools.markdownToHTML(postData.content); |  | ||||||
|  |  | ||||||
| 					if(postData.uploadedImages) { | 					if(postData.uploadedImages) { | ||||||
| 						try { | 						try { | ||||||
| 							postData.uploadedImages = JSON.parse(postData.uploadedImages); | 							postData.uploadedImages = JSON.parse(postData.uploadedImages); | ||||||
| @@ -164,13 +176,15 @@ var	RDB = require('./redis.js'), | |||||||
| 					} else { | 					} else { | ||||||
| 						postData.uploadedImages = []; | 						postData.uploadedImages = []; | ||||||
| 					} | 					} | ||||||
| 					posts.push(postData); |  | ||||||
| 				} |  | ||||||
| 				callback(null); |  | ||||||
| 			}); |  | ||||||
| 		} |  | ||||||
|  |  | ||||||
| 		async.eachSeries(pids, iterator, function(err) { | 					postTools.toHTML(postData.content, function(err, content) { | ||||||
|  | 						postData.content = content; | ||||||
|  | 						posts.push(postData); | ||||||
|  | 						callback(null); | ||||||
|  | 					}); | ||||||
|  | 				} | ||||||
|  | 			}); | ||||||
|  | 		}, function(err) { | ||||||
| 			if(!err) { | 			if(!err) { | ||||||
| 				callback(null, posts); | 				callback(null, posts); | ||||||
| 			} else { | 			} else { | ||||||
| @@ -329,8 +343,9 @@ var	RDB = require('./redis.js'), | |||||||
| 							}, | 							}, | ||||||
| 							content: function(next) { | 							content: function(next) { | ||||||
| 								plugins.fireHook('filter:post.get', postData, function(postData) { | 								plugins.fireHook('filter:post.get', postData, function(postData) { | ||||||
| 									postData.content = postTools.markdownToHTML(postData.content, false); | 									postTools.toHTML(postData.content, function(err, content) { | ||||||
| 									next(null, postData.content); | 										next(null, content); | ||||||
|  | 									}); | ||||||
| 								}); | 								}); | ||||||
| 							} | 							} | ||||||
| 						}, function(err, results) { | 						}, function(err, results) { | ||||||
|   | |||||||
| @@ -44,7 +44,7 @@ var user = require('./../user.js'), | |||||||
|  |  | ||||||
| 				require('async').each(data.categories, iterator, function(err) { | 				require('async').each(data.categories, iterator, function(err) { | ||||||
| 					data.motd_class = (meta.config.show_motd === '1' || meta.config.show_motd === undefined) ? '' : 'none'; | 					data.motd_class = (meta.config.show_motd === '1' || meta.config.show_motd === undefined) ? '' : 'none'; | ||||||
| 					data.motd = marked(meta.config.motd || "# NodeBB <span class='hidden-phone'>v " + pkg.version + "</span>\nWelcome to NodeBB, the discussion platform of the future.\n\n<a target=\"_blank\" href=\"http://www.nodebb.org\" class=\"btn btn-large\"><i class=\"icon-comment\"></i><span class='hidden-phone'> Get NodeBB</span></a> <a target=\"_blank\" href=\"https://github.com/designcreateplay/NodeBB\" class=\"btn btn-large\"><i class=\"icon-github-alt\"></i><span class='hidden-phone'> Fork us on Github</span></a> <a target=\"_blank\" href=\"https://twitter.com/dcplabs\" class=\"btn btn-large\"><i class=\"icon-twitter\"></i><span class='hidden-phone'> @dcplabs</span></a>"); | 					data.motd = require('marked')(meta.config.motd || "# NodeBB <span class='hidden-phone'>v " + pkg.version + "</span>\nWelcome to NodeBB, the discussion platform of the future.\n\n<a target=\"_blank\" href=\"http://www.nodebb.org\" class=\"btn btn-large\"><i class=\"icon-comment\"></i><span class='hidden-phone'> Get NodeBB</span></a> <a target=\"_blank\" href=\"https://github.com/designcreateplay/NodeBB\" class=\"btn btn-large\"><i class=\"icon-github-alt\"></i><span class='hidden-phone'> Fork us on Github</span></a> <a target=\"_blank\" href=\"https://twitter.com/dcplabs\" class=\"btn btn-large\"><i class=\"icon-twitter\"></i><span class='hidden-phone'> @dcplabs</span></a>"); | ||||||
| 					res.json(data); | 					res.json(data); | ||||||
| 				}); | 				}); | ||||||
|  |  | ||||||
|   | |||||||
| @@ -4,7 +4,6 @@ var user = require('./../user.js'), | |||||||
| 	fs = require('fs'), | 	fs = require('fs'), | ||||||
| 	utils = require('./../../public/src/utils.js'), | 	utils = require('./../../public/src/utils.js'), | ||||||
| 	path = require('path'), | 	path = require('path'), | ||||||
| 	marked = require('marked'), |  | ||||||
| 	winston = require('winston'); | 	winston = require('winston'); | ||||||
|  |  | ||||||
| (function(User) { | (function(User) { | ||||||
| @@ -353,12 +352,15 @@ var user = require('./../user.js'), | |||||||
|  |  | ||||||
| 							userData.posts = posts.filter(function(p) {return p.deleted !== "1";}); | 							userData.posts = posts.filter(function(p) {return p.deleted !== "1";}); | ||||||
| 							userData.isFollowing = isFollowing; | 							userData.isFollowing = isFollowing; | ||||||
| 							userData.signature = postTools.markdownToHTML(userData.signature, true); |  | ||||||
| 							if(!userData.profileviews) | 							if(!userData.profileviews) | ||||||
| 								userData.profileviews = 1; | 								userData.profileviews = 1; | ||||||
| 							if(callerUID !== userData.uid) | 							if(callerUID !== userData.uid) | ||||||
| 								user.incrementUserFieldBy(userData.uid, 'profileviews', 1); | 								user.incrementUserFieldBy(userData.uid, 'profileviews', 1); | ||||||
| 							res.json(userData); |  | ||||||
|  | 							postTools.toHTML(userData.signature, function(err, signature) { | ||||||
|  | 								userData.signature = signature; | ||||||
|  | 								res.json(userData); | ||||||
|  | 							}); | ||||||
| 						}); | 						}); | ||||||
| 					}); | 					}); | ||||||
| 				} else { | 				} else { | ||||||
|   | |||||||
| @@ -5,7 +5,6 @@ var	RDB = require('./redis.js') | |||||||
| 	user = require('./user.js'), | 	user = require('./user.js'), | ||||||
| 	categories = require('./categories.js'), | 	categories = require('./categories.js'), | ||||||
| 	posts = require('./posts.js'), | 	posts = require('./posts.js'), | ||||||
| 	marked = require('marked'), |  | ||||||
| 	threadTools = require('./threadTools.js'), | 	threadTools = require('./threadTools.js'), | ||||||
| 	postTools = require('./postTools'), | 	postTools = require('./postTools'), | ||||||
| 	async = require('async'), | 	async = require('async'), | ||||||
| @@ -14,9 +13,6 @@ var	RDB = require('./redis.js') | |||||||
| 	reds = require('reds'), | 	reds = require('reds'), | ||||||
| 	topicSearch = reds.createSearch('nodebbtopicsearch'); | 	topicSearch = reds.createSearch('nodebbtopicsearch'); | ||||||
|  |  | ||||||
| marked.setOptions({ |  | ||||||
| 	breaks: true |  | ||||||
| }); |  | ||||||
|  |  | ||||||
| (function(Topics) { | (function(Topics) { | ||||||
|  |  | ||||||
| @@ -569,15 +565,17 @@ marked.setOptions({ | |||||||
|  |  | ||||||
| 						if(postData.content) { | 						if(postData.content) { | ||||||
| 							stripped = postData.content.replace(/>.+\n\n/, ''); | 							stripped = postData.content.replace(/>.+\n\n/, ''); | ||||||
| 							stripped = utils.strip_tags(postTools.markdownToHTML(stripped)); | 							postTools.toHTML(stripped, function(err, stripped) { | ||||||
| 						} | 								stripped = utils.strip_tags(stripped); | ||||||
|  |  | ||||||
| 						callback(null, { | 								callback(null, { | ||||||
| 							"text": stripped, | 									"text": stripped, | ||||||
| 							"username": userData.username, | 									"username": userData.username, | ||||||
| 							"picture": userData.picture, | 									"picture": userData.picture, | ||||||
| 							"timestamp" : timestamp | 									"timestamp" : timestamp | ||||||
| 						}); | 								}); | ||||||
|  | 							}); | ||||||
|  | 						} | ||||||
| 					}); | 					}); | ||||||
| 				}); | 				}); | ||||||
| 			} else callback(new Error('no-teaser-found')); | 			} else callback(new Error('no-teaser-found')); | ||||||
|   | |||||||
| @@ -5,7 +5,6 @@ var utils = require('./../public/src/utils.js'), | |||||||
| 	meta = require('./meta.js'), | 	meta = require('./meta.js'), | ||||||
| 	emailjsServer = emailjs.server.connect(meta.config.mailer), | 	emailjsServer = emailjs.server.connect(meta.config.mailer), | ||||||
| 	bcrypt = require('bcrypt'), | 	bcrypt = require('bcrypt'), | ||||||
| 	marked = require('marked'), |  | ||||||
| 	notifications = require('./notifications.js'), | 	notifications = require('./notifications.js'), | ||||||
| 	topics = require('./topics.js'), | 	topics = require('./topics.js'), | ||||||
| 	async = require('async'); | 	async = require('async'); | ||||||
|   | |||||||
| @@ -6,7 +6,6 @@ var express = require('express'), | |||||||
| 	path = require('path'), | 	path = require('path'), | ||||||
| 	redis = require('redis'), | 	redis = require('redis'), | ||||||
| 	redisServer = redis.createClient(nconf.get('redis:port'), nconf.get('redis:host')), | 	redisServer = redis.createClient(nconf.get('redis:port'), nconf.get('redis:host')), | ||||||
| 	marked = require('marked'), |  | ||||||
| 	utils = require('../public/src/utils.js'), | 	utils = require('../public/src/utils.js'), | ||||||
| 	pkg = require('../package.json'), | 	pkg = require('../package.json'), | ||||||
| 	fs = require('fs'), | 	fs = require('fs'), | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user