-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path54.js
More file actions
116 lines (91 loc) · 2.8 KB
/
Copy path54.js
File metadata and controls
116 lines (91 loc) · 2.8 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
113
114
115
116
// var visit = require('unist-util-visit');
var util = require('../util/util');
function createUniqueHash(hash, map) {
if(!map) return hash;
if(!map[hash]) {
map[hash] = 1;
}
else{
var nhash = hash;
while (map[nhash]) {
map[hash] = map[hash] + 1;
nhash = nhash + map[hash];
}
hash = nhash;
map[hash] = 1;
}
return hash;
}
function createPositionValue(node) {
if(!node || !node.position) return 0;
return [
node.position.start.line,
node.position.start.column,
node.position.end.line,
node.position.end.column
].join('-');
}
function getValues(node) {
var values = [];
node.hasOwnProperty('type') && values.push(node.type);
node.hasOwnProperty('tagName') && values.push(node.tagName);
node.hasOwnProperty('value') && values.push(node.value);
node.hasOwnProperty('depth') && values.push(node.depth);
node.hasOwnProperty('ordered') && values.push(node.ordered);
node.hasOwnProperty('start') && values.push(node.start);
node.hasOwnProperty('spread') && values.push(node.spread);
node.hasOwnProperty('checked') && values.push(node.checked);
node.hasOwnProperty('lang') && values.push(node.lang);
node.hasOwnProperty('meta') && values.push(node.meta);
node.hasOwnProperty('identifier') && values.push(node.identifier);
node.hasOwnProperty('label') && values.push(node.label);
node.hasOwnProperty('url') && values.push(node.url);
node.hasOwnProperty('title') && values.push(node.title);
node.hasOwnProperty('referenceType') && values.push(node.referenceType);
node.hasOwnProperty('alt') && values.push(node.alt);
if(node.align && node.align.length > 0){
values.push(node.align.join('-'));
}
if(values.length === 0) {
values.push(createPositionValue(node));
}
return values;
}
function all(nodes) {
var hashs = [];
var map = {};
for(var i=0;i<nodes.length;i++) {
var node = nodes[i];
var h = one(node, map);
hashs.push(h);
}
var hash = hashs.reduce(function (a, b) {
return a+b;
});
return hash;
}
function one(node, map) {
var hashs = [0];
if(node.children && node.children.length>0){
var hash1 = all(node.children);
hashs.push(hash1);
}
var values = getValues(node);
if(values.length>0){
var hash0 = util.hash(values.join('-'));
hashs.push(hash0);
}
var hash = hashs.reduce(function (a, b) {
return a+b;
});
hash = createUniqueHash(hash, map);
node.hash = hash;
return hash;
}
module.exports = function plugin(options = {}) {
return function transformer(root) {
console.time('hash');
one(root, {});
console.timeEnd('hash');
};
};