Skip to content

Commit 93aa29b

Browse files
authored
Merge pull request #2 from codewars/reference-languages
2 parents 9aa5d8b + eac80d1 commit 93aa29b

File tree

12 files changed

+166
-99
lines changed

12 files changed

+166
-99
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
category: guides
3+
tags:
4+
- testing
5+
languages:
6+
- javascript
7+
---
8+
9+
# Testing JavaScript

content/refs/javascript-ref.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
category: references
3+
languages: [javascript]
4+
---
5+
6+
# Sample JavaScript Reference
7+
8+
> TODO Remove this file
9+
10+
This file exists to show how the sidebar on language pages are automatically populated and grouped by `category`.

data/categories.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,9 @@
22
- id: docs
33
name: Docs
44
description: Docs
5+
6+
- id: guides
7+
name: Guides
8+
- id: references
9+
name: References
10+
description: Reference Manuals

data/tags.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,6 @@
99
name: Content
1010
- id: codewars
1111
name: Codewars
12+
13+
- id: testing
14+
name: Testing

gridsome.config.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ module.exports = {
6161
typeName: "Tag",
6262
},
6363
// Can reference multiple languages to be listed on the language page.
64-
// languages: {
65-
// typeName: "Language",
66-
// },
64+
languages: {
65+
typeName: "Language",
66+
},
6767
},
6868
},
6969
},

src/assets/styles.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ pre[class*="language-"] {
149149
}
150150

151151
& > code[class*="language-"] {
152-
@apply border-none leading-relaxed;
152+
@apply border-none leading-relaxed px-0;
153153
}
154154
}
155155

src/components/NextPrevLinks.vue

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -22,51 +22,24 @@
2222
</div>
2323
</template>
2424

