-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexportHTML.js
More file actions
112 lines (94 loc) · 3.83 KB
/
Copy pathexportHTML.js
File metadata and controls
112 lines (94 loc) · 3.83 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
'use strict';
const Changeset = require('ep_etherpad-lite/static/js/Changeset');
const Security = require('ep_etherpad-lite/static/js/security');
const http = require('http');
const https = require('https');
const fetchToBase64 = (src) => new Promise((resolve) => {
let url;
if (src.startsWith('//')) {
url = `http://localhost:9001${src.slice(1)}`;
} else if (src.startsWith('/')) {
url = `http://localhost:9001${src}`;
} else {
url = src;
}
const proto = url.startsWith('https') ? https : http;
proto.get(url, (res) => {
const chunks = [];
res.on('data', (chunk) => chunks.push(chunk));
res.on('end', () => {
const buffer = Buffer.concat(chunks);
const mime = (res.headers['content-type'] || 'image/png').split(';')[0];
resolve(`data:${mime};base64,${buffer.toString('base64')}`);
});
res.on('error', () => resolve(null));
}).on('error', () => resolve(null));
});
exports.getLineHTMLForExport = async (hook, context) => {
const attribLine = context.attribLine;
const apool = context.apool;
if (!attribLine) return;
let imgsHTML = '';
let lineFloatValue = null; // float of the first image on the line
const opIter = Changeset.opIterator(attribLine);
while (opIter.hasNext()) {
const op = opIter.next();
const imageSrcAttrib = Changeset.opAttributeValue(op, 'image', apool);
if (!imageSrcAttrib) continue;
try {
let decodedSrc = decodeURIComponent(imageSrcAttrib);
if (!decodedSrc) continue;
const isBase64 = decodedSrc.startsWith('data:');
const isHttp = decodedSrc.startsWith('http');
const isServerPath = decodedSrc.startsWith('/');
if (!isBase64 && !isHttp && !isServerPath) {
console.warn(`[ep_images_extended exportHTML] Invalid image src: ${decodedSrc}`);
continue;
}
if (!isBase64) {
const b64 = await fetchToBase64(decodedSrc);
if (!b64) {
console.warn(`[ep_images_extended exportHTML] Could not fetch image: ${decodedSrc}`);
continue;
}
decodedSrc = b64;
}
const imageWidthAttrib = Changeset.opAttributeValue(op, 'image-width', apool);
const imageHeightAttrib = Changeset.opAttributeValue(op, 'image-height', apool);
const imageIdAttrib = Changeset.opAttributeValue(op, 'image-id', apool);
const imageFloatAttrib = Changeset.opAttributeValue(op, 'image-float', apool);
if (lineFloatValue === null) {
lineFloatValue = imageFloatAttrib || 'none';
}
const escapedSrc = decodedSrc.replace(/&/g, '&').replace(/"/g, '"');
let tag = `<img src="${escapedSrc}"`;
if (imageWidthAttrib) {
tag += ` width="${Security.escapeHTMLAttribute(imageWidthAttrib.replace(/px$/, ''))}"`;
}
if (imageHeightAttrib) {
tag += ` height="${Security.escapeHTMLAttribute(imageHeightAttrib.replace(/px$/, ''))}"`;
}
if (imageIdAttrib) {
tag += ` data-image-id="${Security.escapeHTMLAttribute(imageIdAttrib)}"`;
}
tag += `>`;
imgsHTML += tag;
} catch (e) {
console.error(`[ep_images_extended exportHTML] Error processing image: ${imageSrcAttrib}`, e);
}
}
if (!imgsHTML) return;
if (lineFloatValue === 'left') {
context.lineContent = `<p style='float:left;margin:0 4px 4px 0'>${imgsHTML}</p>`;
} else if (lineFloatValue === 'right') {
context.lineContent = `<p style='float:right;margin:0 0 4px 4px'>${imgsHTML}</p>`;
} else {
// float:none or default — center the image
context.lineContent = `<p style='text-align:center'>${imgsHTML}</p>`;
}
};
exports.stylesForExport = (hook, padId, cb) => {
// height:auto keeps the aspect ratio when max-width:100% shrinks a wide image
// in a narrow viewport (otherwise the fixed height="" attribute squishes it).
cb('img { max-width: 100%; height: auto; vertical-align: middle; }');
};