FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Fallout typescript · mcjava-wiki/mcjava-wiki@07aa5b0 · GitHub

Fallout typescript · mcjava-wiki/mcjava-wiki@07aa5b0 · GitHub
Skip to content

Navigation Menu

Commit 07aa5b0

Browse files
committed
Fallout typescript
Signed-off-by: Joshua Castle <26531652+Kas-tle@users.noreply.github.com>
1 parent 52403eb commit 07aa5b0

24 files changed

Lines changed: 269 additions & 117 deletions

‎.tsconfig‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es2016",
4+
"jsx": "react",
5+
"module": "commonjs",
6+
"allowJs": true,
7+
"outDir": "./dist",
8+
"esModuleInterop": true,
9+
"forceConsistentCasingInFileNames": true,
10+
"strict": true,
11+
"skipLibCheck": true
12+
},
13+
"exclude": ["node_modules", "public", ".cache"]
14+
}

‎gatsby-browser.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import React from 'react'
22
import { RootWrapper } from './src/components/RootWrapper'
33
import { PageWrapper } from './src/components/PageWrapper'
4-
import Prism from 'prism-react-renderer/prism'
4+
const Prism = require('prism-react-renderer/prism') as typeof import('prismjs');
55

6-
(typeof global !== 'undefined' ? global : window).Prism = Prism
6+
window.Prism = Prism
77

88
require('./src/components/highlights')
99

‎gatsby-config.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ const visit = require('unist-util-visit')
44

