From 679131ad22b0aa581ed9460096f8df12a02e5029 Mon Sep 17 00:00:00 2001 From: Travis Fischer Date: Thu, 7 Apr 2022 12:38:58 -0400 Subject: [PATCH] feat: WIP custom header nav --- .env.example | 2 +- components/NotionPage.tsx | 4 +++- components/NotionPageHeader.tsx | 26 ++++++++++++++++++++++++ lib/config.ts | 17 ++++++++++++---- lib/get-config-value.ts | 9 +++++---- lib/site-config.ts | 34 ++++++++++++++++++++++++++++++++ lib/types.ts | 2 ++ readme.md | 10 +++++----- site.config.js => site.config.ts | 8 ++++++-- styles/notion.css | 2 +- tsconfig.json | 2 +- 11 files changed, 97 insertions(+), 19 deletions(-) create mode 100644 components/NotionPageHeader.tsx create mode 100644 lib/site-config.ts rename site.config.js => site.config.ts (93%) diff --git a/.env.example b/.env.example index 04dfc9a209..bca6ff57c3 100644 --- a/.env.example +++ b/.env.example @@ -18,7 +18,7 @@ # Optional (for persisting preview images to redis) # NOTE: if you want to enable redis, only REDIS_HOST and REDIS_PASSWORD are required -# NOTE: don't forget to set isRedisEnabled to true in the site.config.js file +# NOTE: don't forget to set isRedisEnabled to true in the site.config.ts file #REDIS_HOST= #REDIS_PASSWORD= #REDIS_USER='default' diff --git a/components/NotionPage.tsx b/components/NotionPage.tsx index 38cedd46fa..e6a6638f0d 100644 --- a/components/NotionPage.tsx +++ b/components/NotionPage.tsx @@ -31,6 +31,7 @@ import { PageHead } from './PageHead' import { PageActions } from './PageActions' import { Footer } from './Footer' import { PageSocial } from './PageSocial' +import { NotionPageHeader } from './NotionPageHeader' import { GitHubShareButton } from './GitHubShareButton' import styles from './styles.module.css' @@ -181,7 +182,8 @@ export const NotionPage: React.FC = ({ Equation, Pdf, Modal, - Tweet + Tweet, + Header: NotionPageHeader }} recordMap={recordMap} rootPageId={site.rootNotionPageId} diff --git a/components/NotionPageHeader.tsx b/components/NotionPageHeader.tsx new file mode 100644 index 0000000000..e4853e420a --- /dev/null +++ b/components/NotionPageHeader.tsx @@ -0,0 +1,26 @@ +import React from 'react' +// import useDarkMode from '@fisch0920/use-dark-mode' + +import { Header, Breadcrumbs, Search } from 'react-notion-x' + +import * as types from 'lib/types' +import { navigationStyle } from 'lib/config' + +// import styles from './styles.module.css' + +export const NotionPageHeader: React.FC<{ + block: types.CollectionViewPageBlock | types.PageBlock +}> = ({ block }) => { + if (navigationStyle === 'default') { + return
+ } + + return ( +
+
+ + +
+
+ ) +} diff --git a/lib/config.ts b/lib/config.ts index d26d842e1f..c77e97302b 100644 --- a/lib/config.ts +++ b/lib/config.ts @@ -1,14 +1,18 @@ /** * Site-wide app configuration. * - * This file pulls from the root "site.config.js" as well as environment variables + * This file pulls from the root "site.config.ts" as well as environment variables * for optional depenencies. */ import { parsePageId } from 'notion-utils' import posthog from 'posthog-js' import { getEnv, getSiteConfig } from './get-config-value' -import { PageUrlOverridesInverseMap, PageUrlOverridesMap } from './types' +import { + PageUrlOverridesInverseMap, + PageUrlOverridesMap, + NavigationStyle +} from './types' export const rootNotionPageId: string = parsePageId( getSiteConfig('rootNotionPageId'), @@ -48,9 +52,9 @@ export const description: string = getSiteConfig('description', 'Notion Blog') // social accounts export const twitter: string | null = getSiteConfig('twitter', null) -export const zhihu: string | null = getSiteConfig('zhihu', null) export const github: string | null = getSiteConfig('github', null) export const linkedin: string | null = getSiteConfig('linkedin', null) +export const zhihu: string | null = getSiteConfig('zhihu', null) // default notion values for site-wide consistency (optional; may be overridden on a per-page basis) export const defaultPageIcon: string | null = getSiteConfig( @@ -78,12 +82,17 @@ export const isTweetEmbedSupportEnabled: boolean = getSiteConfig( true ) -// where it all starts -- the site's root Notion page +// Optional whether or not to include the Notion ID in page URLs or just use slugs export const includeNotionIdInUrls: boolean = getSiteConfig( 'includeNotionIdInUrls', !!isDev ) +export const navigationStyle: NavigationStyle = getSiteConfig( + 'navigationStyle', + 'default' +) + // ---------------------------------------------------------------------------- // Optional redis instance for persisting preview images diff --git a/lib/get-config-value.ts b/lib/get-config-value.ts index a702150d94..c3b9b7adef 100644 --- a/lib/get-config-value.ts +++ b/lib/get-config-value.ts @@ -1,11 +1,12 @@ import rawSiteConfig from '../site.config' +import { SiteConfig } from './site-config' if (!rawSiteConfig) { - throw new Error(`Config error: invalid site.config.js`) + throw new Error(`Config error: invalid site.config.ts`) } -// TODO: allow environment variables to override site.config.js -let siteConfigOverrides +// allow environment variables to override site.config.ts +let siteConfigOverrides: SiteConfig try { if (process.env.NEXT_PUBLIC_SITE_CONFIG) { @@ -16,7 +17,7 @@ try { throw err } -const siteConfig = { +const siteConfig: SiteConfig = { ...rawSiteConfig, ...siteConfigOverrides } diff --git a/lib/site-config.ts b/lib/site-config.ts new file mode 100644 index 0000000000..70c57783f5 --- /dev/null +++ b/lib/site-config.ts @@ -0,0 +1,34 @@ +import * as types from './types' + +export interface SiteConfig { + rootNotionPageId: string + rootNotionSpaceId?: string + + name: string + domain: string + author: string + description?: string + + twitter?: string + github?: string + linkedin?: string + zhihu?: string + + defaultPageIcon?: string | null + defaultPageCover?: string | null + defaultPageCoverPosition?: number | null + + isPreviewImageSupportEnabled?: boolean + isTweetEmbedSupportEnabled?: boolean + isRedisEnabled?: boolean + + includeNotionIdInUrls?: boolean + pageUrlOverrides?: types.PageUrlOverridesMap + pageUrlAdditions?: types.PageUrlOverridesMap + + navigationStyle?: types.NavigationStyle +} + +export const siteConfig = (config: SiteConfig): SiteConfig => { + return config +} diff --git a/lib/types.ts b/lib/types.ts index 3ac70f05a3..e50121ebb8 100644 --- a/lib/types.ts +++ b/lib/types.ts @@ -2,6 +2,8 @@ import { ExtendedRecordMap, PageMap } from 'notion-types' export * from 'notion-types' +export type NavigationStyle = 'default' | 'custom' + export interface PageError { message?: string statusCode: number diff --git a/readme.md b/readme.md index d438d6551a..61bc574ebf 100644 --- a/readme.md +++ b/readme.md @@ -18,7 +18,7 @@ It uses Notion as a CMS, [react-notion-x](https://github.com/NotionX/react-notio ## Features -- Setup only takes a few minutes ([single config file](./site.config.js)) 💪 +- Setup only takes a few minutes ([single config file](./site.config.ts)) 💪 - Robust support for Notion content via [react-notion-x](https://github.com/NotionX/react-notion-x) - Built using Next.js, TS, and React - Excellent page speeds @@ -38,12 +38,12 @@ It uses Notion as a CMS, [react-notion-x](https://github.com/NotionX/react-notio ## Setup -**All config is defined in [site.config.js](./site.config.js).** +**All config is defined in [site.config.ts](./site.config.ts).** This project requires a recent version of Node.js (>= 14.17). 1. Fork / clone this repo -2. Change a few values in [site.config.js](./site.config.js) +2. Change a few values in [site.config.ts](./site.config.ts) 3. `npm install` 4. `npm run dev` to test locally 5. `npm run deploy` to deploy to vercel 💪 @@ -82,11 +82,11 @@ NOTE: if you have multiple pages in your workspace with the same slugified name, We use [next/image](https://nextjs.org/docs/api-reference/next/image) to serve images efficiently, with preview images optionally generated via [lqip-modern](https://github.com/transitive-bullshit/lqip-modern). This gives us extremely optimized image support for sexy smooth images. -Preview images are **enabled by default**, but they can be slow to generate, so if you want to disable them, set `isPreviewImageSupportEnabled` to `false` in `site.config.js`. +Preview images are **enabled by default**, but they can be slow to generate, so if you want to disable them, set `isPreviewImageSupportEnabled` to `false` in `site.config.ts`. ### Redis -If you want to cache generated preview images to speed up subsequent builds, you'll need to first set up an external [Redis](https://redis.io) data store. To enable redis caching, set `isRedisEnabled` to `true` in `site.config.js` and then set `REDIS_HOST` and `REDIS_PASSWORD` environment variables to point to your redis instance. +If you want to cache generated preview images to speed up subsequent builds, you'll need to first set up an external [Redis](https://redis.io) data store. To enable redis caching, set `isRedisEnabled` to `true` in `site.config.ts` and then set `REDIS_HOST` and `REDIS_PASSWORD` environment variables to point to your redis instance. You can do this locally by adding a `.env` file: diff --git a/site.config.js b/site.config.ts similarity index 93% rename from site.config.js rename to site.config.ts index 7cb9a72402..afcde4cc90 100644 --- a/site.config.js +++ b/site.config.ts @@ -1,4 +1,6 @@ -export default { +import { siteConfig } from './lib/site-config' + +export default siteConfig({ // the site's root Notion page (required) rootNotionPageId: '7875426197cf461698809def95960ebf', @@ -25,6 +27,8 @@ export default { defaultPageCover: null, defaultPageCoverPosition: 0.5, + navigationStyle: 'custom', + // whether or not to enable support for LQIP preview images (optional) isPreviewImageSupportEnabled: true, @@ -42,4 +46,4 @@ export default { // '/bar': '0be6efce9daf42688f65c76b89f8eb27' // } pageUrlOverrides: null -} +}) diff --git a/styles/notion.css b/styles/notion.css index 112429c13b..2eebe20d27 100644 --- a/styles/notion.css +++ b/styles/notion.css @@ -39,7 +39,7 @@ width: 100% !important; } -.notion-header .nav-header { +.notion-header .notion-nav-header { max-width: 1100px; margin: 0 auto; } diff --git a/tsconfig.json b/tsconfig.json index 1fc4cb518c..73d666205e 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -19,5 +19,5 @@ "incremental": true }, "exclude": ["node_modules"], - "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"] + "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", "site.config.ts"] }