This repository has been archived by the owner on Aug 10, 2022. It is now read-only.
/
index.js
95 lines (75 loc) 路 2.28 KB
/
index.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
const visit = require('unist-util-visit');
const {resolve} = require('path');
const countries = require('countries-list').countries;
const globby = require('globby');
const langs = require('languages');
const { pathPrefix } = require('../../gatsby-config');
const alternateCodes = {
'in': 'id',
'iw': 'he',
'nb': 'no',
};
function replaceValue(array) {
const htmlValue = array.map(item => `<li>
<svg class="lexicon-icon lexicon-icon-${item.icon}">
<use xlink:href="${pathPrefix}/images/icons/icons.svg#${item.icon}" />
</svg>
<span>${item.name}</span>
</li>`);
return htmlValue.join(' ');
}
function getFiles(dir, glob) {
const files = globby.sync(dir, {
expandDirectories: {
files: glob || [],
extensions: ['svg'],
},
});
const filelist = [];
files.forEach(file => {
const name = file.replace(/\.svg$/, '');
const nameWithoutExtension = name.replace(dir + '/', '');
filelist.push({
name: nameWithoutExtension,
icon: nameWithoutExtension,
});
});
return filelist;
}
module.exports = ({markdownAST}) => {
visit(markdownAST, 'html', node => {
const foreachFlags = '[foreach Flags]';
const foreachIcons = '[foreach Icons]';
const path = resolve(__dirname, '../../static/images/icons');
if (node.value.includes(foreachFlags)) {
let dataFlags = getFiles(path, ['flags-*']);
dataFlags = dataFlags.map(item => {
const parts = item.name.split('-');
const langCode = parts[1];
const countryCode = parts[2];
let lang = langs.getLanguageInfo(alternateCodes[langCode] || langCode).name;
if (countryCode) {
const country = countries[countryCode.toUpperCase()];
if (country) {
lang += ' (' + country.name + ')';
}
}
let icon = langCode + '-' + countryCode;
return {
name: lang + ` (${icon.toLowerCase()})`,
icon: icon.toLowerCase(),
};
});
node.value = node.value.replace(foreachFlags, replaceValue(dataFlags));
}
if (node.value.includes(foreachIcons)) {
let dataIcons = getFiles(path, ['*']);
dataIcons = dataIcons.map(item => {
if (!item.name.includes('flags') && item.name !== 'icons') {
return item;
}
}).filter(value => typeof value !== 'undefined');
node.value = node.value.replace(foreachIcons, replaceValue(dataIcons));
}
});
};