Skip to content

Commit 0d774cd

Browse files
authored
docs: inline website theme (golangci#1096)
Inline @Rocketseat theme for Gatsby because we need a lot of changes. Fix bad GitHub edit url, next/prev links, external links without target="_blank".
1 parent e560b3f commit 0d774cd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+2003
-72
lines changed

docs/gatsby-browser.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export { wrapRootElement } from "./src/@rocketseat/gatsby-theme-docs/gatsby/wrapRootElement";
2+
export { wrapPageElement } from "./src/@rocketseat/gatsby-theme-docs/gatsby/wrapPageElement";

docs/gatsby-config.js

+56-13
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,73 @@
1+
const withDefault = require(`./src/@rocketseat/gatsby-theme-docs-core/util/with-default`);
2+
13
const siteUrl = `https://golangci-lint.run`;
24

5+
const siteConfig = require(`./src/config/site.js`);
6+
const { basePath, configPath, docsPath } = withDefault(siteConfig);
7+
38
module.exports = {
49
siteMetadata: {
510
siteTitle: `golangci-lint`,
611
defaultTitle: ``,
712
siteTitleShort: `golangci-lint`,
813
siteDescription: `Fast Go linters runner golangci-lint.`,
9-
siteUrl: siteUrl,
14+
siteUrl,
1015
siteAuthor: `@golangci`,
1116
siteImage: `/logo.png`,
1217
siteLanguage: `en`,
1318
themeColor: `#7159c1`,
14-
basePath: `/`,
19+
basePath,
1520
footer: ${new Date().getFullYear()}`,
1621
},
1722
plugins: [
1823
`gatsby-alias-imports`,
24+
25+
`gatsby-transformer-sharp`,
26+
`gatsby-plugin-sharp`,
27+
{
28+
resolve: `gatsby-source-filesystem`,
29+
options: {
30+
name: `docs`,
31+
path: docsPath,
32+
},
33+
},
34+
{
35+
resolve: `gatsby-source-filesystem`,
36+
options: {
37+
name: `config`,
38+
path: configPath,
39+
},
40+
},
41+
{
42+
resolve: `gatsby-transformer-yaml`,
43+
options: {
44+
typeName: `SidebarItems`,
45+
},
46+
},
1947
{
20-
resolve: `@rocketseat/gatsby-theme-docs`,
48+
resolve: `gatsby-plugin-mdx`,
2149
options: {
22-
configPath: `src/config`,
23-
docsPath: `src/docs`,
24-
githubUrl: `https://github.com/golangci/golangci-lint`,
25-
baseDir: `docs`,
50+
extensions: [`.mdx`, `.md`],
51+
gatsbyRemarkPlugins: [
52+
`gatsby-remark-autolink-headers`,
53+
`gatsby-remark-external-links`,
54+
`gatsby-remark-embedder`,
55+
{
56+
resolve: `gatsby-remark-images`,
57+
options: {
58+
maxWidth: 960,
59+
withWebp: true,
60+
linkImagesToOriginal: false,
61+
},
62+
},
63+
`gatsby-remark-responsive-iframe`,
64+
`gatsby-remark-copy-linked-files`,
65+
],
66+
plugins: [
67+
`gatsby-remark-autolink-headers`,
68+
`gatsby-remark-external-links`,
69+
`gatsby-remark-images`,
70+
],
2671
},
2772
},
2873
{
@@ -59,13 +104,11 @@ module.exports = {
59104
},
60105
},
61106
},
62-
{
63-
resolve: `gatsby-transformer-remark`,
64-
options: {
65-
plugins: [`gatsby-remark-external-links`],
66-
},
67-
},
68107
`gatsby-plugin-netlify`,
69108
`gatsby-plugin-netlify-cache`,
109+
110+
`gatsby-plugin-catch-links`,
111+
`gatsby-plugin-emotion`,
112+
`gatsby-plugin-react-helmet`,
70113
],
71114
};

docs/gatsby-node.js

