Skip to content

Commit b907bdf

Browse files
authored
Turn remaining files into TypeScript source (#758)
1 parent c373f8b commit b907bdf

File tree

12 files changed

+56
-42
lines changed

12 files changed

+56
-42
lines changed

components/content.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Menu from "../components/menu";
1+
import Menu from "./menu";
22
import { DiscordIcon, GitHubIcon } from "./icons";
33
import React, { useEffect, useMemo, useRef, useState } from "react";
44
import Link from "next/link";

components/menu.jsx renamed to components/menu.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { Fragment, useCallback, useState } from "react";
22
import classnames from "classnames";
3-
import Link from "next/link";
43

54
const monthNames = [
65
"January",
@@ -130,7 +129,8 @@ function Level2({ href, menu }) {
130129
return <ul>{items}</ul>;
131130
}
132131

133-
function pagesFor(menu) {
132+
// TODO: Type the menu hierarchy
133+
function pagesFor(menu: Record<PropertyKey, any>) {
134134
return Object.entries(menu).map(([, entry]) => {
135135
if (entry.page) {
136136
return {

lib/api.js renamed to lib/api.ts

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ import fs from "fs";
22
import glob from "glob";
33
import path from "path";
44
import matter from "gray-matter";
5-
import util from "util";
65

7-
import { toHTML } from "./markdown.js";
6+
import { toHTML } from "./markdown";
87

98
const contentDir = path.join(process.cwd(), "content").replace(/\\/g, "/");
109

1110
// Merge app level props in with page props
12-
export function withAppProps(props = { props: {} }) {
11+
export function withAppProps(
12+
props: { props: Record<PropertyKey, unknown> } = { props: {} }
13+
) {
1314
const blog = getLastBlog();
1415
delete blog.body;
1516
props.props.app = {
@@ -64,7 +65,6 @@ export async function getProps(menu, slug) {
6465
const page = loadPage(`${slug}`);
6566
page.body = await toHTML(page.body);
6667

67-
const sectionTitle = menu[root].title;
6868
const normalized = [
6969
{
7070
key: root,
@@ -86,31 +86,39 @@ export async function getProps(menu, slug) {
8686
function setPrevNext(page, menu) {
8787
let didMatch = false;
8888

89-
eachPage(menu, (p, prev) => {
90-
if (p.href == page.href) {
91-
didMatch = true;
92-
93-
if (prev && prev.title) {
94-
page.prev = {
95-
title: prev.menuTitle || prev.title,
96-
href: prev.href,
89+
eachPage(
90+
menu,
91+
(p, prev) => {
92+
if (p.href == page.href) {
93+
didMatch = true;
94+
95+
if (prev && prev.title) {
96+
page.prev = {
97+
title: prev.menuTitle || prev.title,
98+
href: prev.href,
99+
};
100+
}
101+
} else if (didMatch) {
102+
page.next = {
103+
title: p.menuTitle || p.title,
104+
href: p.href,
97105
};
98-
}
99-
} else if (didMatch) {
100-
page.next = {
101-
title: p.menuTitle || p.title,
102-
href: p.href,
103-
};
104106

105-
didMatch = false;
106-
}
107-
});
107+
didMatch = false;
108+
}
109+
},
110+
undefined,
111+
undefined
112+
);
108113

109114
return page;
110115
}
111116

112117
// Build a list of paths from the sitemap
113-
function collectPaths(level, prefix = "") {
118+
function collectPaths(
119+
level: Record<string, { nested?: string[]; href?: string }>,
120+
prefix = ""
121+
) {
114122
let out = [];
115123

116124
for (const [k, v] of Object.entries(level)) {

lib/blog.js renamed to lib/blog.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ function cachedPaths() {
99
return paths;
1010
}
1111

12-
export function getBlogPostsByYear({ limit } = {}) {
12+
export function getBlogPostsByYear(limit?: number) {
1313
let count = 0;
1414
return cachedPaths().reduce((years, post) => {
15-
if (limit != null && count >= limit) {
15+
if (limit !== undefined && count >= limit) {
1616
return years;
1717
}
1818
const date = new Date(post.date);

lib/markdown.js renamed to lib/markdown.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { stream } from "unified-stream";
21
import { unified } from "unified";
32
import remarkParse from "remark-parse";
43
import remarkRehype from "remark-rehype";
@@ -80,11 +79,13 @@ export const toHTML = async (raw) => {
8079
await unified()
8180
.use(remarkParse)
8281
.use(remarkCodeRemoveSomeLines)
82+
// @ts-expect-error: unified's plugin type mistakenly selects the wrong union overload
8383
.use(remarkRehype, { allowDangerousHtml: true })
8484
.use(rehypeHighlight, rehypeHighlightOptions)
8585
.use(rehypeRaw)
8686
.use(rehypeSlug)
8787
.use(rehyperBlockquotePlus, rehyperBlockquotePlusOptions)
88+
// @ts-expect-error: unified's plugin type mistakenly selects the Array<void> union variant
8889
.use(rehypeStringify)
8990
.process(raw)
9091
);
File renamed without changes.

package-lock.json

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"scripts": {
77
"dev": "next dev",
88
"build": "next build && npm run build:rss && next export",
9-
"build:rss": "node ./scripts/generate-rss-feed.js",
9+
"build:rss": "tsc --outDir dist --noEmit false && sed -i 's/markdown\"/markdown.js\"/g' dist/lib/api.js && node scripts/generate-rss-feed.js",
1010
"start": "next start",
1111
"fmt": "prettier --write --prose-wrap always '{components,content,pages,lib,styles}/**/*.{js,jsx,ts,tsx,scss}'",
1212
"fmt:check": "prettier --check --prose-wrap always '{components,content,pages,lib,styles}/**/*.{js,jsx,ts,tsx,scss}'"
File renamed without changes.

pages/blog.jsx renamed to pages/blog.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@ import Layout from "../components/layout";
33
import * as blog from "../lib/blog";
44
import * as api from "../lib/api";
55

6-
export default function Blog({ app, postsByYear }) {
6+
type Props = {
7+
app: any;
8+
// TODO: Properly type the posts
9+
postsByYear: Record<number, any>;
10+
};
11+
12+
export default function Blog({ app, postsByYear }: Props) {
713
return (
814
<Layout title={"Blog Posts"} blog={app.blog}>
915
<div className="is-marginless tk-docs">
@@ -13,7 +19,7 @@ export default function Blog({ app, postsByYear }) {
1319
<h1 className="title">Blog Posts</h1>
1420
{Object.entries(postsByYear)
1521
.reverse()
16-
.map(([year, { key, title, nested }]) => (
22+
.map(([year, { title, nested }]) => (
1723
<YearPosts year={year} posts={nested} key={title} />
1824
))}
1925
</section>

0 commit comments

Comments
 (0)