forked from visioncan/hexo-tag-flickr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flickrTagUtil.js
112 lines (97 loc) · 2.72 KB
/
flickrTagUtil.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
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
/* eslint-disable linebreak-style */
'use strict';
const rPhotoId = /\d{5,}/;
const rPhotoSize = /^([sqtmnwzcbhkfo-]|3k|4k|5k|6k)$/;
const cleanProtocol = function(src) {
return src.replace('https:', '');
};
const flickrTagUtil = {
convertAttr: function(args, defaultSize, useSrcset) {
const attrs = {
classes: [],
id: '',
size: defaultSize,
useSrcset: useSrcset
};
let i = 0;
for (i = 0; i < args.length; i++) {
const item = args[i];
if (rPhotoId.test(item)) {
attrs.id = item;
break;
} else {
attrs.classes.push(item);
}
}
args = args.slice(i + 1);
if (args.length) {
if (rPhotoSize.test(args[0])) {
attrs.size = args.shift();
}
// TODO: with link
}
return attrs;
},
imgFormat: function(tag, jsonData) {
const imgAttr = {};
imgAttr.photoId = tag.id;
imgAttr.src = cleanProtocol(jsonData.selectedSize.source);
imgAttr.width = jsonData.selectedSize.width;
if (tag.useSrcset) {
imgAttr.srcset = jsonData.sizeList.map(e => cleanProtocol(e.source) + ' ' + e.width + 'w').join(', ');
}
imgAttr.class = tag.classes.join(' ');
return imgAttr;
},
// get image size
getImage: function(flickrJson, photo_size) {
const sizeTable = {
's': 'Square',
'q': 'Large Square',
't': 'Thumbnail',
'm': 'Small',
'n': 'Small 320',
'w': 'Small 400',
'-': 'Medium',
'z': 'Medium 640',
'c': 'Medium 800',
'b': 'Large',
'h': 'Large 1600',
'k': 'Large 2048',
'3k': 'Extra Large 3072',
'4k': 'Extra Large 4096',
'f': 'VR 4K',
'5k': 'Extra Large 5120',
'6k': 'Extra Large 6144',
'o': 'Original'
};
const returnValue = {};
returnValue.sizeList = [];
if (flickrJson && flickrJson.sizes.size) {
// this code assumes that the sizes provided by flickr API are in ascending order
for (let i = 0; i < flickrJson.sizes.size.length; i++) {
returnValue.sizeList.push(flickrJson.sizes.size[i]);
returnValue.selectedSize = flickrJson.sizes.size[i];
if (flickrJson.sizes.size[i].label === sizeTable[photo_size]) {
break;
}
}
}
return returnValue;
},
toBase58: function(num) {
if (typeof num !== 'number') num = parseInt(num, 10);
let enc = '';
const alpha = '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ';
let div = num;
let mod;
while (num >= 58) {
div = num / 58;
mod = num - (58 * Math.floor(div));
enc = '' + alpha.substr(mod, 1) + enc;
num = Math.floor(div);
}
return div ? '' + alpha.substr(div, 1) + enc : enc;
}
};
module.exports = flickrTagUtil;