Skip to content

Commit 0bb73c8

Browse files
committed
feat: support image block
fix #4
1 parent d5e9c46 commit 0bb73c8

File tree

10 files changed

+5768
-29
lines changed

10 files changed

+5768
-29
lines changed

README.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -266,21 +266,25 @@ As this plugin relies on the the official Notion API which is still in beta, we
266266

267267
- Sub Page
268268
- Call out
269-
- Image
270269
- Video
271270
- Code Block
272271
- Coloured Text
273272
- Underlined Text
274273

275274
## Image
276275

277-
As of `2021-07-01`, the official Notion API doesn't export any image block from a page.
278-
However, there is a workaround that you can use to inject images into an article.
279-
You just need to embed them using the normal markdown syntax as part of your paragraph like this:
276+
Since v3.3, `gatsby-source-notion` provided a limited support for images on a Notion page.
277+
Now, all images on a Notion page will be translated into the `![caption](url)` syntax and subsequently converted to a `img` element if you use `gatsby-plugin-mdx`.
280278

281-
```md
282-
![alt text](path-to-image)
283-
```
279+
However, it has a caveat. Embedding images would probably be missing in your final web site.
280+
It is because the embedded image URL returned from Notion is valid only for 1 hour.
281+
[This is a restriction from Notion.](https://developers.notion.com/reference/file-object#files-uploaded-to-notion-objects)
282+
As gatsby would not download the image for further processing,
283+
you simply cannot use Notion as your image host.
284+
285+
Plugins like [gatsby-remark-images-anywhere](https://github.com/d4rekanguok/gatsby-remark-images-anywhere) would solve the issue.
286+
However, as of `2022-07-01` there is no similar plugin that supports Gatsby V4.
287+
Let us know if you find any.
284288

285289
# Cover & Icon
286290

e2e/gatsby-config.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,17 @@ export const graphqlTypegen: GatsbyConfig['graphqlTypegen'] = true;
2525
export const jsxRuntime: GatsbyConfig['jsxRuntime'] = 'automatic';
2626

2727
export const plugins: GatsbyConfig['plugins'] = [
28+
'gatsby-plugin-sharp',
2829
{
2930
resolve: 'gatsby-source-notion',
3031
options: {
3132
previewCallRate: 0.5,
3233
},
3334
},
35+
{
36+
resolve: 'gatsby-transformer-remark',
37+
options: {
38+
plugins: ['gatsby-remark-images'],
39+
},
40+
},
3441
];

e2e/gatsby-node.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* MIT LICENSE ***
3+
* -------------------------------------------------------------------------
4+
* This code may be modified and distributed under the MIT license.
5+
* See the LICENSE file for details.
6+
* -------------------------------------------------------------------------
7+
* @summary Create pages via Gatsby Node API
8+
* @author Alvis HT Tang <alvis@hilbert.space>
9+
* @license MIT
10+
* @copyright Copyright (c) 2022 - All Rights Reserved.
11+
* -------------------------------------------------------------------------
12+
*/
13+
14+
import { resolve } from 'node:path';
15+
16+
import type { Actions, CreatePagesArgs, GatsbyNode } from 'gatsby';
17+
18+
export const createPages: GatsbyNode['createPages'] = async ({
19+
actions: { createPage },
20+
graphql,
21+
}) => {
22+
createProjectPages(await queryProjects(graphql), createPage);
23+
};
24+
25+
/**
26+
* create project pages
27+
* @param projects projects metadata
28+
* @param createPage the createPage function
29+
*/
30+
function createProjectPages(
31+
projects: Queries.ProjectsQuery | undefined,
32+
createPage: Actions['createPage'],
33+
): void {
34+
projects?.notionDatabase?.childrenNotionPage?.forEach((page) => {
35+
const mdx = page?.childMarkdownRemark;
36+
const path = page?.properties?.path;
37+
38+
if (mdx && path) {
39+
createPage({
40+
path,
41+
// NOTE: must resolve to the absolute path of the component
42+
component: resolve(__dirname, 'src', 'templates', 'project.tsx'),
43+
context: { id: mdx.id },
44+
});
45+
}
46+
});
47+
}
48+
49+
/**
50+
* get projects metadata
51+
* @param graphql the graphql query function
52+
* @returns projects metadata
53+
*/
54+
async function queryProjects(
55+
graphql: CreatePagesArgs['graphql'],
56+
): Promise<Queries.ProjectsQuery | undefined> {
57+
const { data } = await graphql<Queries.ProjectsQuery>(`
58+
query Projects {
59+
notionDatabase(title: { eq: "Projects" }) {
60+
childrenNotionPage {
61+
childMarkdownRemark {
62+
id
63+
}
64+
properties {
65+
path
66+
}
67+
}
68+
}
69+
}
70+
`);
71+
72+
return data;
73+
}

e2e/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
},
1313
"devDependencies": {
1414
"cypress": "^8.7.0",
15+
"gatsby-plugin-sharp": "^4.0.0",
16+
"gatsby-remark-images": "^6.0.0",
17+
"gatsby-transformer-remark": "^5.0.0",
1518
"presetter": "^3.0.0",
1619
"presetter-preset-react": "^3.0.0",
1720
"presetter-preset-strict": "^3.0.0",

e2e/src/pages/index.tsx

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* -------------------------------------------------------------------------
1414
*/
1515

16-
import { graphql } from 'gatsby';
16+
import { graphql, Link } from 'gatsby';
1717

1818
import type { PageProps } from 'gatsby';
1919
import type { FC } from 'react';
@@ -31,6 +31,9 @@ export const query = graphql`
3131
nodes {
3232
ref
3333
title
34+
properties {
35+
path
36+
}
3437
}
3538
}
3639
}
@@ -54,8 +57,14 @@ const Test: FC<PageProps<Queries.IndexPageQuery>> = ({ data }) => (
5457
<section id="pages">
5558
<h1>Pages</h1>
5659
<ul>
57-
{data.allNotionPage.nodes.map(({ ref, title }) => (
58-
<li key={ref}>{title}</li>
60+
{data.allNotionPage.nodes.map(({ ref, title, properties }) => (
61+
<li key={ref}>
62+
{properties?.path ? (
63+
<Link to={properties.path}>{title}</Link>
64+
) : (
65+
title
66+
)}
67+
</li>
5968
))}
6069
</ul>
6170
</section>

e2e/src/templates/project.tsx

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* MIT LICENSE ***
3+
* -------------------------------------------------------------------------
4+
* This code may be modified and distributed under the MIT license.
5+
* See the LICENSE file for details.
6+
* -------------------------------------------------------------------------
7+
* @summary Collection of layouts
8+
* @author Alvis HT Tang <alvis@hilbert.space>
9+
* @license MIT
10+
* @copyright Copyright (c) 2021 - All Rights Reserved.
11+
* -------------------------------------------------------------------------
12+
*/
13+
14+
import { graphql } from 'gatsby';
15+
16+
import type { PageProps } from 'gatsby';
17+
18+
export const query = graphql`
19+
query Project($id: String!) {
20+
markdownRemark(id: { eq: $id }) {
21+
frontmatter {
22+
title
23+
}
24+
html
25+
}
26+
}
27+
`;
28+
29+
const Project: React.FC<PageProps<Queries.ProjectQuery>> = ({ data }) => {
30+
// since this page is referred from the createPages API, it's safe to assume the mdx node is there
31+
const mdx = data.markdownRemark!;
32+
33+
const title = mdx.frontmatter?.title;
34+
const html = mdx.html || '';
35+
36+
return (
37+
<section className="py-12 px-4">
38+
<div className="mx-auto w-full max-w-2xl font-sans main">
39+
<h1 className="mt-2 mb-6 font-sans text-5xl font-semibold leading-tight text-center font-heading">
40+
{title}
41+
</h1>
42+
<div
43+
className="prose-sm"
44+
dangerouslySetInnerHTML={{ __html: html }}></div>
45+
</div>
46+
</section>
47+
);
48+
};
49+
50+
export default Project;

0 commit comments

Comments
 (0)