forked from SaraVieira/starter-book
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
92 lines (82 loc) · 2.24 KB
/
index.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
import React from "react";
import { graphql } from "gatsby";
import Layout from "../components/Layout";
import SEO from "../components/SEO";
import ReactMarkdown from "react-markdown";
import Prism from "prismjs";
import "prismjs/components/prism-bash";
import "prismjs/components/prism-jsx";
import "prismjs/components/prism-typescript";
import "prismjs/components/prism-graphql";
import "prismjs/components/prism-tsx";
import "prismjs/components/prism-json";
import "prism-theme-night-owl";
// Use any from here: https://www.npmjs.com/package/prism-themes
// import "prism-themes/themes/prism-vs.css";
import "./../styles/style.scss";
import {
imageRenderer,
absoluteImageRenderer,
headingRenderer,
RootRenderer,
SmallRootRenderer,
} from "../utils/renderers";
import info from "../../bookInfo";
if (typeof window !== "undefined") {
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
const theme = urlParams.get("theme");
if (theme === "dark") {
document.getElementsByTagName("body")[0].classList += "dark";
}
}
function codeBlock({ value, language }) {
if (!value) return null;
const lang = language || "bash";
var html = Prism.highlight(value, Prism.languages[lang]);
var cls = `lang-${lang}`;
return (
<pre className={cls}>
<code dangerouslySetInnerHTML={{ __html: html }} className={cls} />
</pre>
);
}
const Index = ({ data, location }) => {
const siteTitle = data.site.siteMetadata.title;
const markdownBook = data.markdownRemark;
const renderers =
process.env.GATSBY_SCRAPPER === "1"
? {
heading: headingRenderer,
root: SmallRootRenderer,
image: absoluteImageRenderer,
}
: {
code: codeBlock,
heading: headingRenderer,
root: RootRenderer,
image: imageRenderer,
};
return (
<Layout location={location} title={siteTitle}>
<SEO title={info.title} />
<ReactMarkdown
renderers={renderers}
source={markdownBook.rawMarkdownBody}
/>
</Layout>
);
};
export default Index;
export const pageQuery = graphql`
query {
site {
siteMetadata {
title
}
}
markdownRemark {
rawMarkdownBody
}
}
`;