Skip to content
This repository has been archived by the owner on May 24, 2024. It is now read-only.

Commit

Permalink
Add links to the daybooks home page.
Browse files Browse the repository at this point in the history
This works by querying for the home page of the daybooks database (which
happens to be called "day books" in my Notion setup).
  • Loading branch information
hockeybuggy committed Jan 20, 2022
1 parent 9e81a3e commit d6f5176
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 10 deletions.
3 changes: 2 additions & 1 deletion src/clients/notion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import { Client } from "@notionhq/client";
class NotionClient {
notion: Client;

constructor (token: string) {
constructor(token: string) {
// Initializing a client
this.notion = new Client({ auth: token });
}

async searchByQuery(query: string): Promise<any> {
const result = await this.notion.search({ query });
// TODO clean up this fixture setup code
// fs.writeFileSync(`./build/fixture-notion-query-${query}.json`, JSON.stringify(result, null, 2));
return result;
}
Expand Down
55 changes: 46 additions & 9 deletions src/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,32 @@ class GenerateSiteService {
`;
}

async generateNotFoundPage(buildDir: string, now: Date) {
const contents = this.fromBaseTemplate(
now,
`
async generateNotFoundPage(
buildDir: string,
now: Date,
daybookRootUrl: string | null
) {
let inner = `
<h1>Daybook Redirects - Not found</h1>
<p>
<a href="https://app.netlify.com/sites/daybook-redirects/deploys?filter=main">
Whoops? Something wrong with the build?
</a>
</p>
);
`
);
`;
if (daybookRootUrl) {
inner =
inner +
`
<p>
<a href="${daybookRootUrl}">
Daybooks home page
</a>
</p>
`;
}
const contents = this.fromBaseTemplate(now, inner);

console.log("Generating not-found page.");
fs.writeFileSync(`${buildDir}/not-found.html`, contents);
Expand All @@ -98,13 +110,25 @@ class GenerateSiteService {
async generateIndexPage(
buildDir: string,
now: Date,
daybookRootUrl: string,
links: Array<{ name: string; url: string }>
) {
const linksUl = links.reduce((acc, curr) => {
const link = `<li><a href="${curr.url}">${curr.name}</a></li>\n`;
return acc + link;
}, "");

let daybookRootLink = "";
if (daybookRootUrl) {
daybookRootLink = `
<p>
<a href="${daybookRootUrl}">
Daybooks home page
</a>
</p>
`;
}

const contents = this.fromBaseTemplate(
now,
`
Expand All @@ -113,6 +137,8 @@ class GenerateSiteService {
<ul>
${linksUl}
</ul>
${daybookRootLink}
`
);
console.log("Generating index page.");
Expand Down Expand Up @@ -288,9 +314,20 @@ class GenerateSiteService {
const now = this.today;
console.log(`Running at: ${now}`);

// TODO drop this (consider moving the .users.me() example)
// const meResponse = await this.notion.users.me({});
// console.log(`User id: ${meResponse.id}`);

// Find the Notion database where all daybooks are kept
let daybookRootUrl = null;
const daybookRootPageQuery = await this.notion.searchByQuery("day books");
if (
daybookRootPageQuery.results &&
daybookRootPageQuery.results.length === 1
) {
daybookRootUrl = daybookRootPageQuery.results[0].url;
}

const buildDir = "./build";
console.log(`Setting up build directory: ${buildDir}`);
if (fs.existsSync(buildDir)) {
Expand All @@ -315,8 +352,8 @@ class GenerateSiteService {
) as Array<Page>;
console.log(generationErrors);

await this.generateNotFoundPage(buildDir, now);
await this.generateIndexPage(buildDir, now, pages);
await this.generateNotFoundPage(buildDir, now, daybookRootUrl);
await this.generateIndexPage(buildDir, now, daybookRootUrl, pages);

console.log("Copying static files into build directory");
fs.copySync("./static", buildDir);
Expand Down

0 comments on commit d6f5176

Please sign in to comment.