nodebb-plugin-markdown 13.2.7 → 13.2.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +62 -3
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -3,16 +3,29 @@
|
|
|
3
3
|
const MarkdownIt = require('markdown-it');
|
|
4
4
|
const fs = require('fs');
|
|
5
5
|
const path = require('path');
|
|
6
|
+
const crypto = require('crypto');
|
|
6
7
|
|
|
7
8
|
const nconf = nodebb.require('nconf');
|
|
8
9
|
const winston = nodebb.require('winston');
|
|
9
10
|
const meta = nodebb.require('./src/meta');
|
|
10
11
|
const activitypub = nodebb.require('./src/activitypub');
|
|
12
|
+
const db = nodebb.require('./src/database');
|
|
11
13
|
const plugins = nodebb.require('./src/plugins');
|
|
12
14
|
|
|
13
15
|
const SocketPlugins = nodebb.require('./src/socket.io/plugins');
|
|
14
16
|
SocketPlugins.markdown = require('./websockets');
|
|
15
17
|
|
|
18
|
+
const strongCharRegex = /\p{L}/u;
|
|
19
|
+
const rtlCharRegex = /[\p{Script=Hebrew}\p{Script=Arabic}\p{Script=Syriac}\p{Script=Thaana}\p{Script=Nko}]/u;
|
|
20
|
+
|
|
21
|
+
function isRTL(text) {
|
|
22
|
+
const match = text.match(strongCharRegex);
|
|
23
|
+
return !!match && rtlCharRegex.test(match[0]);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const md5 = filename => crypto.createHash('md5').update(filename).digest('hex');
|
|
27
|
+
const resizedSuffix = /-resized(\.[\w]+)?$/;
|
|
28
|
+
|
|
16
29
|
let parser;
|
|
17
30
|
let app;
|
|
18
31
|
const Markdown = {
|
|
@@ -160,7 +173,7 @@ const Markdown = {
|
|
|
160
173
|
let env = {
|
|
161
174
|
parse: true,
|
|
162
175
|
type: data.type,
|
|
163
|
-
images: new Map(),
|
|
176
|
+
images: new Map(),
|
|
164
177
|
};
|
|
165
178
|
|
|
166
179
|
({ env } = await plugins.hooks.fire('filter:markdown.beforeParse', { env, data: Object.freeze({ ...data }) }));
|
|
@@ -178,9 +191,32 @@ const Markdown = {
|
|
|
178
191
|
}
|
|
179
192
|
}
|
|
180
193
|
|
|
194
|
+
if (env.parse && data.postData) {
|
|
195
|
+
await Markdown.addUploadSizes(env.images, data.postData.uploads);
|
|
196
|
+
}
|
|
197
|
+
|
|
181
198
|
return env;
|
|
182
199
|
},
|
|
183
200
|
|
|
201
|
+
// Adds the sizes of the post's own uploads to `images`, keyed by filename.
|
|
202
|
+
// They are recorded on upload by posts.uploads.saveSize, so this is a single
|
|
203
|
+
// read and never touches the image itself. Entries already present (added by
|
|
204
|
+
// filter:markdown.beforeParse) are left alone.
|
|
205
|
+
addUploadSizes: async (images, uploads) => {
|
|
206
|
+
if (!Array.isArray(uploads) || !uploads.length) {
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
const sizes = await db.getObjects(uploads.map(filePath => `upload:${md5(filePath)}`));
|
|
211
|
+
uploads.forEach((filePath, idx) => {
|
|
212
|
+
const size = sizes[idx];
|
|
213
|
+
const filename = path.basename(filePath);
|
|
214
|
+
if (size && size.width && size.height && !images.has(filename)) {
|
|
215
|
+
images.set(filename, { width: size.width, height: size.height });
|
|
216
|
+
}
|
|
217
|
+
});
|
|
218
|
+
},
|
|
219
|
+
|
|
184
220
|
afterParse: function (payload) {
|
|
185
221
|
if (!payload) {
|
|
186
222
|
return payload;
|
|
@@ -272,9 +308,18 @@ const Markdown = {
|
|
|
272
308
|
|
|
273
309
|
parser.use((md) => {
|
|
274
310
|
md.core.ruler.before('linkify', 'autodir', (state) => {
|
|
275
|
-
state.tokens.forEach((token) => {
|
|
311
|
+
state.tokens.forEach((token, index) => {
|
|
276
312
|
if (token.type === 'paragraph_open') {
|
|
277
|
-
|
|
313
|
+
const inline = state.tokens[index + 1];
|
|
314
|
+
// `dir="auto"` resolves direction from the first strong character of the
|
|
315
|
+
// rendered HTML, which skips content isolated in `<bdi>` (e.g. mentions),
|
|
316
|
+
// so a paragraph containing only an RTL mention falls back to LTR.
|
|
317
|
+
// The source text still has that character, so detect RTL here instead.
|
|
318
|
+
if (inline && inline.type === 'inline' && isRTL(inline.content)) {
|
|
319
|
+
token.attrJoin('dir', 'rtl');
|
|
320
|
+
} else {
|
|
321
|
+
token.attrJoin('dir', 'auto');
|
|
322
|
+
}
|
|
278
323
|
}
|
|
279
324
|
});
|
|
280
325
|
});
|
|
@@ -304,6 +349,20 @@ const Markdown = {
|
|
|
304
349
|
|
|
305
350
|
token.attrSet('class', `${token.attrGet('class') || ''} img-fluid img-markdown`);
|
|
306
351
|
|
|
352
|
+
// Give the image its intrinsic size so it reserves the space it will
|
|
353
|
+
// take before it loads, otherwise images finishing above the viewport
|
|
354
|
+
// shift the content below them. A resized variant is not tracked
|
|
355
|
+
// separately, but it keeps the original's ratio, which is all that
|
|
356
|
+
// matters while these are rendered with `height: auto`
|
|
357
|
+
if (env.images && env.images.size) {
|
|
358
|
+
const filename = path.basename(new URL(attributes.get('src'), nconf.get('url')).pathname);
|
|
359
|
+
const size = env.images.get(filename) || env.images.get(filename.replace(resizedSuffix, '$1'));
|
|
360
|
+
if (size) {
|
|
361
|
+
token.attrSet('width', size.width);
|
|
362
|
+
token.attrSet('height', size.height);
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
|
|
307
366
|
return renderImage(tokens, idx, options, env, self);
|
|
308
367
|
};
|
|
309
368
|
|