-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatic.config.js
190 lines (180 loc) · 5.47 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
import React from 'react'
import path from 'path'
import { reloadClientData } from 'react-static/node'
import chokidar from 'chokidar'
import marked from 'marked'
import jdown from 'jdown'
import SanitizeState from 'marked-sanitizer-github'
import emoji from 'node-emoji'
import prism from 'prismjs'
import 'prismjs/components/prism-git'
import 'prismjs/components/prism-markdown'
import 'prismjs/components/prism-python'
import 'prismjs/components/prism-tsx'
import 'prismjs/components/prism-typescript'
import 'prismjs/components/prism-vim'
// Typescript support in static.config.js is not yet supported, but is coming in a future update!
if (process.env.REACT_STATIC_ENV === 'development') {
chokidar.watch('contents', {
depth: 5,
usePolling: true,
}).on('all', data => {
return reloadClientData()
})
}
/**
* @type { [key: string]: string }
*/
const entityMap = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": ''',
'/': '/'
}
const createRenderer = () => {
const renderer = new marked.Renderer()
renderer.heading = (text, level) => {
const escapedText = text.toLowerCase().replace(/[\s+,.*+?^${}()|[\]\\]+/g, '-')
return `
<h${level} id="${escapedText}">
<a class="anchor" href="#${escapedText}">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">
<path d="M0 0h24v24H0z" fill="none"/>
<path d="M3.9 12c0-1.71 1.39-3.1 3.1-3.1h4V7H7c-2.76 0-5 2.24-5 5s2.24 5 5 5h4v-1.9H7c-1.71 0-3.1-1.39-3.1-3.1zM8 13h8v-2H8v2zm9-6h-4v1.9h4c1.71 0 3.1 1.39 3.1 3.1s-1.39 3.1-3.1 3.1h-4V17h4c2.76 0 5-2.24 5-5s-2.24-5-5-5z"/>
</svg>
</a>
<span>${text}</span>
</h${level}>`
}
renderer.link = (href, title, text) => {
return `
<a href="${href}" title="${title ? title : text}" target="_blank" rel="nofollow noopener">
${text}
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 26 26" class="icon">
<path d="M0 0h24v24H0z" fill="none"/>
<path d="M19 19H5V5h7V3H5c-1.11 0-2 .9-2 2v14c0 1.1.89 2 2 2h14c1.1 0 2-.9 2-2v-7h-2v7zM14 3v2h3.59l-9.83 9.83 1.41 1.41L19 6.41V10h2V3h-7z"/>
</svg>
</a>
`
}
renderer.text = text => {
return text.replace(/(:.*:)/g, (match) => emoji.emojify(match))
}
return renderer
}
const config = {
entry: path.join(__dirname, 'src', 'index.tsx'),
maxThreads: 1,
basePath: '/',
Document: ({ Html, Head, Body, children }) => (
<Html lang="ja-JP">
<Head>
<meta charSet="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>{'My site'}</title>
<meta name="description" content={'Build with React-Static, Material-UI, Emotion etc'} />
<meta property="og:type" content={'website'} />
<meta property="og:title" content={'My site'} />
<meta property="og:description" content={'Build with React-Static, Material-UI, Emotion etc'} />
</Head>
<Body>
{children}
</Body>
</Html>
),
getRoutes: async () => {
const renderer = createRenderer()
const { about, home, posts: contents } = await jdown('contents', {
fileInfo: true,
markdown: {
renderer,
sanitizer: new SanitizeState().getSanitizer(),
headerPrefix: 'user-content-',
highlight: (code /*: string */, lang /*: string */) => {
const languages = lang ? prism.languages[lang] : ''
return prism.highlight(
lang
? code
: code.replace(/[&<>"'\/]/g, key =>
key in entityMap ? entityMap[key] : key
),
languages,
lang
)
},
},
})
// Sort by created date
const posts = contents.sort((a, b) => (
a.date < b.date ? 1 : -1
))
return [
{
path: '/',
template: 'src/containers/Home',
getData: () => ({
posts: posts.slice(0, 3), // Show 3 entries.
}),
},
{
path: '/about/',
template: 'src/containers/About',
getData: () => ({
about,
}),
},
{
path: '/posts/',
template: 'src/containers/Posts',
getData: () => ({
posts,
}),
children: posts.map((post, i) => {
const prevId = i + 1
const nextId = i - 1
const prev = prevId in posts
? { slug: posts[prevId].slug, title: posts[prevId].title }
: undefined
const next = nextId in posts
? { slug: posts[nextId].slug, title: posts[nextId].title }
: undefined
return {
path: `/${post.slug}`,
template: 'src/containers/Post',
getData: () => ({
post,
prev,
next,
}),
}
}),
},
{
path: '404',
template: 'src/containers/Errors',
}
]
},
plugins: [
require.resolve('react-static-plugin-typescript'),
[
require.resolve('react-static-plugin-source-filesystem'),
{
location: path.resolve('./src/pages'),
},
],
require.resolve('react-static-plugin-react-router'),
require.resolve('react-static-plugin-sitemap'),
'material-ui',
[
require.resolve('react-static-plugin-mdx'),
{
includePaths: [path.resolve('./slides')],
extensions: ['.md', '.mdx'],
}
]
],
}
export default config