+252
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,252 @@
1+
const path = require(`path`);
2+
const { createFilePath } = require(`gatsby-source-filesystem`);
3+
const fs = require(`fs`);
4+
5+
const {
6+
normalizeBasePath,
7+
resolveLink,
8+
} = require(`./src/@rocketseat/gatsby-theme-docs-core/util/url`);
9+
const withDefault = require(`./src/@rocketseat/gatsby-theme-docs-core/util/with-default`);
10+
11+
exports.createPages = ({ graphql, actions: { createPage }, reporter }) => {
12+
reporter.success(`onCreateDocs`);
13+
14+
const siteConfig = require(`./src/config/site.js`);
15+
const { basePath, baseDir, docsPath, githubUrl } = withDefault(siteConfig);
16+
17+
const docsTemplate = require.resolve(
18+
`./src/@rocketseat/gatsby-theme-docs/src/templates/docs-query.js`
19+
);
20+
const homeTemplate = require.resolve(
21+
`./src/@rocketseat/gatsby-theme-docs/src/templates/homepage-query.js`
22+
);
23+
24+
return graphql(
25+
`
26+
{
27+
files: allFile(filter: { extension: { in: ["md", "mdx"] } }) {
28+
edges {
29+
node {
30+
id
31+
relativePath
32+
childMdx {
33+
frontmatter {
34+
next
35+
}
36+
fields {
37+
slug
38+
}
39+
}
40+
}
41+
}
42+
}
43+
sidebar: allSidebarItems {
44+
edges {
45+
node {
46+
label
47+
link
48+
items {
49+
label
50+
link
51+
}
52+
id
53+
}
54+
}
55+
}
56+
}
57+
`
58+
).then((result) => {
59+
if (result.errors) {
60+
reporter.panic(
61+
`docs: there was an error loading the docs folder!`,
62+
result.errors
63+
);
64+
return;
65+
}
66+
67+
createPage({
68+
path: basePath,
69+
component: homeTemplate,
70+
});
71+
72+
// Generate prev/next items based on sidebar.yml file
73+
const sidebar = result.data.sidebar.edges;
74+
const listOfItems = [];
75+
76+
sidebar.forEach(({ node: { label, link, items } }) => {
77+
if (Array.isArray(items)) {
78+
items.forEach((item) => {
79+
listOfItems.push({
80+
label: item.label,
81+
link: resolveLink(item.link, basePath),
82+
});
83+
});
84+
} else {
85+
listOfItems.push({
86+
label,
87+
link: resolveLink(link, basePath),
88+
});
89+
}
90+
});
91+
92+
// Generate docs pages
93+
const docs = result.data.files.edges;
94+
docs.forEach((doc) => {
95+
const {
96+
childMdx: {
97+
frontmatter: { next },
98+
fields: { slug },
99+
},
100+
relativePath,
101+
} = doc.node;
102+
103+
const githubEditUrl =
104+
githubUrl &&
105+
`${githubUrl}/tree/master/${baseDir}/${docsPath}/${relativePath}`;
106+
107+
const currentPageIndex = listOfItems.findIndex(
108+
(page) => page.link === slug
109+
);
110+
111+
const prevItem = listOfItems[currentPageIndex - 1];
112+
const nextItem = next
113+
? listOfItems.find((item) => item.link === next)
114+
: listOfItems[currentPageIndex + 1];
115+
116+
createPage({
117+
path: slug,
118+
component: docsTemplate,
119+
context: {
120+
slug,
121+
prev: prevItem,
122+
next: nextItem,
123+
githubEditUrl,
124+
},
125+
});
126+
});
127+
128+
reporter.success(`docs pages created`);
129+
});
130+
};
131+
132+
exports.createSchemaCustomization = ({ actions }) => {
133+
actions.createTypes(`
134+
type MdxFrontmatter @dontInfer {
135+
title: String!
136+
description: String
137+
image: String
138+
disableTableOfContents: Boolean
139+
next: String
140+
}
141+
`);
142+
143+
actions.createTypes(`
144+
type SidebarItems implements Node {
145+
label: String!
146+
link: String
147+
items: [SidebarItemsItems]
148+
}
149+
150+
type SidebarItemsItems {
151+
label: String
152+
link: String
153+
}
154+
`);
155+
};
156+
157+
exports.onPreBootstrap = ({ store, reporter }, themeOptions) => {
158+
const { configPath, docsPath } = withDefault(themeOptions);
159+
const { program } = store.getState();
160+
161+
const dirs = [
162+
path.join(program.directory, configPath),
163+
path.join(program.directory, docsPath),
164+
];
165+
166+
dirs.forEach((dir) => {
167+
if (!fs.existsSync(dir)) {
168+
reporter.success(`docs: intialized the ${dir} directory`);
169+
fs.mkdirSync(dir);
170+
}
171+
});
172+
};
173+
174+
exports.onCreateNode = (
175+
{ node, actions: { createNodeField }, getNode },
176+
themeOptions
177+
) => {
178+
if (node.internal.type !== `Mdx`) {
179+
return;
180+
}
181+
182+
const { basePath } = withDefault(themeOptions);
183+
184+
let value = createFilePath({ node, getNode });
185+
if (value === "index") value = "";
186+
187+
createNodeField({
188+
name: `slug`,
189+
node,
190+
value: normalizeBasePath(basePath, value),
191+
});
192+
193+
createNodeField({
194+
name: `id`,
195+
node,
196+
value: node.id,
197+
});
198+
};
199+
200+
/**
201+
[
202+
{
203+
"node": {
204+
"label": "Home",
205+
"link": "/",
206+
"items": null,
207+
"id": "a2913be3-af3c-5fc9-967e-a058e86b20a9"
208+
}
209+
},
210+
{
211+
"node": {
212+
"label": "With dropdown",
213+
"link": null,
214+
"items": [
215+
{ "label": "My Example", "link": "/my-example" },
216+
{ "label": "Teste 2", "link": "/teste-2" }
217+
],
218+
"id": "c7d9606c-4bda-5097-a0df-53108e9f4efd"
219+
}
220+
}
221+
]
222+
*/
223+
224+
// Ler todo o array e salvar em uma objeto chave/valor
225+
/**
226+
* {
227+
* '/': {
228+
* prev: null,
229+
* next: {
230+
* label: 'My example',
231+
* link: '/my-example'
232+
* }
233+
* },
234+
* '/my-example': {
235+
* prev: {
236+
* label: 'Home',
237+
* link: '/'
238+
* },
239+
* next: {
240+
* label: 'Teste 2',
241+
* link: '/teste-2'
242+
* }
243+
* },
244+
* '/teste-2': {
245+
* prev: {
246+
* label: 'My example',
247+
* link: '/my-example'
248+
* },
249+
* next: null
250+
* }
251+
* }
252+
*/

docs/gatsby-ssr.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export { wrapRootElement } from "./src/@rocketseat/gatsby-theme-docs/gatsby/wrapRootElement";
2+
export { wrapPageElement } from "./src/@rocketseat/gatsby-theme-docs/gatsby/wrapPageElement";

0 commit comments

Comments
 (0)