Skip to content

Commit

Permalink
improve types definitions (#2298)
Browse files Browse the repository at this point in the history
  • Loading branch information
gdams authored Oct 7, 2023
1 parent e3c058c commit 36de08d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 96 deletions.
69 changes: 19 additions & 50 deletions gatsby-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,72 +8,39 @@ import path from 'path'
import adapter from 'gatsby-adapter-netlify'
import locales from './locales/i18n'

interface SiteMetadata {
title: string;
description: string;
author: string;
siteUrl: string;
social: {
twitter: string
}
}

interface AsciidocNode {
id: string;
document: {
title: string;
};
fields: {
slug: string;
};
html: string;
}
import type { GatsbyConfig } from 'gatsby'
import type { MDXPage, AsciidocPage } from './src/types'

interface LocalSearchNormalizerArgs {
data: {
allAsciidoc: {
edges: Array<{ node: AsciidocNode }>;
edges: Array<{ node: AsciidocPage }>;
};
};
}

interface MdxNode {
excerpt: string;
fields: {
slug: string;
postPath: string;
};
frontmatter: {
date: string;
title: string;
};
}
type SiteMetadata = GatsbyConfig['siteMetadata'] & {
siteUrl: string;
};

interface AllMdxQueryResult {
allMdx: {
totalCount: number;
edges: {
node: MdxNode;
}[];
};
allMdx: MDXPage;
site: {
siteMetadata: SiteMetadata;
}
}

const metadata: SiteMetadata = {
title: 'Adoptium',
description: 'Eclipse Adoptium provides prebuilt OpenJDK binaries ...',
author: 'Eclipse Adoptium',
siteUrl: 'https://adoptium.net',
social: {
twitter: 'Adoptium'
}
};

module.exports = {
const config: GatsbyConfig = {
adapter: adapter(),
siteMetadata: metadata,
siteMetadata: {
title: 'Adoptium',
description: 'Eclipse Adoptium provides prebuilt OpenJDK binaries ...',
author: 'Eclipse Adoptium',
siteUrl: 'https://adoptium.net',
social: {
twitter: 'Adoptium'
}
},
plugins: [
'gatsby-plugin-sitemap',
{
Expand Down Expand Up @@ -325,3 +292,5 @@ module.exports = {
}
]
}

export default config
59 changes: 13 additions & 46 deletions gatsby-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { promisify } from 'util'
import { createFilePath } from 'gatsby-source-filesystem'
import locales from './locales/i18n'
import authors from './src/json/authors.json'
import type { Actions, GatsbyNode, Node } from 'gatsby'
import type { GatsbyNode, Node } from 'gatsby'

import { localizedSlug, findKey, removeTrailingSlash } from './src/util/gatsby-node-helpers'
const exec = util.promisify(execChild)
Expand All @@ -28,26 +28,6 @@ interface AdoptiumData {
most_recent_feature_version: string;
}

interface Page {
path: string;
component: string;
context: {
locale: string;
defaultGitSHA?: string | null;
language: string;
i18n: {
routed: boolean;
originalPath: string;
path: string;
language: string;
};
};
}

interface Action {
createNodeField: (field: any) => void;
}

interface MdxNode extends Node {
frontmatter: {
date: string;
Expand All @@ -58,25 +38,6 @@ interface MdxNode extends Node {
}
}

interface FileNode {
id: string;
relativePath: string;
absolutePath: string;
}

interface CreateNodeArgs {
node: Node;
actions: Action;
getNode: (id: string) => FileNode;
getNodes: () => FileNode[];
}

interface CreatePageArgs {
page: Page;
actions: Actions;
getNodes: () => Node[];
}

interface LocaleEntry {
default?: boolean;
locale: string;
Expand Down Expand Up @@ -160,9 +121,14 @@ export const sourceNodes: GatsbyNode['sourceNodes'] = async ({ actions, createNo
createNode(MostRecentFeatureVersion)
}

export const onCreatePage = ({ page, actions, getNodes }: CreatePageArgs): void => {
export const onCreatePage: GatsbyNode['onCreatePage'] = async ({ page, actions, getNodes }) => {
const { createPage, deletePage } = actions

// Throw error if page.context is undefined
if (!page.context) {
throw new Error('Error retrieving page context')
}

// Delete pages such as /about/index.de
if (page.path.includes('index')) {
return deletePage(page)
Expand Down Expand Up @@ -200,12 +166,12 @@ export const onCreatePage = ({ page, actions, getNodes }: CreatePageArgs): void
// This context also gets passed to the src/components/layout file
// This should ensure that the locale is available on every page
context: {
...page.context,
...(page.context ? page.context : {}),
locale,
defaultGitSHA,
language: lang,
i18n: {
...page.context.i18n,
...(page.context?.i18n ? page.context.i18n : {}),
routed: true,
originalPath: page.path,
path: removeTrailingSlash(localizedPath),
Expand All @@ -231,7 +197,7 @@ export const onCreatePage = ({ page, actions, getNodes }: CreatePageArgs): void
})
}

export const onCreateNode = async ({ node, actions, getNode, getNodes }: CreateNodeArgs) => {
export const onCreateNode: GatsbyNode['onCreateNode'] = async ({ node, actions, getNode, getNodes }) => {
const { createNodeField } = actions

if (node.internal.type === 'Asciidoc') {
Expand All @@ -240,9 +206,10 @@ export const onCreateNode = async ({ node, actions, getNode, getNodes }: CreateN
// Handle the case where fetchFilePath is not found.
throw new Error(`No file path found for node with parent ID: ${node.parent}`);
}
const name = path.basename(fetchFilePath.relativePath, '.adoc')

const currentFileDir = path.dirname(fetchFilePath.absolutePath)
const name = path.basename(fetchFilePath.relativePath as string, '.adoc')

const currentFileDir = path.dirname(fetchFilePath.absolutePath as string);
const partialDir = path.join(currentFileDir, '_partials')

// Check if post.name is "index" -- because that's the file for default language
Expand Down

0 comments on commit 36de08d

Please sign in to comment.