forked from netlify/staticgen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatic.config.js
240 lines (222 loc) · 6.92 KB
/
static.config.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import React, { Component } from 'react'
import { ServerStyleSheet } from 'styled-components'
import { map, mapValues, find, uniq, sortBy, pick, pickBy, flatMap } from 'lodash'
import decamelize from 'decamelize'
import dateFns from 'date-fns'
import { toSlug } from 'Scripts/util'
import grayMatter from 'gray-matter'
import marked from 'marked'
import yaml from 'js-yaml'
import titleCase from 'title-case'
import siteYaml from './site.yaml'
import fetchArchive from './scripts/fetch-archive'
import * as projectsMarkdown from './content/projects/*.md'
import * as pagesMarkdown from './content/pages/*.md'
import promoMarkdown from './content/promo.md'
import footerMarkdown from './content/footer.md'
const siteConfig = yaml.safeLoad(siteYaml)
const pageCache = {}
function processMarkdown (markdown, key) {
if (!pageCache[key]) {
const { content, data } = grayMatter(markdown)
const html = marked(content)
pageCache[key] = { content: html, key: decamelize(key, '-'), ...data }
}
return pageCache[key]
}
function mapProjectFrontMatter ({
title,
repo,
repohost,
homepage,
description,
startertemplaterepo,
content,
twitter,
}) {
const repoHost = repohost || 'github'
let starterTemplateRepo = startertemplaterepo
if (starterTemplateRepo && !starterTemplateRepo.includes(':')) {
if (repoHost === 'github') {
starterTemplateRepo = `https://github.com/${starterTemplateRepo}`
} else if (repoHost === 'gitlab') {
starterTemplateRepo = `https://gitlab.com/${starterTemplateRepo}`
}
}
return {
title,
slug: toSlug(title),
repo,
repoHost,
homepage,
twitter,
description,
starterTemplateRepo,
content,
}
}
function extractRelevantProjectData (data) {
return mapValues(data, project => {
const timestamps = map(project, 'timestamp')
const newestTimestamp = dateFns.max(...timestamps).getTime()
const oldestTimestamp = dateFns.min(...timestamps).getTime()
const dataAgeInDays = dateFns.differenceInDays(Date.now(), oldestTimestamp)
const {
followers, forks, stars, issues, repo
} = find(project, { timestamp: newestTimestamp }) || {}
const {
forks: forksPrevious,
stars: starsPrevious,
issues: issuesPrevious,
followers: followersPrevious,
} = find(project, { timestamp: oldestTimestamp }) || {}
const relevantData = {
followers,
forks,
stars,
issues,
forksPrevious,
starsPrevious,
issuesPrevious,
followersPrevious,
dataAgeInDays,
}
if (repo) relevantData.repo = repo
return relevantData
})
}
/**
* Retrieve and format all project data.
*/
async function getProjects () {
/**
* Get project details from frontmatter.
*/
const projectDetails = map(projectsMarkdown, processMarkdown)
/**
* Get external project data.
*/
const projectDataRaw = await fetchArchive(projectDetails)
const projectData = extractRelevantProjectData(projectDataRaw)
/**
* Combine project details from frontmatter and project data from
* external sources, using the project's key derived from its Markdown
* filename.
*/
const projects = projectDetails.map(project => {
const data = projectData[project.key]
const fieldValues = pick(project, map(siteConfig.fields, 'name'))
const mappedProject = mapProjectFrontMatter(project)
const filteredProject = pickBy({ ...mappedProject, ...data }, val => val)
return { fieldValues, ...filteredProject, ...data }
})
return projects
}
/**
* Generate dropdown filter values from frontmatter values.
*/
function generateFilters (projects) {
return siteConfig.filters.map(filter => ({
...filter,
values: sortBy(uniq(flatMap(projects, `fieldValues.${filter.field}`))),
}))
}
/**
* Retrieve and format markdown for unique pages.
*/
function getPages () {
const pages = map(pagesMarkdown, processMarkdown)
return pages.map(page => {
const path = `/${page.key}`
return { path, name: titleCase(path), ...page }
})
}
export default {
getSiteData: () => ({
title: siteConfig.title,
subtitle: siteConfig.subtitle,
titleHome: siteConfig.titleHome,
repo: siteConfig.repo,
shareUrlSite: siteConfig.url,
shareTextSite: siteConfig.shareText,
shareButtons: siteConfig.shareButtons,
pages: getPages(),
navLinks: siteConfig.navLinks,
footerText: marked(footerMarkdown),
copyrightName: siteConfig.copyrightName,
}),
getRoutes: async () => {
const projects = await getProjects()
const pages = getPages()
const promo = marked(promoMarkdown)
return [
{
path: '/',
component: 'src/Home/Home',
getData: () => ({
projects,
promo,
sorts: siteConfig.sorts,
filters: generateFilters(projects),
fields: siteConfig.fields,
}),
children: projects.map(project => ({
path: project.slug,
component: 'src/Project/Project',
getData: () => ({
...project,
fields: siteConfig.fields,
shareUrl: `${siteConfig.url}/${project.slug}`,
shareText: `${siteConfig.shareTextProjectStart}${project.title}${siteConfig.shareTextProjectEnd}`,
}),
})),
},
...pages.map(({ key, title, content }) => ({
path: `/${key}`,
component: 'src/Page',
getData: () => ({
title,
content,
}),
})),
{
is404: true,
component: 'src/App/404',
},
]
},
renderToHtml: (render, Comp, meta) => {
const sheet = new ServerStyleSheet()
const html = render(sheet.collectStyles(<Comp />))
meta.styleTags = sheet.getStyleElement()
return html
},
siteRoot: siteConfig.url,
Document: class CustomHtml extends Component {
render () {
const {
Html, Head, Body, children, renderMeta,
} = this.props
return (
<Html>
<Head>
<meta charSet="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link type="image/x-icon" rel="shortcut icon" href="favicon.ico" />
<meta content="IE=edge,chrome=1" httpEquiv="X-UA-Compatible" />
<meta name="twitter:card" value={siteConfig.description} />
<meta property="og:title" content={siteConfig.title} />
<meta property="og:type" content="website" />
<meta property="og:image" content={`/images/${siteConfig.socialPreviewImage}`} />
<meta property="og:url" content={siteConfig.url} />
<meta property="og:description" content={siteConfig.description} />
<link href="//fonts.googleapis.com/css?family=Roboto+Slab:700" rel="stylesheet" type="text/css" />
<link href="//fonts.googleapis.com/css?family=Roboto:100,400,600,700" rel="stylesheet" type="text/css" />
{renderMeta.styleTags}
</Head>
<Body>{children}</Body>
</Html>
)
}
},
}