forked from javascript-tutorial/server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimgDescToAttrs.js
More file actions
executable file
·64 lines (45 loc) · 1.57 KB
/
Copy pathimgDescToAttrs.js
File metadata and controls
executable file
·64 lines (45 loc) · 1.57 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
'use strict';
/**
* Reads attrs from  into image token
*
* P.S. Plugins that work like  require special parser, not markdown-compatible markup
*/
const parseAttrs = require('../utils/parseAttrs');
const tokenUtils = require('../utils/token');
function readImgAttrs(state) {
for (let idx = 0; idx < state.tokens.length; idx++) {
let token = state.tokens[idx];
if (token.type !== 'inline') continue;
for (let i = 0; i < token.children.length; i++) {
let inlineToken = token.children[i];
if (inlineToken.type == 'image') {
processImg(inlineToken);
}
}
}
// doesn't work for 
// works for 
function processImg(imgToken) {
if (!imgToken.children.length) return; //  empty image
// last always textToken
let lastTextToken = imgToken.children[imgToken.children.length - 1];
let parts = lastTextToken.content.split('|');
if (parts.length != 2) { // no | or many || (invalid)
// try ', ' for tables
parts = lastTextToken.content.split(', ');
if (parts.length != 2) {
// still invalid
return;
}
}
lastTextToken.content = parts[0];
let attrs = parseAttrs(parts[1]);
for (let name in attrs) {
if (!state.md.options.html && ['height', 'width'].indexOf(name) == -1) continue;
tokenUtils.attrReplace(imgToken, name, attrs[name]);
}
}
}
module.exports = function(md) {
md.core.ruler.push('read_img_attrs', readImgAttrs);
};