-
-
Notifications
You must be signed in to change notification settings - Fork 128
Expand file tree
/
Copy pathpage.js
More file actions
391 lines (376 loc) · 12.3 KB
/
page.js
File metadata and controls
391 lines (376 loc) · 12.3 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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
import React from "react";
import Link from "gatsby-link";
import Helmet from "react-helmet";
import SiteFooter from "../components/SiteFooter";
import SiteHeader from "../components/SiteHeader";
import ExamplesViewer from "../components/ExamplesViewer";
import ContactAndMailingList from "../components/ContactAndMailingList";
import Layout from "../components/Layout";
import { graphql } from "gatsby";
const sectionIs = desiredSection => ({ sectionId }) =>
sectionId === desiredSection;
const AugmentedText = ({ children, noLink }) => (
<span
dangerouslySetInnerHTML={{
__html: processHTML(htmlerize(children), noLink),
}}
/>
);
function PageList({ pages, location, depth = 0 }) {
return (
<ul className={`page-list nav flex-column ${depth === 0 ? "mb5" : null}`}>
{pages.map(({ to, title, subpages }, idx) => (
<li key={idx} className="f6 lh-copy pv1">
<Link
className={`nav-link ${location.pathname === to ? "active" : ""}`}
to={to}
>
<AugmentedText>{title}</AugmentedText>
</Link>
{subpages && subpages.length ? (
<PageList pages={subpages} location={location} depth={depth + 1} />
) : null}
</li>
))}
</ul>
);
}
const Nav = ({ sections, pages, location }) => (
<aside className="sidebar col-xs-12 col-md-3 last-xs mt3">
{sections.map(({ id, title }, idx) => (
<section key={idx}>
<h4 className="f6 ttu fw6 mt0 mb3 bb pb2">
<AugmentedText>{title}</AugmentedText>
</h4>
<div className="nested-list-reset">
<PageList location={location} pages={pages.filter(sectionIs(id))} />
</div>
</section>
))}
</aside>
);
const tag = (
name,
label = name,
noLink = false,
linkTarget = "/postgraphile/pricing/"
) =>
`<${
noLink ? "span" : "a href='" + linkTarget + "'"
} class="plan-${name}"><span class='first-letter'>${
label[0]
}</span><span class='rest'>${label.substr(1)}</span></${
noLink ? "span" : "a"
}>`;
function processHTML(html, noLink) {
return html
.replace(/\[SPON\]/g, tag("sponsor", "spon", noLink, "/sponsor/"))
.replace(/\[SUPPORTER\]/g, tag("supporter", "supporter", noLink))
.replace(/\[PRO\]/g, tag("pro", "pro", noLink))
.replace(/\[ENTERPRISE\]/g, tag("enterprise", "enterprise", noLink))
.replace(/^.* Gallery$/g, "<strong>$&</strong>");
}
function processTableOfContents(html) {
if (!html) return null;
return html
.replace(/\bPostgreSQL\b/g, "PG")
.replace(/\b([A-Z_]{5,})\b(?!<)/g, "<span class='allcaps'>$1</span>")
.replace(/(<\/?code[^>]*>[^()]+)\([^)]*\)(&#)/g, "$1(...)$2")
.replace(/<(\/?code)[^>]*>/g, "<$1>");
}
function htmlerize(text) {
return text
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}
const getNextPrev = (nav, pathname) => {
const allPages = nav.reduce((memo, page) => {
memo.push(page);
if (page.subpages) {
memo.push(...page.subpages);
}
return memo;
}, []);
const currentPage = allPages.find(({ to }) => to === pathname);
if (!currentPage) {
return {};
}
const currentIndex = allPages.indexOf(currentPage);
let next, nextText, prev, prevText;
if (currentIndex > 0) {
prev = allPages[currentIndex - 1].to;
prevText = allPages[currentIndex - 1].title;
}
if (currentIndex >= 0 && currentIndex < allPages.length - 1) {
next = allPages[currentIndex + 1].to;
nextText = allPages[currentIndex + 1].title;
}
return { next, nextText, prev, prevText };
};
class Page extends React.Component {
state = {
/*
* So... On production, if you load a page with a code sample containing <span
* class="gatsby-highlight-code-line"> then that span will be removed, and thus
* all the line breaks in highlighted areas of your code will be missing. But
* React still believe it's rendering it, and SSR definitely rendered it, so
* I've no idea where it actually goes. And of course you can't reproduce it on
* development. And if you navigate to the page it's fine - it's only when you
* link to it directly that it goes wrong. Sense: it makes none. Anyway, this
* hack might fix it... 🤷♂️
*/
hack: 1,
};
componentDidMount() {
setTimeout(() => {
this.setState({ hack: 2 });
}, 0);
}
render() {
const {
data: { remark, nav = { edges: [] }, examples } = {},
location,
history,
} = this.props;
const {
html: rawHTML = "",
tableOfContents: rawTableOfContents = "",
timeToRead = "",
frontmatter: { title = "", fullTitle = "", showExamples, noToc } = {},
} = remark ?? {};
const tableOfContents =
!noToc && timeToRead > 1
? processTableOfContents(rawTableOfContents)
: null;
const html = processHTML(rawHTML);
const [, navSection] = location.pathname.split("/");
const thisNavEdge = nav.edges.find(
({ node: { name } }) => name === navSection
);
const thisNav = (thisNavEdge && thisNavEdge.node) || {
pages: [],
sections: [],
};
const navPages = thisNav.pages;
const navSections = thisNav.sections || [];
const { next, nextText, prev, prevText } = getNextPrev(
navPages,
location.pathname
);
const isPostGraphileDocs = navSection === "postgraphile";
return (
<Layout {...this.props}>
<div
className={`template-page ${
location.pathname.match(
/^\/(postgraphile|news|support|sponsors?)(\/|$)/
)
? "postgraphile"
: ""
}`}
>
<Helmet
title={`${
isPostGraphileDocs ? "PostGraphile" : "Graphile"
} | ${title}`}
meta={[
{
name: "description",
content:
"Utilities to build powerful and performant GraphQL APIs",
},
{
name: "keywords",
content:
"GraphQL, API, Graph, PostgreSQL, PostGraphile, PostGraphQL, Postgres-GraphQL, server, plugins, introspection, reflection",
},
]}
/>
<SiteHeader location={location} history={history} />
<div className="page-content">
<section>
<div
style={{
display: location.pathname.match(/^\/news\//) ? "none" : "",
textAlign: "center",
}}
>
<div
className="book-a-call"
style={{
backgroundColor: "#2d80d3",
color: "white",
padding: "0.25rem",
}}
>
Need help or advice? We now offer Development Support, direct
from the maintainer{" "}
<Link
className="button--outline"
to="/support/"
style={{ backgroundColor: "white", margin: "0.25rem" }}
>
Get support
</Link>
</div>
</div>
<div className="container">
<div className="row between-xs">
<Nav
sections={navSections}
pages={navPages}
location={location}
/>
<div className="col-xs-12 col-md-9 first-xs main-content">
<div className="row">
<div className="col-xs-12" style={{ width: "100%" }}>
{tableOfContents && (
<div
key={String(this.state.hack) + "_toc"}
className="toc"
>
<h4>Table of Contents</h4>
<div
dangerouslySetInnerHTML={{
__html: tableOfContents,
}}
/>
</div>
)}
<div
className="edit-this-page"
style={{
display: location.pathname.match(/^\/news\//)
? "none"
: "",
}}
>
<a
href={`https://github.com/graphile/graphile.github.io/edit/develop/src/pages${location.pathname.substr(
0,
location.pathname.length - 1
)}.md`}
>
📝 Suggest improvement/edit this page
</a>
</div>
<h2 className="mt3">{fullTitle || title}</h2>
<div
key={this.state.hack}
dangerouslySetInnerHTML={{ __html: html }}
/>
</div>
<br />
{showExamples && (
<ExamplesViewer
examples={examples.edges
.filter(
({ node }) => node.category === showExamples
)
.map(({ node }) => node)}
/>
)}
<br />
<div className="col-xs-12 mt3 mb5">
<div className="row between-xs">
<div className="col-xs-6">
{prev ? (
<Link className="" to={prev}>
<span className="fas fa-fw fa-arrow-left" />{" "}
{prevText ? (
<AugmentedText noLink>
{prevText}
</AugmentedText>
) : (
"Previous"
)}
</Link>
) : null}
</div>
<div className="col-xs-6 tr">
{next ? (
<Link className="" to={next}>
{nextText ? (
<AugmentedText noLink>
{nextText}
</AugmentedText>
) : (
"Next"
)}{" "}
<span className="fas fa-fw fa-arrow-right" />
</Link>
) : null}
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</section>
<ContactAndMailingList />
</div>
<SiteFooter />
</div>
</Layout>
);
}
}
export const pageQuery = graphql`
query PageByPath($slug: String!) {
remark: markdownRemark(frontmatter: { path: { eq: $slug } }) {
html
tableOfContents
timeToRead
frontmatter {
path
title
fullTitle
showExamples
noToc
}
}
nav: allNavJson {
edges {
node {
id
name
sections {
id
title
}
pages {
to
title
sectionId
subpages {
to
title
sectionId
}
}
}
}
}
examples: allExamplesJson {
edges {
node {
category
id
title
examples {
title
example
exampleLanguage
result
resultLanguage
}
}
}
}
}
`;
export default Page;