This repository has been archived by the owner on Apr 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathampify.js
97 lines (66 loc) · 2.57 KB
/
ampify.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
'use strict';
var ampify = require('ampify');
var fs = require('fs');
var src = {
ua: "./dist/index.html",
ru: "./dist/ru/index.html",
en: "./dist/en/index.html"
},
dest = {
ua: "./dist/amp-index.html",
ru: "./dist/ru/amp-index.html",
en: "./dist/en/amp-index.html"
};
//var src = "./dist/index.html",
// dest = "./dist/amp-index.html";
//console.log(src.ua, dest.ua);
//writeAmpHtml(src.ua, dest.ua);
//writeAmpHtml(src.ru, dest.ru);
//writeAmpHtml(src.en, dest.en);
function prepareAmpHtml(lang,html,ampify) {
var options = {
cwd: (lang=='ua') ? "./dist/" : "./dist/ru/"// Assets (images/styles) file path
//cwd: "./dist/" // Assets (images/styles) file path
}, amphtml, canonical;
var ampifiedHtml = ampify(html, options);
if (lang == 'ua') {
amphtml = '<link rel="amphtml" href="http://merge.place/amp-index.html">',
canonical = '<link rel="canonical" href="http://merge.place/index.html">';
} else {
amphtml = '<link rel="amphtml" href="http://merge.place/'+lang+'/amp-index.html">',
canonical = '<link rel="canonical" href="http://merge.place/'+lang+'/index.html">';
}
var googleAnalyticsRegEx = /<script>[\s\S]*?<\/script>/ig;
var ampVideoTagRegEx = /<amp-video[\s\S]*?>[\s\S]*?<\/amp-video>/ig;
ampifiedHtml = ampifiedHtml.replace(amphtml, canonical);
ampifiedHtml = ampifiedHtml.replace(googleAnalyticsRegEx, '');
ampifiedHtml = ampifiedHtml.replace(ampVideoTagRegEx, '');
return ampifiedHtml
}
// UA
//var options = {
// cwd: "./dist/" // Assets (images/styles) file path
// },
// amphtml = '<link rel="amphtml" href="http://merge.place/amp-index.html">',
// canonical = '<link rel="canonical" href="http://merge.place/index.html">';
//
//var googleAnalyticsRegEx = /<script>[\s\S]*?<\/script>/ig;
//var ampVideoTagRegEx = /<amp-video[\s\S]*?>[\s\S]*?<\/amp-video>/ig;
fs.readFile(src.ua, 'utf8', function(err, html){
var ampifiedHtml = prepareAmpHtml('ua', html, ampify);
fs.writeFile(dest.ua, ampifiedHtml, function(err){
console.log('error: '+err);
});
});
fs.readFile(src.ru, 'utf8', function(err, html){
var ampifiedHtml = prepareAmpHtml('ru', html, ampify);
fs.writeFile(dest.ru, ampifiedHtml, function(err){
console.log('error: '+err);
});
});
fs.readFile(src.en, 'utf8', function(err, html){
var ampifiedHtml = prepareAmpHtml('en', html, ampify);
fs.writeFile(dest.en, ampifiedHtml, function(err){
console.log('error: '+err);
});
});