-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathpost-build.js
More file actions
142 lines (122 loc) · 3.66 KB
/
Copy pathpost-build.js
File metadata and controls
142 lines (122 loc) · 3.66 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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/* @flow */
import fs from 'fs';
import path from 'path';
import {load as parseHtml} from 'cheerio';
import {parse as parseToml} from 'toml';
import _ from 'lodash/fp';
import frontMatter from 'front-matter';
import markdownIt from 'markdown-it';
import moment from 'moment';
import RSS from 'rss';
import sm from 'sitemap';
import {blogPostsFromPages} from './utils';
import {type BlogPostType} from './types';
const config = parseToml(
String(fs.readFileSync(path.join(__dirname, '/config.toml')))
);
const {siteTitle, siteDescription, hostname} = config;
const generateSitemapUrl = (page: mixed): ?Object => {
let pagePath;
if (typeof page === 'object' && page !== null) {
pagePath = page.path;
} else {
pagePath = page;
}
const nonIndexedPages = ['/404/'];
const importantPages = ['/', '/work/', '/blog/'];
const isRootPath = pagePath === '/';
const isNonIndexedPage = _.some(_.identity, [
_.includes(pagePath, nonIndexedPages),
_.some(
(nonIndexedPage: string) => _.startsWith(nonIndexedPage, pagePath),
nonIndexedPages
),
]);
const isImportantPage = _.includes(pagePath, importantPages);
if (!pagePath || isNonIndexedPage) return null;
return {
url: pagePath,
changefreq: isImportantPage ? 'daily' : 'monthly',
priority: isRootPath ? 1 : 0.85, // eslint-disable-line no-magic-numbers
};
};
const generateSitemap = (pages: $ReadOnlyArray<mixed>) => {
const sitemapUrls = _.flow(_.map(x => generateSitemapUrl(x)), _.compact)(
pages
);
const sitemap = sm.createSitemap({
hostname,
cacheTime: 600000,
urls: sitemapUrls,
});
fs.writeFileSync(
path.join(__dirname, '/public/sitemap.xml'),
sitemap.toString()
);
};
const generateFeed = (pages: $ReadOnlyArray<mixed>) => {
const feed = new RSS({
title: siteTitle,
description: siteDescription,
feed_url: `${hostname}/feed.xml`,
site_url: hostname,
copyright: '© 2018 ТОО «Anvilabs»',
language: 'ru',
pubDate: moment().toJSON(),
});
const md = markdownIt({
html: true,
linkify: true,
typographer: true,
});
_.flow(
blogPostsFromPages,
_.filter(({draft}: {draft?: boolean}) => !draft),
_.map((post: BlogPostType & {path: string, requirePath: string}) => {
// read the markdown file
const content = String(
fs.readFileSync(path.join(__dirname, `/pages/${post.requirePath}`))
);
// extract yaml meta tags
const meta = frontMatter(content);
// render markdown to html
const html = md.render(meta.body);
// replace relative links with absolute ones
const $ = parseHtml(html, {
recognizeSelfClosing: true,
decodeEntities: false,
});
$('img').each((idx: number, elem: Object) => {
const src = _.last(
$(elem)
.attr('src')
.split('./')
);
$(elem).attr('src', `${hostname}${post.path}${src}`);
});
return {...post, body: $.html()};
}),
_.forEach((post: BlogPostType & {path: string}) => {
feed.item({
..._.pick(['title', 'author', 'date'], post),
description: post.body,
url: `${hostname}${post.path}`,
});
})
)(pages);
fs.writeFileSync(
path.join(__dirname, './public/feed.xml'),
feed.xml({indent: true})
);
};
const generateRobots = () => {
const fileContent = `User-agent: *\nAllow: /\n\nSitemap: ${hostname}/sitemap.xml`;
fs.writeFileSync(path.join(__dirname, '/public/robots.txt'), fileContent);
};
const postBuild = (pages: $ReadOnlyArray<mixed>, callback: () => void) => {
generateSitemap(pages);
generateFeed(pages);
generateRobots();
callback();
};
export default postBuild;