-
Notifications
You must be signed in to change notification settings - Fork 543
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add caching, rss generation, and update screenshot
- Loading branch information
Showing
10 changed files
with
205 additions
and
42 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import { resolve } from 'path' | ||
import { writeFile } from './fs-helpers' | ||
import { renderToStaticMarkup } from 'react-dom/server' | ||
|
||
import { textBlock } from './notion/renderers' | ||
import getBlogIndex from './notion/getBlogIndex' | ||
import getNotionUsers from './notion/getNotionUsers' | ||
import { postIsReady, getBlogLink } from './blog-helpers' | ||
|
||
// must use weird syntax to bypass auto replacing of NODE_ENV | ||
process.env['NODE' + '_ENV'] = 'production' | ||
|
||
// constants | ||
const NOW = new Date().toJSON() | ||
|
||
function mapToAuthor(author) { | ||
return `<author><name>${author.full_name}</name></author>` | ||
} | ||
|
||
function decode(string) { | ||
return string | ||
.replace(/&/g, '&') | ||
.replace(/</g, '<') | ||
.replace(/>/g, '>') | ||
.replace(/"/g, '"') | ||
.replace(/'/g, ''') | ||
} | ||
|
||
function mapToEntry(post) { | ||
return ` | ||
<entry> | ||
<id>${post.link}</id> | ||
<title>${decode(post.title)}</title> | ||
<link href="${post.link}"/> | ||
<updated>${new Date(post.date).toJSON()}</updated> | ||
<content type="xhtml"> | ||
<div xmlns="http://www.w3.org/1999/xhtml"> | ||
${renderToStaticMarkup( | ||
post.preview | ||
? (post.preview || []).map((block, idx) => | ||
textBlock(block, false, post.title + idx) | ||
) | ||
: post.content | ||
)} | ||
<p class="more"> | ||
<a href="${post.link}">Read more</a> | ||
</p> | ||
</div> | ||
</content> | ||
${(post.authors || []).map(mapToAuthor).join('\n ')} | ||
</entry>` | ||
} | ||
|
||
function concat(total, item) { | ||
return total + item | ||
} | ||
|
||
function createRSS(blogPosts = []) { | ||
const postsString = blogPosts.map(mapToEntry).reduce(concat, '') | ||
|
||
return `<?xml version="1.0" encoding="utf-8"?> | ||
<feed xmlns="http://www.w3.org/2005/Atom"> | ||
<title>My Blog</title> | ||
<subtitle>Blog</subtitle> | ||
<link href="/atom" rel="self" type="application/rss+xml"/> | ||
<link href="/" /> | ||
<updated>${NOW}</updated> | ||
<id>My Notion Blog</id>${postsString} | ||
</feed>` | ||
} | ||
|
||
async function main() { | ||
const postsTable = await getBlogIndex(true) | ||
const neededAuthors = new Set<string>() | ||
|
||
const blogPosts = Object.keys(postsTable) | ||
.map(slug => { | ||
const post = postsTable[slug] | ||
if (!postIsReady(post)) return | ||
|
||
post.authors = post.Authors || [] | ||
|
||
for (const author of post.authors) { | ||
neededAuthors.add(author) | ||
} | ||
return post | ||
}) | ||
.filter(Boolean) | ||
|
||
const { users } = await getNotionUsers([...neededAuthors]) | ||
|
||
blogPosts.forEach(post => { | ||
post.authors = post.authors.map(id => users[id]) | ||
post.link = getBlogLink(post.Slug) | ||
post.title = post.Page | ||
post.date = post.Date | ||
}) | ||
|
||
const outputPath = './public/atom' | ||
await writeFile(resolve(outputPath), createRSS(blogPosts)) | ||
console.log(`Atom feed file generated at \`${outputPath}\``) | ||
} | ||
|
||
main().catch(error => console.error(error)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import fs from 'fs' | ||
import { promisify } from 'util' | ||
|
||
export const readFile = promisify(fs.readFile) | ||
export const writeFile = promisify(fs.writeFile) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
import path from 'path' | ||
|
||
export const NOTION_TOKEN = process.env.NOTION_TOKEN | ||
export const BLOG_INDEX_ID = process.env.BLOG_INDEX_ID | ||
export const API_ENDPOINT = 'https://www.notion.so/api/v3' | ||
export const BLOG_INDEX_CACHE = path.resolve('.blog_index_data') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#54