-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexposes.js
More file actions
68 lines (64 loc) · 2.53 KB
/
exposes.js
File metadata and controls
68 lines (64 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
const cheerio = require('cheerio');
const sanitizeHtmlLib = require('sanitize-html');
const htmlSanitizer = require('@/utils/htmlSanitizer');
function exposeFeed (feed, userId) {
const publicFeed = feed?.toObject ? feed.toObject() : feed;
return {
...publicFeed,
id: publicFeed._id,
_id: undefined,
subscribers: publicFeed.subscribers.length,
isSubscribe: publicFeed.subscribers.toString().includes(userId)
};
}
function exposeArticle (article, userId, { onlyDescription } = {}) {
const publicArticle = article?.toObject ? article.toObject() : article;
article.title = article.title.replace(/([א-ת] )(צפו)/, '$1• $2');
if (onlyDescription) {
delete publicArticle.content;
const sensitizedDescription = sanitizeHtmlLib(publicArticle.description, {
allowedTags: ['p'],
allowedAttributes: {
a: ['href']
}
});
const $description = cheerio.load(sensitizedDescription);
const descriptionReal = $description.root().find('p:first');
return {
...publicArticle,
readBy: publicArticle.readBy?.length || 0,
readByMe: publicArticle.readBy?.toString().includes(userId) ?? false,
id: publicArticle._id,
_id: undefined,
description: descriptionReal.html()
};
} else {
const sensitizedContent = htmlSanitizer(publicArticle.content);
const $content = cheerio.load(sensitizedContent);
$content.root().find('a').attr('target', '_blank');
$content.root().find('iframe').attr({ height: '597px', width: '1047px' });
$content.root().find('iframe').not('[src]').remove();
$content.root().find('.advads-marketing-content-in-the-content-of-the-article').remove();
const sensitizedDescription = sanitizeHtmlLib(publicArticle.description, {
allowedTags: ['p'],
allowedAttributes: {
a: ['href']
}
});
const $description = cheerio.load(sensitizedDescription);
const descriptionReal = $description.root().find('p:first');
return {
...publicArticle,
_id: undefined,
id: publicArticle._id,
readBy: publicArticle.readBy?.length || 0,
readByMe: publicArticle.readBy?.toString().includes(userId) ?? false,
content: $content.html(),
description: descriptionReal.html()
};
};
}
module.exports = {
exposeFeed,
exposeArticle
};