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

136 lines
3.9 KiB
JavaScript

'use strict';
9 years ago
var nconf = require('nconf');
var url = require('url');
var winston = require('winston');
const sanitize = require('sanitize-html');
const _ = require('lodash');
9 years ago
8 years ago
var meta = require('../meta');
9 years ago
var plugins = require('../plugins');
var translator = require('../translator');
var utils = require('../utils');
let sanitizeConfig = {
allowedTags: sanitize.defaults.allowedTags.concat([
// Some safe-to-use tags to add
'span', 'a', 'pre', 'small',
'sup', 'sub', 'u', 'del',
'video', 'audio', 'iframe', 'embed',
'img', 'tfoot', 'h1', 'h2',
's', 'button', 'i',
]),
allowedAttributes: {
...sanitize.defaults.allowedAttributes,
a: ['href', 'hreflang', 'media', 'rel', 'target', 'type'],
img: ['alt', 'height', 'ismap', 'src', 'usemap', 'width', 'srcset'],
iframe: ['height', 'name', 'src', 'width'],
video: ['autoplay', 'controls', 'height', 'loop', 'muted', 'poster', 'preload', 'src', 'width'],
audio: ['autoplay', 'controls', 'loop', 'muted', 'preload', 'src'],
embed: ['height', 'src', 'type', 'width'],
},
globalAttributes: ['accesskey', 'class', 'contenteditable', 'dir',
'draggable', 'dropzone', 'hidden', 'id', 'lang', 'spellcheck', 'style',
'tabindex', 'title', 'translate', 'aria-expanded', 'data-*',
],
};
module.exports = function (Posts) {
8 years ago
Posts.urlRegex = {
regex: /href="([^"]+)"/g,
length: 6,
};
8 years ago
8 years ago
Posts.imgRegex = {
8 years ago
regex: /src="([^"]+)"/g,
8 years ago
length: 5,
};
Posts.parsePost = async function (postData) {
if (!postData) {
return postData;
}
8 years ago
postData.content = String(postData.content || '');
const cache = require('./cache');
const pid = String(postData.pid);
const cachedContent = cache.get(pid);
if (postData.pid && cachedContent !== undefined) {
postData.content = cachedContent;
cache.hits += 1;
return postData;
}
cache.misses += 1;
const data = await plugins.fireHook('filter:parse.post', { postData: postData });
data.postData.content = translator.escape(data.postData.content);
if (global.env === 'production' && data.postData.pid) {
cache.set(pid, data.postData.content);
}
return data.postData;
};
Posts.parseSignature = async function (userData, uid) {
8 years ago
userData.signature = sanitizeSignature(userData.signature || '');
return await plugins.fireHook('filter:parse.signature', { userData: userData, uid: uid });
};
9 years ago
8 years ago
Posts.relativeToAbsolute = function (content, regex) {
9 years ago
// Turns relative links in post body to absolute urls
var parsed;
8 years ago
var current = regex.regex.exec(content);
var absolute;
while (current !== 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
8 years ago
absolute = nconf.get('base_url') + current[1];
9 years ago
} else {
// External link
absolute = '//' + current[1];
}
8 years ago
content = content.slice(0, current.index + regex.length) + absolute + content.slice(current.index + regex.length + current[1].length);
9 years ago
}
} catch (err) {
winston.verbose(err.messsage);
}
9 years ago
}
8 years ago
current = regex.regex.exec(content);
9 years ago
}
return content;
};
8 years ago
Posts.sanitize = function (content) {
return sanitize(content, {
allowedTags: sanitizeConfig.allowedTags, allowedAttributes: sanitizeConfig.allowedAttributes,
});
};
Posts.configureSanitize = async () => {
// Each allowed tags should have some common global attributes...
sanitizeConfig.allowedTags.forEach((tag) => {
sanitizeConfig.allowedAttributes[tag] = _.union(sanitizeConfig.allowedAttributes[tag], sanitizeConfig.globalAttributes);
});
sanitizeConfig = await plugins.fireHook('filter:sanitize.config', sanitizeConfig);
};
8 years ago
function sanitizeSignature(signature) {
signature = translator.escape(signature);
var tagsToStrip = [];
8 years ago
if (meta.config['signatures:disableLinks']) {
8 years ago
tagsToStrip.push('a');
}
if (meta.config['signatures:disableImages']) {
8 years ago
tagsToStrip.push('img');
}
return utils.stripHTMLTags(signature, tagsToStrip);
8 years ago
}
};