Skip to content

Commit 6956c2e

Browse files
authored
add config for trailing slash (hasura#54)
1 parent 0a907ef commit 6956c2e

File tree

8 files changed

+50
-27
lines changed

8 files changed

+50
-27
lines changed

config.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ const config = {
22
"gatsby": {
33
"pathPrefix": "/",
44
"siteUrl": "https://learn.hasura.io",
5-
"gaTrackingId": null
5+
"gaTrackingId": null,
6+
"trailingSlash": false
67
},
78
"header": {
89
"logo": "https://graphql-engine-cdn.hasura.io/learn-hasura/assets/homepage/favicon.png",
@@ -24,11 +25,11 @@ const config = {
2425
},
2526
"sidebar": {
2627
"forcedNavOrder": [
27-
"/introduction",
28+
"/introduction", // add trailing slash if enabled above
2829
"/codeblock"
2930
],
3031
"collapsedNav": [
31-
"/codeblock"
32+
"/codeblock" // add trailing slash if enabled above
3233
],
3334
"links": [
3435
{ "text": "Hasura", "link": "https://hasura.io"},

gatsby-config.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ const plugins = [
1111
}
1212
},
1313
'gatsby-plugin-emotion',
14-
'gatsby-plugin-remove-trailing-slashes',
1514
'gatsby-plugin-react-helmet',
1615
{
1716
resolve: "gatsby-source-filesystem",
@@ -77,6 +76,12 @@ if (config.pwa && config.pwa.enabled && config.pwa.manifest) {
7776
} else {
7877
plugins.push('gatsby-plugin-remove-serviceworker');
7978
}
79+
80+
// check and remove trailing slash
81+
if (config.gatsby && !config.gatsby.trailingSlash) {
82+
plugins.push('gatsby-plugin-remove-trailing-slashes');
83+
}
84+
8085
module.exports = {
8186
pathPrefix: config.gatsby.pathPrefix,
8287
siteMetadata: {

gatsby-node.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const componentWithMDXScope = require("gatsby-plugin-mdx/component-with-mdx-scope");
22
const path = require("path");
33
const startCase = require("lodash.startcase");
4+
const config = require("./config");
45

56
exports.createPages = ({ graphql, actions }) => {
67
const { createPage } = actions;
@@ -74,11 +75,19 @@ exports.onCreateNode = ({ node, getNode, actions }) => {
7475
value = "";
7576
}
7677

77-
createNodeField({
78-
name: `slug`,
79-
node,
80-
value: `/${value}`
81-
});
78+
if(config.gatsby && config.gatsby.trailingSlash) {
79+
createNodeField({
80+
name: `slug`,
81+
node,
82+
value: value === "" ? `/` : `/${value}/`
83+
});
84+
} else {
85+
createNodeField({
86+
name: `slug`,
87+
node,
88+
value: `/${value}`
89+
});
90+
}
8291

8392
createNodeField({
8493
name: "id",

src/components/rightSidebar.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import Link from "./link";
55
import './styles.css';
66
import config from '../../config';
77

8-
const forcedNavOrder = config.sidebar.forcedNavOrder;
9-
108
const Sidebar = styled('aside')`
119
width: 100%;
1210
background-color: #fff;

src/components/sidebar/index.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import {ExternalLink} from "react-feather";
66
import '../styles.css';
77
import config from '../../../config';
88

9-
const forcedNavOrder = config.sidebar.forcedNavOrder;
10-
119
// eslint-disable-next-line no-unused-vars
1210
const ListItem = styled(({ className, active, level, ...props }) => {
1311
return (

src/components/sidebar/tree.js

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ const calculateTreeData = edges => {
77
const tree = originalData.reduce((accu, {node: {fields: {slug, title}}}) => {
88
const parts = slug.split('/');
99
let {items: prevItems} = accu;
10-
for (const part of parts.slice(1, -1)) {
11-
let tmp = prevItems.find(({label}) => label == part);
10+
const slicedParts = config.gatsby && config.gatsby.trailingSlash ? parts.slice(1, -2) : parts.slice(1, -1);
11+
for (const part of slicedParts) {
12+
let tmp = prevItems && prevItems.find(({label}) => label == part);
1213
if (tmp) {
1314
if (!tmp.items) {
1415
tmp.items = [];
@@ -19,13 +20,14 @@ const calculateTreeData = edges => {
1920
}
2021
prevItems = tmp.items;
2122
}
22-
const existingItem = prevItems.find(({label}) => label === parts[parts.length - 1]);
23+
const slicedLength = config.gatsby && config.gatsby.trailingSlash ? parts.length - 2 : parts.length - 1;
24+
const existingItem = prevItems.find(({label}) => label === parts[slicedLength]);
2325
if (existingItem) {
2426
existingItem.url = slug;
2527
existingItem.title = title;
2628
} else {
2729
prevItems.push({
28-
label: parts[parts.length - 1],
30+
label: parts[slicedLength],
2931
url: slug,
3032
items: [],
3133
title
@@ -35,12 +37,15 @@ const calculateTreeData = edges => {
3537
}, {items: []});
3638
const {sidebar: {forcedNavOrder = []}} = config;
3739
const tmp = [...forcedNavOrder];
40+
if(config.gatsby && config.gatsby.trailingSlash) {
41+
}
3842
tmp.reverse();
3943
return tmp.reduce((accu, slug) => {
4044
const parts = slug.split('/');
4145
let {items: prevItems} = accu;
42-
for (const part of parts.slice(1, -1)) {
43-
let tmp = prevItems.find(({label}) => label == part);
46+
const slicedParts = config.gatsby && config.gatsby.trailingSlash ? parts.slice(1, -2) : parts.slice(1, -1);
47+
for (const part of slicedParts) {
48+
let tmp = prevItems.find((item) => item && item.label == part);
4449
if (tmp) {
4550
if (!tmp.items) {
4651
tmp.items = [];
@@ -49,7 +54,9 @@ const calculateTreeData = edges => {
4954
tmp = {label: part, items: []};
5055
prevItems.push(tmp)
5156
}
52-
prevItems = tmp.items;
57+
if(tmp && tmp.items) {
58+
prevItems = tmp.items;
59+
}
5360
}
5461
// sort items alphabetically.
5562
prevItems.map((item) => {
@@ -62,15 +69,17 @@ const calculateTreeData = edges => {
6269
return 0;
6370
});
6471
})
65-
const index = prevItems.findIndex(({label}) => label === parts[parts.length - 1]);
66-
accu.items.unshift(prevItems.splice(index, 1)[0]);
72+
const slicedLength = config.gatsby && config.gatsby.trailingSlash ? (parts.length - 2) : (parts.length - 1);
73+
const index = prevItems.findIndex(({label}) => label === parts[slicedLength]);
74+
if(prevItems.length) {
75+
accu.items.unshift(prevItems.splice(index, 1)[0]);
76+
}
6777
return accu;
6878
}, tree);
6979
}
7080

71-
7281
const Tree = ({edges}) => {
73-
const [treeData] = useState(() => {
82+
let [treeData] = useState(() => {
7483
return calculateTreeData(edges);
7584
});
7685
const defaultCollapsed = {};

src/components/sidebar/treeNode.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ const TreeNode = ({className = '', setCollapsed, collapsed, url, title, items, .
3939

4040
{!isCollapsed && hasChildren ? (
4141
<ul>
42-
{items.map((item) => (
42+
{items.map((item, index) => (
4343
<TreeNode
44-
key={item.url}
44+
key={item.url + index.toString()}
4545
setCollapsed={setCollapsed}
4646
collapsed={collapsed}
4747
{...item}

src/templates/docs.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@ export default class MDXRuntimeTest extends Component {
6161
return { ...acc, [cur]: [cur] };
6262
}
6363

64-
const prefix = cur.split("/")[1];
64+
let prefix = cur.split("/")[1];
65+
if(config.gatsby && config.gatsby.trailingSlash) {
66+
prefix = prefix + '/';
67+
}
6568

6669
if (prefix && forcedNavOrder.find(url => url === `/${prefix}`)) {
6770
return { ...acc, [`/${prefix}`]: [...acc[`/${prefix}`], cur] };

0 commit comments

Comments
 (0)