-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.babel.js
125 lines (111 loc) · 3.66 KB
/
gulpfile.babel.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
113
114
115
116
117
118
119
120
121
122
123
124
125
/*jshint esversion: 6 */
import gulp from 'gulp';
import babel from 'gulp-babel';
import through from 'through2';
import path from 'path';
import gutil from 'gulp-util';
import {
File,
PluginError
} from 'gulp-util';
import algoliasearch from 'algoliasearch';
import * as dotenv from 'dotenv';
import matter from 'gray-matter';
import "isomorphic-fetch";
var commits_url = 'https://api.github.com/repos/cosname/cosx.org/commits?since=' +
curday + 'T00:00:00Z';
console.log(commits_url)
dotenv.load();
const PATH = {
content: "docs/content/**/*.md",
index: "dist/index.json"
};
function getAliases(datum, file) {
// Hugo use slug as url
// if not available, will use filename as url
if (datum.data.slug === undefined) {
return path.basename(file.path, '.md');
}
// console.log(datum)
return datum.data.slug;
}
function capitalize(value, dat) {
// console.log(value)
// console.log(dat)
var baseurl = "https://cosx.org/"
if(value.length < 13){
return baseurl + "" + value.slice(0,value.length-3)
}else if(value.length == 14){
return baseurl + "chinar/" + value.slice(0,value.length-3)
}else if(dat.date === undefined){
return baseurl +
value.slice(0, 4) + "/" +
value.slice(5, 7) + "/" +
value.slice(11, value.length-3)
}else{
return baseurl +
dat.date.toString().slice(0, 4) + "/" +
dat.date.toString().slice(5, 7) + "/" +
dat.slug
}
}
// function getAliases2(datum, file) {
// // Hugo use slug as url
// // if not available, will use filename as url
// if (datum.data.title === undefined) {
// return path.basename(file.path, '.md');
// }
//
// return datum.data.title;
// }
gulp.task('index', () => {
console.log("Starting to index rezhajulio.id ...");
let index = [];
gulp.src(PATH.content)
.pipe(through.obj(function (file, enc, cb) {
this.push(file);
let datum = matter(file.contents.toString(), {
lang: 'yaml',
delims: ['---', '---']
});
// console.log(datum)
if (datum.data.draft !== true) {
index.push({
content: datum.content,
uri: getAliases(datum, file),
title: datum.data.title,
autor: datum.data.author,
date: datum.data.date,
description: datum.data.description,
url: capitalize(path.basename(file.path), datum.data),
objectID: path.basename(file.path)
});
}
cb();
}, function endStream(cb) {
let indexFile = new File({
base: path.join(__dirname, './dist/'),
cwd: __dirname,
path: path.join(__dirname, PATH.index)
});
indexFile.contents = new Buffer(JSON.stringify(index));
let client = algoliasearch(process.env.ALGOLIA_API_KEY, process.env.ALGOLIA_API_SECRET);
let db = client.initIndex('cosx.org');
fetch(commits_url )
.then(resp=>resp.json())
.then(commits=>{
console.log(commits)
if (commits.length > 0) {
db.saveObjects(index, (err, content) => {
if (err) {
console.log(err);
}
cb();
});
}
})
// console.log("indexing to algolia...");
this.push(indexFile);
}))
.pipe(gulp.dest('dist'));
});