forked from ether/ep_comments_page
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exportHTML.js
60 lines (54 loc) · 2.43 KB
/
exportHTML.js
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
'use strict';
const $ = require('cheerio');
const commentManager = require('./commentManager');
const settings = require('ep_etherpad-lite/node/utils/Settings');
// Iterate over pad attributes to find only the comment ones
const findAllCommentUsedOn = (pad) => {
const commentsUsed = [];
pad.pool.eachAttrib((key, value) => { if (key === 'comment') commentsUsed.push(value); });
return commentsUsed;
};
// Add the props to be supported in export
exports.exportHtmlAdditionalTagsWithData =
async (hookName, pad) => findAllCommentUsedOn(pad).map((name) => ['comment', name]);
exports.getLineHTMLForExport = async (hookName, context) => {
if (settings.ep_comments_page && settings.ep_comments_page.exportHtml === false) return;
// I'm not sure how optimal this is - it will do a database lookup for each line..
const {comments} = await commentManager.getComments(context.padId);
let hasPlugin = false;
// Load the HTML into a throwaway div instead of calling $.load() to avoid
// https://github.com/cheeriojs/cheerio/issues/1031
const content = $('<div>').html(context.lineContent);
// include links for each comment which we will add content later.
content.find('span').each(function () {
const span = $(this);
const commentId = span.data('comment');
if (!commentId) return; // not a comment. please optimize me in selector
if (!comments[commentId]) return; // if this comment has been deleted..
hasPlugin = true;
span.append(
$('<sup>').append(
$('<a>').attr('href', `#${commentId}`).text('*')));
// Replace data-comment="foo" with class="comment foo".
if (/^c-[0-9a-zA-Z]+$/.test(commentId)) {
span.removeAttr('data-comment').addClass('comment').addClass(commentId);
}
});
if (hasPlugin) context.lineContent = content.html();
};
exports.exportHTMLAdditionalContent = async (hookName, {padId}) => {
if (settings.ep_comments_page && settings.ep_comments_page.exportHtml === false) return;
const {comments} = await commentManager.getComments(padId);
if (!comments) return;
const div = $('<div>').attr('id', 'comments');
for (const [commentId, comment] of Object.entries(comments)) {
div.append(
$('<p>')
.attr('role', 'comment')
.addClass('comment')
.attr('id', commentId)
.text(`* ${comment.text}`));
}
// adds additional HTML to the body, we get this HTML from the database of comments:padId
return $.html(div);
};