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:
Julian Lam
2025-01-29 11:55:38 -05:00
parent abe6a587b6
commit f64e6f0f1a

View File

@@ -36,15 +36,9 @@ let sanitizeConfig = {
const allowedTypes = new Set(['default', 'plaintext', 'activitypub.note', 'activitypub.article', 'markdown']); const allowedTypes = new Set(['default', 'plaintext', 'activitypub.note', 'activitypub.article', 'markdown']);
module.exports = function (Posts) { module.exports = function (Posts) {
Posts.urlRegex = { Posts.urlRegex = /href="([^"]+)"/g;
regex: /href="([^"]+)"/g, Posts.imgRegex = /src="([^"]+)"/g;
length: 6, Posts.mdImageUrlRegex = /\[.+?\]\(([^\\)]+)\)/g;
};
Posts.imgRegex = {
regex: /src="([^"]+)"/g,
length: 5,
};
Posts.parsePost = async function (postData, type) { Posts.parsePost = async function (postData, type) {
if (!postData) { if (!postData) {
@@ -89,7 +83,7 @@ module.exports = function (Posts) {
return content; return content;
} }
let parsed; let parsed;
let current = regex.regex.exec(content); let current = regex.exec(content);
let absolute; let absolute;
while (current !== null) { while (current !== null) {
if (current[1]) { if (current[1]) {
@@ -104,15 +98,16 @@ module.exports = function (Posts) {
absolute = `//${current[1]}`; 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 + absolute +
content.slice(current.index + regex.length + current[1].length); content.slice(current.index + offset + current[1].length);
} }
} catch (err) { } catch (err) {
winston.verbose(err.messsage); winston.verbose(err.messsage);
} }
} }
current = regex.regex.exec(content); current = regex.exec(content);
} }
return content; return content;