Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/Add the file block #51

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/components/NotionBlocks.astro
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import NumberedListItems from './notion-blocks/NumberedListItems.astro'
import ToDo from './notion-blocks/ToDo.astro'
import SyncedBlock from './notion-blocks/SyncedBlock.astro'
import Toggle from './notion-blocks/Toggle.astro'
import File from './notion-blocks/File.astro'

export interface Props {
blocks: interfaces.Block[]
Expand Down Expand Up @@ -162,6 +163,8 @@ const bookmarkURLMap = await buildURLToHTMLMap(bookmarkURLs)
return <SyncedBlock block={block} headings={headings} />
case 'toggle':
return <Toggle block={block} headings={headings} />
case 'file':
return <File block={block} />
}
return null
})}
26 changes: 26 additions & 0 deletions src/components/notion-blocks/File.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
import * as interfaces from '../../lib/interfaces.ts'
export interface Props {
block: interfaces.Block
}

const { block } = Astro.props

let url: URL;
let fileName: string = 'File';
try {
url = new URL(block.FileBlock.File.External.Url)
fileName = block.FileBlock.File.External.Url.match(".+/(.+?)([\?#;].*)?$")[1];
} catch (err) {
console.error(err)
}
---

<div class="file">
<div>
{url && (<p><a href={url} target="_blank">&#128229; {fileName}</a></p>)}
</div>
</div>

<style>
</style>
9 changes: 9 additions & 0 deletions src/lib/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export interface Block {
Table?: Table
ColumnList?: ColumnList
TableOfContents?: TableOfContents
FileBlock?: FileBlock
}

export interface Paragraph {
Expand Down Expand Up @@ -103,8 +104,16 @@ export interface File {
ExpiryTime?: string
}

export interface FileBlock {
Type: string
File: {
External: External
}
}

export interface External {
Url: string
ExpiryTime?: string
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://developers.notion.com/reference/file-object
type: external の場合は expiry_time 持たなそうです。

}

export interface Code {
Expand Down
19 changes: 19 additions & 0 deletions src/lib/notion/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import type {
Text,
Annotation,
SelectProperty,
FileBlock,
} from '../interfaces'
// eslint-disable-next-line @typescript-eslint/no-var-requires
import { Client } from '@notionhq/client'
Expand Down Expand Up @@ -232,6 +233,10 @@ export async function getAllBlocksByBlockId(blockId: string): Promise<Block[]> {
if (Date.parse(block.Image.File.ExpiryTime) < Date.now()) {
block.Image = (await getBlock(block.Id)).Image
}
} else if (block.Type === 'file' && block.FileBlock && block.FileBlock.File && block.FileBlock.File.External.ExpiryTime) {
if (Date.parse(block.FileBlock.File.External.ExpiryTime) < Date.now()) {
block.FileBlock = (await getBlock(block.Id)).FileBlock
}
}
}

Expand Down Expand Up @@ -475,6 +480,20 @@ function _buildBlock(blockObject: responses.BlockObject): Block {
block.TableOfContents = tableOfContents
}
break
case 'file':
if (blockObject.file?.file) {
const FileBlock: FileBlock = {
Type: blockObject.file.type,
File: {
External: {
Url: blockObject.file.file.url,
ExpiryTime: blockObject.file.file.expiry_time
}
}
}
block.FileBlock = FileBlock
}
break
}

return block
Expand Down