-
Notifications
You must be signed in to change notification settings - Fork 4
/
addMetaTags.js
37 lines (31 loc) · 1.15 KB
/
addMetaTags.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
const fs = require('fs');
let locales = require('./src/locale/locales.json');
let defaultLocale = 'en-US';
for (let locale of locales) {
let filePath;
if (locale === defaultLocale) {
filePath = `./dist/betoken-landing-page/index.html`;
} else {
filePath = `./dist/betoken-landing-page/${locale}/index.html`;
}
let fileData = fs.readFileSync(filePath, 'utf-8');
let headIdx = fileData.indexOf('<head>') + 6;
// insert meta tags
let metaTags = require(`./src/locale/meta.${locale}.json`);
for (let tagData of metaTags) {
let tag = '\n <meta ';
let keys = Object.keys(tagData);
for (let key of keys) {
tag += `${key}="${tagData[key]}" `;
}
tag += '/>\n';
fileData = fileData.slice(0, headIdx) + tag + fileData.slice(headIdx);
}
// insert title
let titleText = require(`./src/locale/title.${locale}.json`);
fileData = fileData.slice(0, headIdx) + `\n <title>${titleText}</title>` + fileData.slice(headIdx);
// insert language tag
let htmlIdx = fileData.indexOf('lang="') + 6;
fileData = fileData.slice(0, htmlIdx) + locale + fileData.slice(htmlIdx);
fs.writeFileSync(filePath, fileData, 'utf-8');
}