mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 08:36:12 +01:00
refactor: Posts.relativeToAbsolute so that the regexes passed to it no longer need a pre-defined length, it is now calculated from the match result, added new regex for markdown image/anchors
This commit is contained in:
@@ -36,15 +36,9 @@ let sanitizeConfig = {
|
||||
const allowedTypes = new Set(['default', 'plaintext', 'activitypub.note', 'activitypub.article', 'markdown']);
|
||||
|
||||
module.exports = function (Posts) {
|
||||
Posts.urlRegex = {
|
||||
regex: /href="([^"]+)"/g,
|
||||
length: 6,
|
||||
};
|
||||
|
||||
Posts.imgRegex = {
|
||||
regex: /src="([^"]+)"/g,
|
||||
length: 5,
|
||||
};
|
||||
Posts.urlRegex = /href="([^"]+)"/g;
|
||||
Posts.imgRegex = /src="([^"]+)"/g;
|
||||
Posts.mdImageUrlRegex = /\[.+?\]\(([^\\)]+)\)/g;
|
||||
|
||||
Posts.parsePost = async function (postData, type) {
|
||||
if (!postData) {
|
||||
@@ -89,7 +83,7 @@ module.exports = function (Posts) {
|
||||
return content;
|
||||
}
|
||||
let parsed;
|
||||
let current = regex.regex.exec(content);
|
||||
let current = regex.exec(content);
|
||||
let absolute;
|
||||
while (current !== null) {
|
||||
if (current[1]) {
|
||||
@@ -104,15 +98,16 @@ module.exports = function (Posts) {
|
||||
absolute = `//${current[1]}`;
|
||||
}
|
||||
|
||||
content = content.slice(0, current.index + regex.length) +
|
||||
const offset = current[0].indexOf(current[1]);
|
||||
content = content.slice(0, current.index + offset) +
|
||||
absolute +
|
||||
content.slice(current.index + regex.length + current[1].length);
|
||||
content.slice(current.index + offset + current[1].length);
|
||||
}
|
||||
} catch (err) {
|
||||
winston.verbose(err.messsage);
|
||||
}
|
||||
}
|
||||
current = regex.regex.exec(content);
|
||||
current = regex.exec(content);
|
||||
}
|
||||
|
||||
return content;
|
||||
|
||||
Reference in New Issue
Block a user