-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
/
Copy pathimage.js
48 lines (38 loc) · 1.19 KB
/
image.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
import { getAndRemoveConfig } from '../utils';
import { isAbsolutePath, getPath, getParentPath } from '../../router/util';
export const imageCompiler = ({ renderer, contentBase, router }) =>
(renderer.image = (href, title, text) => {
let url = href;
let attrs = [];
const { str, config } = getAndRemoveConfig(title);
title = str;
if (config['no-zoom']) {
attrs.push('data-no-zoom');
}
if (title) {
attrs.push(`title="${title}"`);
}
if (config.size) {
const [width, height] = config.size.split('x');
if (height) {
attrs.push(`width="${width}" height="${height}"`);
} else {
attrs.push(`width="${width}"`);
}
}
if (config.class) {
attrs.push(`class="${config.class}"`);
}
if (config.id) {
attrs.push(`id="${config.id}"`);
}
if (!isAbsolutePath(href, config.absolute)) {
url = getPath(contentBase, getParentPath(router.getCurrentPath()), href);
}
if (attrs.length > 0) {
return `<img src="${url}" data-origin="${href}" alt="${text}" ${attrs.join(
' '
)} />`;
}
return `<img src="${url}" data-origin="${href}" alt="${text}"${attrs}>`;
});