From 6635b50c35ca0f7605d3a33b4713f6bcc9499d6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= <baris@nodebb.org> Date: Sun, 28 May 2017 01:10:16 -0400 Subject: [PATCH] closes #5522 --- src/posts/parse.js | 22 +++++++++++++++------- src/topics/follow.js | 3 ++- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/posts/parse.js b/src/posts/parse.js index 3e33226b8d..4c5bdded9f 100644 --- a/src/posts/parse.js +++ b/src/posts/parse.js @@ -11,9 +11,17 @@ var cache = require('./cache'); var plugins = require('../plugins'); var translator = require('../translator'); -var urlRegex = /href="([^"]+)"/g; - module.exports = function (Posts) { + + Posts.urlRegex = { + regex: /href="([^"]+)"/g, + length: 6, + }; + Posts.imgRegex = { + regex:/src="([^"]+)"/g, + length: 5, + }; + Posts.parsePost = function (postData, callback) { postData.content = String(postData.content || ''); @@ -42,10 +50,10 @@ module.exports = function (Posts) { plugins.fireHook('filter:parse.signature', { userData: userData, uid: uid }, callback); }; - Posts.relativeToAbsolute = function (content) { + Posts.relativeToAbsolute = function (content, regex) { // Turns relative links in post body to absolute urls var parsed; - var current = urlRegex.exec(content); + var current = regex.regex.exec(content); var absolute; while (current !== null) { if (current[1]) { @@ -54,19 +62,19 @@ module.exports = function (Posts) { if (!parsed.protocol) { if (current[1].startsWith('/')) { // Internal link - absolute = nconf.get('url') + current[1]; + absolute = nconf.get('base_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); + content = content.slice(0, current.index + regex.length) + absolute + content.slice(current.index + regex.length + current[1].length); } } catch (err) { winston.verbose(err.messsage); } } - current = urlRegex.exec(content); + current = regex.regex.exec(content); } return content; diff --git a/src/topics/follow.js b/src/topics/follow.js index 6ca4b61625..89db6b3d13 100644 --- a/src/topics/follow.js +++ b/src/topics/follow.js @@ -219,7 +219,8 @@ module.exports = function (Topics) { titleEscaped = title.replace(/%/g, '%').replace(/,/g, ','); } - postData.content = posts.relativeToAbsolute(postData.content); + postData.content = posts.relativeToAbsolute(postData.content, posts.urlRegex); + postData.content = posts.relativeToAbsolute(postData.content, posts.imgRegex); notifications.create({ type: 'new-reply',