Skip to content

Commit

Permalink
Merge pull request #96 from otoyo/replace-blog-metadata-setter
Browse files Browse the repository at this point in the history
Replace blog metadata setter
  • Loading branch information
otoyo committed Mar 6, 2023
2 parents 5da0074 + 3407c32 commit 0838ff2
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 43 deletions.
7 changes: 1 addition & 6 deletions astro.config.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { defineConfig } from 'astro/config';
import { CUSTOM_DOMAIN, BASE_PATH } from './src/server-constants';
import BlogMetadataSetter from './src/integrations/blog-metadata-setter';
import FeaturedImageDownloader from './src/integrations/featured-image-downloader';
import PublicNotionCopier from './src/integrations/public-notion-copier';

Expand Down Expand Up @@ -30,9 +29,5 @@ const getSite = function () {
export default defineConfig({
site: getSite(),
base: BASE_PATH,
integrations: [
BlogMetadataSetter(),
FeaturedImageDownloader(),
PublicNotionCopier(),
],
integrations: [FeaturedImageDownloader(), PublicNotionCopier()],
});
14 changes: 0 additions & 14 deletions src/integrations/blog-metadata-setter.ts

This file was deleted.

15 changes: 8 additions & 7 deletions src/layouts/Layout.astro
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
---
import {
PUBLIC_SITE_TITLE,
PUBLIC_SITE_DESCRIPTION,
PUBLIC_GA_TRACKING_ID,
ENABLE_LIGHTBOX,
} from '../server-constants.ts'
import { getDatabase } from '../lib/notion/client.ts'
import { getNavLink, getStaticFilePath } from '../lib/blog-helpers.ts'
import '../styles/syntax-coloring.css'
import GoogleAnalytics from '../components/GoogleAnalytics.astro'
Expand All @@ -18,8 +17,10 @@ export interface Props {
const { title = '', description = '', path = '/', ogImage = '' } = Astro.props
const siteTitle = title ? `${title} - ${PUBLIC_SITE_TITLE}` : PUBLIC_SITE_TITLE
const siteDescription = description ? description : PUBLIC_SITE_DESCRIPTION
const database = await getDatabase()
const siteTitle = title ? `${title} - ${database.Title}` : database.Title
const siteDescription = description ? description : database.Description
const siteURL = new URL(path, Astro.site).toString()
const siteOGImage = new URL('/default-og-image.png', Astro.site)
---
Expand All @@ -37,7 +38,7 @@ const siteOGImage = new URL('/default-og-image.png', Astro.site)
<meta property="og:url" content={siteURL} />
<meta property="og:title" content={siteTitle} />
<meta property="og:description" content={siteDescription} />
<meta property="og:site_name" content={PUBLIC_SITE_TITLE} />
<meta property="og:site_name" content={database.Title} />
<meta property="og:image" content={ogImage || siteOGImage} />
<meta name="twitter:title" content={siteTitle} />
<meta name="twitter:description" content={siteDescription} />
Expand All @@ -54,10 +55,10 @@ const siteOGImage = new URL('/default-og-image.png', Astro.site)
<GoogleAnalytics trackingId={PUBLIC_GA_TRACKING_ID} />
<header class="header">
<h1>
<a href={getNavLink('/')}>{PUBLIC_SITE_TITLE}</a>
<a href={getNavLink('/')}>{database.Title}</a>
</h1>
<div class="description">
{PUBLIC_SITE_DESCRIPTION}
{database.Description}
</div>
</header>
<div class="container">
Expand Down
16 changes: 11 additions & 5 deletions src/lib/notion/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,12 @@ const client = new Client({
auth: NOTION_API_SECRET,
})

let cache: Post[] | null = null
let postsCache: Post[] | null = null
let dbCache: Database | null = null

export async function getAllPosts(): Promise<Post[]> {
if (cache !== null) {
return Promise.resolve(cache)
if (postsCache !== null) {
return Promise.resolve(postsCache)
}

const params: requestParams.QueryDatabase = {
Expand Down Expand Up @@ -104,10 +105,10 @@ export async function getAllPosts(): Promise<Post[]> {
params['start_cursor'] = res.next_cursor as string
}

cache = results
postsCache = results
.filter((pageObject) => _validPageObject(pageObject))
.map((pageObject) => _buildPost(pageObject))
return cache
return postsCache
}

export async function getPosts(pageSize = 10): Promise<Post[]> {
Expand Down Expand Up @@ -357,6 +358,10 @@ export async function downloadFile(url: URL) {
}

export async function getDatabase(): Promise<Database> {
if (dbCache !== null) {
return Promise.resolve(dbCache)
}

const params: requestParams.RetrieveDatabase = {
database_id: DATABASE_ID,
}
Expand Down Expand Up @@ -397,6 +402,7 @@ export async function getDatabase(): Promise<Database> {
Cover: cover,
}

dbCache = database
return database
}

Expand Down
9 changes: 4 additions & 5 deletions src/pages/feed.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import rss from '@astrojs/rss'
import { PUBLIC_SITE_TITLE, PUBLIC_SITE_DESCRIPTION } from '../server-constants'
import { getAllPosts } from '../lib/notion/client'
import { getAllPosts, getDatabase } from '../lib/notion/client'
import { getPostLink } from '../lib/blog-helpers'

export async function get() {
const posts = await getAllPosts()
const [posts, database] = await Promise.all([getAllPosts(), getDatabase()])

return rss({
title: PUBLIC_SITE_TITLE,
description: PUBLIC_SITE_DESCRIPTION,
title: database.Title,
description: database.Description,
site: import.meta.env.SITE,
items: posts.map((post) => ({
link: new URL(getPostLink(post.Slug), import.meta.env.SITE).toString(),
Expand Down
6 changes: 0 additions & 6 deletions src/server-constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,3 @@ export const REQUEST_TIMEOUT_MS = parseInt(
10
)
export const ENABLE_LIGHTBOX = import.meta.env.ENABLE_LIGHTBOX

export const PUBLIC_SITE_TITLE =
import.meta.env.SITE_TITLE || 'astro-notion-blog'
export const PUBLIC_SITE_DESCRIPTION =
import.meta.env.SITE_DESCRIPTION ||
'astro-notion-blog is generated statically by Astro'

0 comments on commit 0838ff2

Please sign in to comment.