Skip to content

Commit f64e6f0

Browse files
committed
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
1 parent abe6a58 commit f64e6f0

File tree

1 file changed

+8
-13
lines changed

1 file changed

+8
-13
lines changed

src/posts/parse.js

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,9 @@ let sanitizeConfig = {
3636
const allowedTypes = new Set(['default', 'plaintext', 'activitypub.note', 'activitypub.article', 'markdown']);
3737

3838
module.exports = function (Posts) {
39-
Posts.urlRegex = {
40-
regex: /href="([^"]+)"/g,
41-
length: 6,
42-
};
43-
44-
Posts.imgRegex = {
45-
regex: /src="([^"]+)"/g,
46-
length: 5,
47-
};
39+
Posts.urlRegex = /href="([^"]+)"/g;
40+
Posts.imgRegex = /src="([^"]+)"/g;
41+
Posts.mdImageUrlRegex = /\[.+?\]\(([^\\)]+)\)/g;
4842

4943
Posts.parsePost = async function (postData, type) {
5044
if (!postData) {
@@ -89,7 +83,7 @@ module.exports = function (Posts) {
8983
return content;
9084
}
9185
let parsed;
92-
let current = regex.regex.exec(content);
86+
let current = regex.exec(content);
9387
let absolute;
9488
while (current !== null) {
9589
if (current[1]) {
@@ -104,15 +98,16 @@ module.exports = function (Posts) {
10498
absolute = `//${current[1]}`;
10599
}
106100

107-
content = content.slice(0, current.index + regex.length) +
101+
const offset = current[0].indexOf(current[1]);
102+
content = content.slice(0, current.index + offset) +
108103
absolute +
109-
content.slice(current.index + regex.length + current[1].length);
104+
content.slice(current.index + offset + current[1].length);
110105
}
111106
} catch (err) {
112107
winston.verbose(err.messsage);
113108
}
114109
}
115-
current = regex.regex.exec(content);
110+
current = regex.exec(content);
116111
}
117112

118113
return content;

0 commit comments

Comments
 (0)