11const { getPages } = require ( "./src/notion-api/get-pages" )
2- const { getNotionPageMD } = require ( "./src/transformers/get-page-md " )
2+ const { notionBlockToMarkdown } = require ( "./src/transformers/notion-block-to-markdown " )
33const { getNotionPageProperties } = require ( "./src/transformers/get-page-properties" )
44const { getNotionPageTitle } = require ( "./src/transformers/get-page-title" )
55const YAML = require ( "yaml" )
6+ const { createRemoteFileNode } = require ( "gatsby-source-filesystem" )
67
7- const NODE_TYPE = "Notion"
8+ const NOTION_NODE_TYPE = "Notion"
9+
10+ exports . onCreateNode = async ( { node, actions : { createNode } , createNodeId, getCache } ) => {
11+ if ( node . internal . type == NOTION_NODE_TYPE ) {
12+ const filesPropertyKey = Object . keys ( node . properties ) . find (
13+ ( key ) => node . properties [ key ] . type == "files" ,
14+ )
15+
16+ if ( filesPropertyKey ) {
17+ for ( let i = 0 ; i < node . properties [ filesPropertyKey ] . value . length ; i ++ ) {
18+ const name = node . properties [ filesPropertyKey ] . value [ i ] . name
19+
20+ if ( name . startsWith ( "http" ) ) {
21+ const fileNode = await createRemoteFileNode ( {
22+ url : name ,
23+ parentNodeId : node . id ,
24+ createNode,
25+ createNodeId,
26+ getCache,
27+ } )
28+
29+ if ( fileNode ) {
30+ node . properties [ filesPropertyKey ] . value [ i ] . remoteImage___NODE = fileNode . id
31+ }
32+ }
33+ }
34+ }
35+ }
36+ }
837
938exports . sourceNodes = async (
1039 { actions, createContentDigest, createNodeId, reporter } ,
11- pluginOptions ,
40+ { token , databaseId , propsToFrontmatter = true , lowerTitleLevel = true } ,
1241) => {
13- const pages = await getPages (
14- { token : pluginOptions . token , databaseId : pluginOptions . databaseId } ,
15- reporter ,
16- )
17-
18- /*
19- if (page.properties[key].type == "rich_text") {
20- page.properties[key].rich_text = blockToString(page.properties[key].rich_text)
21- }
22- */
42+ const pages = await getPages ( { token, databaseId } , reporter )
2343
2444 pages . forEach ( ( page ) => {
2545 const title = getNotionPageTitle ( page )
2646 const properties = getNotionPageProperties ( page )
27- const frontmatter = Object . keys ( properties ) . reduce (
28- ( acc , key ) => {
29- if ( properties [ key ] . type == "date" ) {
30- return {
31- ...acc ,
32- [ key ] : properties [ key ] . value . start ,
33- }
34- }
35-
36- if ( properties [ key ] . type == "rich_text" ) {
37- return {
38- ...acc ,
39- [ key ] : blockToString ( properties [ key ] . value ) ,
40- }
41- }
47+ let markdown = notionBlockToMarkdown ( page , lowerTitleLevel )
4248
43- return {
49+ if ( propsToFrontmatter ) {
50+ const frontmatter = Object . keys ( properties ) . reduce (
51+ ( acc , key ) => ( {
4452 ...acc ,
45- [ key ] : properties [ key ] . value ,
46- }
47- } ,
48- { title } ,
49- )
50- const markdown = "---\n"
51- . concat ( YAML . stringify ( frontmatter ) )
52- . concat ( "\n---\n\n" )
53- . concat ( getNotionPageMD ( page ) )
53+ [ key ] : properties [ key ] . value . remoteImage || properties [ key ] . value ,
54+ } ) ,
55+ { title } ,
56+ )
57+
58+ markdown = "---\n" . concat ( YAML . stringify ( frontmatter ) ) . concat ( "\n---\n\n" ) . concat ( markdown )
59+ }
5460
5561 actions . createNode ( {
56- id : createNodeId ( `${ NODE_TYPE } -${ page . id } ` ) ,
62+ id : createNodeId ( `${ NOTION_NODE_TYPE } -${ page . id } ` ) ,
5763 title,
5864 properties,
5965 archived : page . archived ,
@@ -65,7 +71,7 @@ exports.sourceNodes = async (
6571 parent : null ,
6672 children : [ ] ,
6773 internal : {
68- type : NODE_TYPE ,
74+ type : NOTION_NODE_TYPE ,
6975 mediaType : "text/markdown" ,
7076 content : markdown ,
7177 contentDigest : createContentDigest ( page ) ,
0 commit comments