@@ -6,8 +6,73 @@ import { eleventyImageTransformPlugin } from "@11ty/eleventy-img";
66
77import 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 */
1016export 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 ) {
0 commit comments