You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nodebb/src/posts/parse.js

93 lines
2.4 KiB
JavaScript

'use strict';
9 years ago
var nconf = require('nconf');
var url = require('url');
var winston = require('winston');
8 years ago
var S = require('string');
9 years ago
8 years ago
var meta = require('../meta');
9 years ago
var cache = require('./cache');
var plugins = require('../plugins');
9 years ago
var translator = require('../../public/src/modules/translator');
9 years ago
var urlRegex = /href="([^"]+)"/g;
module.exports = function (Posts) {
Posts.parsePost = function (postData, callback) {
postData.content = postData.content || '';
if (postData.pid && cache.has(String(postData.pid))) {
postData.content = cache.get(String(postData.pid));
return callback(null, postData);
}
// Casting post content into a string, just in case
if (typeof postData.content !== 'string') {
postData.content = postData.content.toString();
}
plugins.fireHook('filter:parse.post', {postData: postData}, function (err, data) {
if (err) {
return callback(err);
}
9 years ago
9 years ago
data.postData.content = translator.escape(data.postData.content);
if (global.env === 'production' && data.postData.pid) {
cache.set(String(data.postData.pid), data.postData.content);
9 years ago
}
callback(null, data.postData);
});
};
Posts.parseSignature = function (userData, uid, callback) {
8 years ago
userData.signature = sanitizeSignature(userData.signature || '');
plugins.fireHook('filter:parse.signature', {userData: userData, uid: uid}, callback);
};
9 years ago
Posts.relativeToAbsolute = function (content) {
9 years ago
// Turns relative links in post body to absolute urls
var parsed, current, absolute;
9 years ago
while ((current = urlRegex.exec(content)) !== null) {
9 years ago
if (current[1]) {
9 years ago
try {
parsed = url.parse(current[1]);
if (!parsed.protocol) {
if (current[1].startsWith('/')) {
// Internal link
absolute = nconf.get('url') + current[1];
} else {
// External link
absolute = '//' + current[1];
}
content = content.slice(0, current.index + 6) + absolute + content.slice(current.index + 6 + current[1].length);
9 years ago
}
9 years ago
} catch(err) {
winston.verbose(err.messsage);
}
9 years ago
}
}
return content;
};
8 years ago
function sanitizeSignature(signature) {
var string = S(signature),
tagsToStrip = [];
if (parseInt(meta.config['signatures:disableLinks'], 10) === 1) {
tagsToStrip.push('a');
}
if (parseInt(meta.config['signatures:disableImages'], 10) === 1) {
tagsToStrip.push('img');
}
return tagsToStrip.length ? string.stripTags.apply(string, tagsToStrip).s : signature;
}
};