Skip to content

Commit 00d6eaf

Browse files
committed
adds wiki pages list, fix creation of root wiki page
1 parent 2e938d9 commit 00d6eaf

3 files changed

Lines changed: 66 additions & 23 deletions

File tree

api/src/controllers/wiki.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const slugRegex = /^\/([a-z0-9-_\/])*/i;
55

66
const formatSlug = (slug) => {
77
if (!slug.startsWith("/")) slug = `/${slug}`;
8-
if (slug.endsWith("/")) slug = slug.slice(0, -1);
8+
if (slug.endsWith("/") && slug !== "/") slug = slug.slice(0, -1);
99
const split = slug.split("/");
1010
const slugified = split.map((token) => slugify(token, { lower: true }));
1111
return slugified.join("/");
@@ -98,7 +98,9 @@ export const getWiki = async (req, res, next) => {
9898
return;
9999
}
100100

101-
res.json(page);
101+
const allPages = await Wiki.find({}, { slug: 1, title: 1 }).lean();
102+
103+
res.json({ page, allPages });
102104
} catch (e) {
103105
next(e);
104106
}

client/pages/wiki/[[...slug]].js

Lines changed: 61 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,13 @@ import LoadingContext from "../../utils/LoadingContext";
1717
import Modal from "../../components/Modal";
1818
import { WikiFields } from "./new";
1919

20-
const Wiki = ({ page, token, userRole, slug }) => {
20+
const sortSlug = (a, b) => {
21+
if (a.slug > b.slug) return 1;
22+
if (a.slug < b.slug) return -1;
23+
return 0;
24+
};
25+
26+
const Wiki = ({ page, allPages, token, userRole, slug }) => {
2127
const [editing, setEditing] = useState(false);
2228
const [showDeleteModal, setShowDeleteModal] = useState(false);
2329

@@ -161,24 +167,57 @@ const Wiki = ({ page, token, userRole, slug }) => {
161167
</Text>
162168
</Box>
163169
{!editing ? (
164-
<MarkdownBody>
165-
<ReactMarkdown
166-
remarkPlugins={[remarkGfm]}
167-
components={{
168-
a({ href, ...props }) {
169-
return href.startsWith("http") ? (
170-
<a href={href} target="_blank" {...props} />
171-
) : (
172-
<Link href={href} passHref>
173-
<a {...props} />
174-
</Link>
175-
);
176-
},
177-
}}
170+
<Box
171+
display="grid"
172+
gridTemplateColumns={["1fr", "200px auto"]}
173+
gridGap={5}
174+
>
175+
<Box
176+
bg="sidebar"
177+
border="1px solid"
178+
borderColor="border"
179+
borderRadius={1}
180+
p={3}
178181
>
179-
{page.body}
180-
</ReactMarkdown>
181-
</MarkdownBody>
182+
<Text
183+
fontWeight={600}
184+
fontSize={1}
185+
mb={3}
186+
_css={{ textTransform: "uppercase" }}
187+
>
188+
Pages
189+
</Text>
190+
{allPages.sort(sortSlug).map((p) => (
191+
<Link
192+
key={`page-${p.slug}`}
193+
href={`/wiki/${p.slug}`}
194+
passHref
195+
>
196+
<Text as="a" display="block">
197+
{p.title}
198+
</Text>
199+
</Link>
200+
))}
201+
</Box>
202+
<MarkdownBody>
203+
<ReactMarkdown
204+
remarkPlugins={[remarkGfm]}
205+
components={{
206+
a({ href, ...props }) {
207+
return href.startsWith("http") ? (
208+
<a href={href} target="_blank" {...props} />
209+
) : (
210+
<Link href={href} passHref>
211+
<a {...props} />
212+
</Link>
213+
);
214+
},
215+
}}
216+
>
217+
{page.body}
218+
</ReactMarkdown>
219+
</MarkdownBody>
220+
</Box>
182221
) : (
183222
<form onSubmit={handleEdit}>
184223
<WikiFields values={page} />
@@ -246,8 +285,10 @@ export const getServerSideProps = withAuthServerSideProps(
246285
) {
247286
throw "banned";
248287
}
249-
const page = await wikiRes.json();
250-
return { props: { page, token, userRole: role, slug: parsedSlug } };
288+
const { page, allPages } = await wikiRes.json();
289+
return {
290+
props: { page, allPages, token, userRole: role, slug: parsedSlug },
291+
};
251292
} catch (e) {
252293
if (e === "banned") throw "banned";
253294
return { props: { token, userRole: role, slug: parsedSlug } };

client/pages/wiki/new.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export const WikiFields = ({ values }) => {
2929
onBlur={(e) => {
3030
let { value } = e.target;
3131
if (!value.startsWith("/")) value = `/${value}`;
32-
if (value.endsWith("/")) value = value.slice(0, -1);
32+
if (value.endsWith("/") && value !== "/") value = value.slice(0, -1);
3333
const split = value.split("/");
3434
const slugified = split.map((token) =>
3535
slugify(token, { lower: true })

0 commit comments

Comments
 (0)