Skip to content

Commit f97781c

Browse files
build from notion
1 parent 71cde1a commit f97781c

5 files changed

Lines changed: 203 additions & 3 deletions

File tree

content/blog/don't-publish.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
title: Don’t publish
3+
date: 2025-12-30T11:54:00.000Z
4+
draft: true
5+
6+
---
7+
undefined

content/blog/example-page.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
title: Example page
3+
description: Description test
4+
date: 2025-12-30T11:50:00.000Z
5+
draft: false
6+
7+
---
8+
9+
## Test
10+
11+
12+
This is an example of a post that I wrote using Notion. This way it’ll be easier to update my blog.
13+
14+
15+
More complicated things
16+
17+
- list
18+
- of
19+
- items
20+
21+
Here’s a [link](https://patworld.world/)
22+

eleventy.config.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,73 @@ import { eleventyImageTransformPlugin } from "@11ty/eleventy-img";
66

77
import pluginFilters from "./_config/filters.js";
88

9+
import * as fs from 'fs';
10+
import { Client } from '@notionhq/client';
11+
import { NotionToMarkdown } from 'notion-to-md';
12+
import slugify from 'slugify';
13+
import * as YAML from 'yaml';
14+
915
/** @param {import("@11ty/eleventy").UserConfig} eleventyConfig */
1016
export default async function(eleventyConfig) {
17+
18+
eleventyConfig.on("eleventy.beforeConfig", async ({ directories, runMode, outputMode }) => {
19+
const database_id = env.NOTION_DATABASE_ID
20+
21+
if (!database_id || !env.NOTION_API_KEY) return
22+
23+
const notion = new Client({
24+
auth: env.NOTION_API_KEY
25+
});
26+
27+
const { data_sources } = await notion.databases.retrieve({
28+
database_id,
29+
});
30+
31+
const data_source_id = data_sources[0].id
32+
33+
const { results } = await notion.dataSources.query({
34+
data_source_id,
35+
})
36+
37+
const pageProperties = results.reduce((res, { id, properties }) => ({
38+
...res,
39+
[id]: {
40+
title: properties["Name"].title[0]?.plain_text,
41+
description: properties["Description"].rich_text[0]?.plain_text,
42+
date: properties["Last edited time"]?.last_edited_time,
43+
draft: !properties["Live"].checkbox,
44+
}
45+
}), {})
46+
47+
const block_ids = results.map(({ id }) => id)
48+
49+
// passing notion client to the option
50+
const n2m = new NotionToMarkdown({
51+
notionClient: notion,
52+
});
53+
54+
await Promise.all(
55+
block_ids.map(async (block_id) => {
56+
const mdblocks = await n2m.pageToMarkdown(block_id);
57+
const mdString = n2m.toMarkdownString(mdblocks);
58+
59+
const { title } = pageProperties[block_id];
60+
61+
const filename = `${slugify(title.toLowerCase())}.md`;
62+
63+
const frontmatter = "---\n"+ YAML.stringify(pageProperties[block_id]) + "\n---\n"
64+
65+
try {
66+
fs.writeFileSync(`content/blog/${filename}`, frontmatter + mdString.parent)
67+
} catch (e) {
68+
console.log(`Writing ${filename} failed: ${e}`)
69+
}
70+
71+
console.log(`Wrote ${filename} successfully.`)
72+
})
73+
)
74+
})
75+
1176
// Drafts, see also _data/eleventyDataSchema.js
1277
eleventyConfig.addPreprocessor("drafts", "*", (data, content) => {
1378
if (data.draft) {

package-lock.json

Lines changed: 103 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@
4747
"zod-validation-error": "^3.5.2"
4848
},
4949
"dependencies": {
50-
"@zachleat/heading-anchors": "^1.0.5"
50+
"@zachleat/heading-anchors": "^1.0.5",
51+
"@notionhq/client": "^5.6.0",
52+
"markdown-it-footnote": "^4.0.0",
53+
"notion-to-md": "^3.1.9",
54+
"slugify": "^1.6.6",
55+
"yaml": "^2.8.2"
5156
}
5257
}

0 commit comments

Comments
 (0)