-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgulpfile.js
More file actions
87 lines (82 loc) · 2.09 KB
/
gulpfile.js
File metadata and controls
87 lines (82 loc) · 2.09 KB
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
"use strict";
const gulp = require("gulp");
const GH_API_TOKEN = process.env.GH_API_TOKEN;
gulp.task("github:issues", getGitHubIssues);
async function getGitHubIssues() {
const graphql = require("graphql.js");
const graph = graphql("https://api.github.com/graphql", {
headers: {
Authorization: `Bearer ${GH_API_TOKEN}`,
"User-Agent": "pioug"
},
asJSON: true
});
const query = graph(
`
query ($number_of_issues: Int!) {
repository(owner:"pioug", name:"blog") {
issues(last:20) {
edges {
node {
title
createdAt,
body,
bodyText,
labels(first: $number_of_issues) {
edges {
node {
name
}
}
}
}
}
}
}
}
`
);
const {
repository: {
issues: { edges: issues }
}
} = await query({
number_of_issues: 100
});
issues
.filter(({ node: { labels: { edges: labels } } }) =>
labels.some(({ name }) => "posts")
)
.forEach(function({ node: issue }) {
const fs = require("fs");
const slugify = require("slugify");
const { title, createdAt, body, bodyText, labels } = issue;
const description = bodyText
.replace(/\s+/g, " ")
.split(" ")
.reduce(
(res, token) => (res.length > 200 ? `${res}` : res + " " + token),
""
);
const md = [
`---`,
`title: ${title}`,
`date: ${createdAt.toString()}`,
`description: ${
description.length >= 200 ? description + "..." : description
}`,
`layout: post.njk`,
`tags:`,
`${labels.edges.map(label => ` - ${label.node.name}`).join("\n")}`,
`---`,
`${body}`
].join("\n");
const filename =
slugify(title, {
remove: /[.,\/#!$%\^&\*;:{}=\-_`~()]/g,
lower: true
}) + ".md";
console.log(`Write ${filename}`);
fs.writeFileSync(`./posts/${filename}`, md);
});
}