25-
<static-query>
26-
query {
27-
allMarkdownPage {
28-
edges {
29-
node {
30-
path
31-
title
32-
}
33-
}
34-
}
35-
}
36-
</static-query>
37-
3825
<script>
3926
import { ArrowLeftIcon, ArrowRightIcon } from "vue-feather-icons";
4027
4128
export default {
4229
props: {
43-
prevPath: {
44-
type: String,
30+
prev: {
31+
// {path: string; title: string}
32+
type: Object,
4533
},
46-
nextPath: {
47-
type: String,
34+
next: {
35+
// {path: string; title: string}
36+
type: Object,
4837
},
4938
},
5039
5140
components: {
5241
ArrowLeftIcon,
5342
ArrowRightIcon,
5443
},
55-
56-
computed: {
57-
pages() {
58-
return this.$static.allMarkdownPage.edges.map((edge) => edge.node);
59-
},
60-
next() {
61-
if (!this.nextPath) return false;
62-
63-
return this.pages.find((page) => page.path === this.nextPath);
64-
},
65-
prev() {
66-
if (!this.prevPath) return false;
67-
68-
return this.pages.find((page) => page.path === this.prevPath);
69-
},
70-
},
7144
};
7245
</script>

src/components/Sidebar.vue

Lines changed: 6 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<template>
22
<div
33
ref="sidebar"
4-
v-if="showSidebar"
4+
v-if="sidebarSections"
55
class="px-4 pt-8 lg:pt-12 divide-y divide-ui-border"
66
>
7-
<div v-for="section of sidebar.sections" :key="section.title" class="py-4">
7+
<div v-for="section of sidebarSections" :key="section.title" class="py-4">
88
<h3
99
class="pt-0 mt-0 mb-1 font-black text-sm leading-snug tracking-tight uppercase border-none text-ui-typo"
1010
>
@@ -13,7 +13,7 @@
1313

1414
<ul class="max-w-full pl-2 mb-0">
1515
<li
16-
v-for="page in findPages(section.items)"
16+
v-for="page in section.items"
1717
:id="page.path"
1818
:key="page.path"
1919
:class="getClassesForAnchor(page.path)"
@@ -34,60 +34,16 @@
3434
</div>
3535
</template>
3636

37-
<static-query>
38-
query Sidebar {
39-
metadata {
40-
settings {
41-
sidebar {
42-
name
43-
sections {
44-
title
45-
items
46-
}
47-
}
48-
}
49-
}
50-
51-
allMarkdownPage {
52-
edges {
53-
node {
54-
path
55-
title
56-
}
57-
}
58-
}
59-
}
60-
</static-query>
61-
6237
<script>
6338
export default {
6439
props: {
6540
currentPath: {
6641
type: String,
6742
required: true,
6843
},
69-
// Name of the sidebar.
70-
name: {
71-
type: String,
72-
},
73-
},
74-
data() {
75-
return {
76-
expanded: [],
77-
};
78-
},
79-
computed: {
80-
pages() {
81-
return this.$static.allMarkdownPage.edges.map((edge) => edge.node);
82-
},
83-
sidebar() {
84-
const name = this.name;
85-
return this.$static.metadata.settings.sidebar.find(
86-
(sidebar) => sidebar.name === name
87-
);
88-
},
89-
showSidebar() {
90-
return this.name && this.sidebar;
44+
sidebarSections: {
45+
// Array of sections
46+
type: Array,
9147
},
9248
},
9349
methods: {
@@ -98,9 +54,6 @@ export default {
9854
"transition transform hover:translate-x-1 hover:text-ui-primary": !current,
9955
};
10056
},
101-
findPages(links) {
102-
return links.map((link) => this.pages.find((page) => page.path === link));
103-
},
10457
},
10558
};
10659
</script>

src/layouts/Default.vue

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<div class="w-full pb-16 bg-ui-background">
2222
<Sidebar
2323
@navigate="sidebarOpen = false"
24-
:name="sidebarName"
24+
:sidebarSections="sidebarSections"
2525
:currentPath="currentPath"
2626
/>
2727
</div>
@@ -52,6 +52,24 @@
5252
query {
5353
metadata {
5454
siteName
55+
settings {
56+
sidebar {
57+
name
58+
sections {
59+
title
60+
items
61+
}
62+
}
63+
}
64+
}
65+
66+
allMarkdownPage {
67+
edges {
68+
node {
69+
path
70+
title
71+
}
72+
}
5573
}
5674
}
5775
</static-query>
@@ -66,8 +84,9 @@ export default {
6684
currentPath: {
6785
type: String,
6886
},
69-
sidebarName: {
70-
type: String,
87+
sidebar: {
88+
// Name of the sidebar defined in settings or array of sections.
89+
type: [String, Array],
7190
},
7291
},
7392
components: {
@@ -95,6 +114,9 @@ export default {
95114
},
96115
},
97116
computed: {
117+
pages() {
118+
return this.$static.allMarkdownPage.edges.map((edge) => edge.node);
119+
},
98120
sidebarStyle() {
99121
return {
100122
top: this.headerHeight + "px",
@@ -104,10 +126,27 @@ export default {
104126
hasSidebar() {
105127
return (
106128
this.$page &&
107-
(this.$page.markdownPage || this.sidebarName) &&
129+
(this.$page.markdownPage || this.sidebar) &&
108130
this.headerHeight > 0
109131
);
110132
},
133+
sidebarSections() {
134+
if (Array.isArray(this.sidebar)) return this.sidebar;
135+
136+
const def = this.$static.metadata.settings.sidebar.find(
137+
(sidebar) => sidebar.name === this.sidebar
138+
);
139+
if (!def) return null;
140+
141+
return def.sections.map((section) => {
142+
return {
143+
title: section.title,
144+
items: section.items.map((link) =>
145+
this.pages.find((page) => page.path === link)
146+
),
147+
};
148+
});
149+
},
111150
},
112151
mounted() {
113152
this.setHeaderHeight();

src/pages/Languages.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<template>
2-
<Layout currentPath="/languages/" sidebarName="docs">
2+
<Layout currentPath="/languages/" sidebar="docs">
33
<div class="w-full md:w-2/3">
44
<div class="content">
55
<h1 id="supported-languages">

src/templates/Language.vue

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<template>
2-
<Layout :currentPath="language.path" sidebarName="docs">
2+
<Layout :currentPath="language.path" :sidebar="sidebarSections">
33
<h1
44
class="mt-4 text-4xl font-black text-ui-typo leading-snug tracking-tighter text-center"
55
>
@@ -83,7 +83,9 @@
8383
</template>
8484
</div>
8585

86-
<!-- TODO List documents related to this language. Let MarkdownPage refernce by `language` frontmatter -->
86+
<div class="mt-8 pt-8 lg:mt-12 lg:pt-12 border-t border-ui-border">
87+
<NextPrevLinks :prev="{ title: 'Languages', path: '/languages/' }" />
88+
</div>
8789
</Layout>
8890
</template>
8991

@@ -111,6 +113,29 @@ query($id: ID!) {
111113
url
112114
}
113115
}
116+
117+
belongsTo(sortBy: "title", order: ASC) {
118+
totalCount
119+
120+
edges {
121+
node {
122+
... on MarkdownPage {
123+
title
124+
excerpt
125+
path
126+
category {
127+
id
128+
name
129+
}
130+
tags {
131+
id
132+
name
133+
path
134+
}
135+
}
136+
}
137+
}
138+
}
114139
}
115140

116141
allLanguage {
@@ -124,7 +149,12 @@ query($id: ID!) {
124149
</page-query>
125150

126151
<script>
152+
import NextPrevLinks from "@/components/NextPrevLinks";
153+
127154
export default {
155+
components: {
156+
NextPrevLinks,
157+
},
128158
computed: {
129159
language() {
130160
return this.$page.language;
@@ -138,6 +168,26 @@ export default {
138168
anyPackages() {
139169
return this.versionsWithPackages.length > 0;
140170
},
171+
pages() {
172+
return this.language.belongsTo.edges.map((e) => e.node);
173+
},
174+
// Sidebar of language page lists all pages referencing the language grouped by category
175+
sidebarSections() {
176+
const pages = this.pages;
177+
if (pages.length === 0) return "docs";
178+
179+
const groups = {};
180+
for (const p of pages) {
181+
const key = p.category.name;
182+
if (!groups[key]) groups[key] = [];
183+
groups[key].push(p);
184+
}
185+
const sections = [];
186+
for (const name of Object.keys(groups)) {
187+
sections.push({ title: name, items: groups[name] });
188+
}
189+
return sections;
190+
},
141191
},
142192
143193
methods: {

0 commit comments

Comments
 (0)