55
/** @type {import('unified').Plugin<Array<void>, import('hast').Root>} */
66
function rehypeMetaAsAttributes() {
7-
return (tree) => {
8-
visit(tree, 'element', (node) => {
7+
return (tree: any) => {
8+
visit(tree, 'element', (node: { tagName: string; data: { meta: any }; properties: { meta: any } }) => {
99
if (node.tagName === 'code' && node.data && node.data.meta) {
1010
node.properties.meta = node.data.meta
1111
}

‎gatsby-node.ts‎

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
const fs = require('fs')
2-
const path = require('path')
3-
const { createFilePath } = require('gatsby-source-filesystem')
4-
const { getSiteUrl } = require('./src/theme-options')
5-
const fetch = require('node-fetch');
6-
const cheerio = require('cheerio');
1+
import { CreateNodeArgs } from "gatsby";
2+
3+
import fs from 'fs';
4+
import path from 'path';
5+
import { createFilePath } from 'gatsby-source-filesystem';
6+
const { getSiteUrl } = require('./src/theme-options.ts')
7+
import cheerio from 'cheerio';
78

89
function createSchemaCustomization({ actions }) {
910
const { createTypes } = actions
@@ -74,7 +75,7 @@ async function onCreateMdxNode({ node, getNode, actions }, options) {
7475
const url = new URL(getSiteUrl(options))
7576
url.pathname = slug
7677

77-
function getGithubLink(tld, route) {
78+
function getGithubLink(tld: string, route: string) {
7879
const {
7980
baseDirectory = path.resolve(__dirname, './'),
8081
githubRepositoryURL = `https://github.${tld}/mcjava-wiki/mcjava-wiki`,
@@ -89,22 +90,35 @@ async function onCreateMdxNode({ node, getNode, actions }, options) {
8990
return `${repositoryURL}${route}${githubDefaultBranch}${relativePath}`
9091
}
9192

92-
const getData = async (url) => {
93+
const getData = async (url: URL | RequestInfo) => {
9394
const response = await fetch(url);
9495
return response.text();
9596
};
9697

98+
interface Contributor {
99+
name: string;
100+
link: string;
101+
}
102+
97103
const getContributors = async () => {
98104
const contributorsHtml = await getData(getGithubLink('com', '/contributors-list/'));
99-
const $ = cheerio.load(await contributorsHtml);
100-
const contributors = [];
101-
$(".avatar").each((i, element) => {
102-
const contributor = {};
103-
const name = $(element).parent().attr('href').slice(1);
104-
contributor.name = name ? name.trim() : '';
105-
const link = $(element).attr('src');
106-
contributor.link = link ? link : '';
107-
contributors.push(contributor);
105+
const $ = cheerio.load(contributorsHtml);
106+
107+
const contributors: Contributor[] = [];
108+
$(".avatar").each((_i: any, element: any) => {
109+
const href = $(element).parent().attr('href');
110+
if (href) {
111+
const name = href.slice(1);
112+
const link = $(element).attr('src');
113+
const contributor: Contributor = {
114+
name: name ? name.trim() : '',
115+
link: link ? link : ''
116+
};
117+
118+
contributor.name = name ? name.trim() : '';
119+
contributor.link = link ? link : '';
120+
contributors.push(contributor);
121+
}
108122
});
109123

110124
return contributors;
@@ -183,9 +197,9 @@ async function onCreateMdxNode({ node, getNode, actions }, options) {
183197

184198
}
185199

186-
async function onCreateNode(...args) {
200+
async function onCreateNode(...args: [CreateNodeArgs]) {
187201
if (args[0].node.internal.type === 'Mdx') {
188-
await onCreateMdxNode(...args)
202+
await onCreateMdxNode(...args, {})
189203
}
190204
}
191205

@@ -223,11 +237,11 @@ async function createPages({ graphql, actions, reporter }) {
223237
return
224238
}
225239

226-
const filteredEdges = data.allMdx.edges.filter((edge) => {
240+
const filteredEdges = data.allMdx.edges.filter((edge: { node: { parent: { sourceInstanceName: string; }; fields: { slug: any; }; }; }) => {
227241
if (edge.node.parent.sourceInstanceName === 'default-page') {
228242
const { slug } = edge.node.fields
229243
const hasCustom404 = data.allMdx.edges.find(
230-
(_edge) => edge !== _edge && _edge.node.fields.slug === slug,
244+
(_edge: { node: any; }) => edge !== _edge && _edge.node.fields.slug === slug,
231245
)
232246
return !hasCustom404
233247
}
@@ -249,7 +263,7 @@ async function createPages({ graphql, actions, reporter }) {
249263
path: node.fields.slug,
250264
component: path.resolve(
251265
__dirname,
252-
`./src/templates/${node.fields.pageType}.js?__contentFilePath=${node.internal.contentFilePath}`,
266+
`./src/templates/${node.fields.pageType}.ts?__contentFilePath=${node.internal.contentFilePath}`,
253267
),
254268
context: {
255269
id: node.id,

‎gatsby-ssr.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ export const wrapPageElement = ({ element, props }) => {
1313

1414
export const onRenderBody = ({ setPreBodyComponents }) => {
1515
setPreBodyComponents([getColorModeInitScriptElement()])
16-
}
16+
}

‎package.json‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"scripts": {
66
"develop": "gatsby develop",
77
"build": "gatsby build",
8-
"serve": "gatsby serve"
8+
"serve": "gatsby serve",
9+
"clean": "gatsby clean"
910
},
1011
"dependencies": {
1112
"@docsearch/css": "^3.3.2",
@@ -47,9 +48,16 @@
4748
"unist-util-visit": "^2.0.3"
4849
},
4950
"devDependencies": {
51+
"@types/node": "^18.13.0",
52+
"@types/prismjs": "^1.26.0",
53+
"@types/react": "^18.0.28",
54+
"@types/react-dom": "^18.0.10",
55+
"@types/react-helmet": "^6.1.6",
56+
"@types/styled-components": "^5.1.26",
5057
"gatsby": "^5.5.0",
5158
"react": "^18.2.0",
5259
"react-dom": "^18.2.0",
53-
"standard-version": "^9.5.0"
60+
"standard-version": "^9.5.0",
61+
"typescript": "^4.9.5"
5462
}
5563
}

‎src/components/AppHeader.tsx‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export function AppHeader() {
102102
return (
103103
<OuterHeader>
104104
<ScreenContainer>
105-
<NavSkipLink tabindex="0" href="#main">
105+
<NavSkipLink tabIndex={0} href="#main">
106106
Skip to content
107107
</NavSkipLink>
108108
<x.div row alignItems="center" flexWrap="nowrap" mx={-2}>
@@ -142,7 +142,9 @@ export function AppHeader() {
142142
<DocSearch {...data.site.siteMetadata.docSearch} />
143143
</x.div>
144144
) : null}
145-
<AppNav col="auto" px={2} />
145+
<x.div col="auto" px={2}>
146+
<AppNav />
147+
</x.div>
146148
</x.div>
147149
</ScreenContainer>
148150
</OuterHeader>

‎src/components/AppNav.tsx‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ export function AppNav() {
3131
</NavLink>
3232
</NavListItem>
3333
<NavListItem>
34-
<NavLink as={ColorModeSwitcher} />
34+
<NavLink
35+
as={ColorModeSwitcher}
36+
/>
3537
</NavListItem>
3638
</NavList>
3739
</Nav>

‎src/components/Button.tsx‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,13 @@ const InnerButton = styled.buttonBox`
6565
}
6666
`
6767

68+
interface ButtonProps {
69+
variant?: 'primary' | 'secondary';
70+
children: React.ReactNode;
71+
}
72+
6873
export const Button = React.forwardRef(
69-
({ variant = 'primary', children, ...props }, ref) => {
74+
({ variant = 'primary', children, ...props }: ButtonProps, ref: any) => {
7075
return (
7176
<AriakitButton ref={ref} data-variant={variant}>
7277
{(buttonProps) => (

‎src/components/Code.tsx‎

Lines changed: 54 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react'
22
import rangeParser from 'parse-numeric-range';
33
import styled, { useTheme, th, up, css, useColorMode } from '@xstyled/styled-components'
4-
import Highlight, { defaultProps } from 'prism-react-renderer'
4+
import Highlight, { defaultProps, Language, PrismTheme } from 'prism-react-renderer'
55
import {
66
LiveProvider,
77
LiveEditor,
@@ -55,12 +55,13 @@ const Pre = styled.pre`
5555
`
5656

5757
// line highlighting
58-
const calculateLinesToHighlight = (meta) => {
58+
const calculateLinesToHighlight = (meta: string) => {
5959
const REhighlights = /highlights={([\d,-]+)}/
60-
if (REhighlights.test(meta)) {
61-
const strlineNumbers = REhighlights.exec(meta)[1]
60+
const match = REhighlights.exec(meta)
61+
if (match) {
62+
const strlineNumbers = match[1]
6263
const lineNumbers = rangeParser(strlineNumbers)
63-
return (index) => (lineNumbers.includes(index + 1))
64+
return (index: number) => (lineNumbers.includes(index + 1))
6465
} else {
6566
return () => false
6667
}
@@ -79,10 +80,11 @@ const CodeLabel = styled.button`
7980
background: #0891B2;
8081
color: #ffffff;
8182
`
82-
const getCodeTitle = (meta) => {
83+
const getCodeTitle = (meta: string) => {
8384
const REtitle = /title={(.*?)}/
84-
if (REtitle.test(meta)) {
85-
const codeTitle = REtitle.exec(meta)[1]
85+
const match = REtitle.exec(meta)
86+
if (match) {
87+
const codeTitle = match[1]
8688
return <CodeLabel>{codeTitle}</CodeLabel>
8789
} else {
8890
return () => false
@@ -139,7 +141,7 @@ export function LiveConfig({ modules }) {
139141
return null
140142
}
141143

142-
function req(path) {
144+
function req(path: string | number) {
143145
const dep = globalModules[path]
144146

145147
if (!dep) {
@@ -150,7 +152,7 @@ function req(path) {
150152
return dep
151153
}
152154

153-
function importToRequire(code) {
155+
function importToRequire(code: string) {
154156
return (
155157
code
156158
// { a as b } => { a: b }
@@ -181,15 +183,24 @@ function importToRequire(code) {
181183
)
182184
}
183185

184-
export function usePrismTheme() {
185-
const theme = useTheme()
186-
const [mode] = useColorMode()
187-
return th(`prism-theme-${mode === 'light' ? 'light' : 'dark'}`)({ theme })
186+
export function usePrismTheme(): PrismTheme {
187+
const theme = useTheme();
188+
const [mode] = useColorMode();
189+
const prismTheme = th(`prism-theme-${mode === 'light' ? 'light' : 'dark'}`)({ theme });
190+
191+
if (typeof prismTheme !== 'object') {
192+
return {
193+
plain: {},
194+
styles: []
195+
};
196+
}
197+
198+
return prismTheme;
188199
}
189200

190201

191202
// setup clipboard copy function
192-
const copyToClipboard = (str) => {
203+
const copyToClipboard = (str: string) => {
193204
const el = document.createElement("textarea")
194205
el.value = str
195206
el.setAttribute("readonly", "")
@@ -226,14 +237,15 @@ const CopyCode = styled.button`
226237
`
227238

228239
export function Code({ children, lang = 'markup', metastring, meta }) {
229-
const prismTheme = usePrismTheme()
240+
const prismTheme = usePrismTheme() as PrismTheme;
241+
230242
if (/live/.test(meta)) {
231243
return (
232244
<LiveProvider
233245
code={children.trim()}
234246
transformCode={(code) => `${importToRequire(code)}`}
235247
scope={{ require: req }}
236-
language={lang}
248+
language={lang as Language}
237249
theme={prismTheme}
238250
noInline={/noInline/.test(meta)}
239251
>
@@ -242,7 +254,7 @@ export function Code({ children, lang = 'markup', metastring, meta }) {
242254
as="div"
243255
style={{ borderTopLeftRadius: 0, borderTopRightRadius: 0 }}
244256
>
245-
<LiveEditor padding={0} />
257+
<LiveEditor />
246258
</Pre>
247259
<LiveError />
248260
</LiveProvider>
@@ -254,30 +266,36 @@ export function Code({ children, lang = 'markup', metastring, meta }) {
254266
<Highlight
255267
{...defaultProps}
256268
code={children.trim()}
257-
language={lang}
269+
language={lang as Language}
258270
theme={prismTheme}
259271
>
260272
{({ className, style, tokens, getLineProps, getTokenProps }) => (
261273
<Pre className={className} style={style}>
262-
{getCodeTitle(metastring)}
263-
<div>
274+
<div>
275+
<>
276+
{getCodeTitle(metastring)}
277+
<div>
264278
<CopyCode onClick={handleClick}><FaClipboard/></CopyCode>
265-
{tokens.map((line, i) => {
266-
const lineProps = getLineProps({ line, key: i })
267-
if (shouldHighlightLine(i)) {
268-
lineProps.className = `${lineProps.className} highlight-line`
269-
}
270-
return (
271-
<div {...lineProps}>
272-
<LineNo>{i + 1}</LineNo>
273-
{line.map((token, key) => (
274-
<span {...getTokenProps({ token, key })} />
275-
))}
276-
</div>
277-
)
278-
})}
279+
<div>
280+
{tokens.map((line, i) => {
281+
const lineProps = getLineProps({ line, key: i })
282+
if (shouldHighlightLine(i)) {
283+
lineProps.className = `${lineProps.className} highlight-line`
284+
}
285+
return (
286+
<div {...lineProps}>
287+
<LineNo>{i + 1}</LineNo>
288+
{line.map((token, key) => (
289+
<span {...getTokenProps({ token, key })} />
290+
))}
291+
</div>
292+
)
293+
})}
279294
</div>
280-
</Pre>
295+
</div>
296+
</>
297+
</div>
298+
</Pre>
281299
)}
282300
</Highlight>
283301
)

0 commit comments

Comments
 (0)

Footer

© 2026 GitHub, Inc.

Back | FazBrowse Home | New Git URL