Skip to content

Commit debb5e3

Browse files
committed
first working proto
1 parent f3c6fd1 commit debb5e3

File tree

6 files changed

+2084
-50
lines changed

6 files changed

+2084
-50
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules/
2+
tags

Dockerfile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
FROM node:current-alpine
2+
3+
ARG FOLDER
4+
# ARG NOTION_TOKEN
5+
# ARG NOTION_ROOT_ID
6+
7+
ENV FOLDER $FOLDER
8+
9+
# Copies your code file from your action repository to the filesystem path `/` of the container
10+
# COPY entrypoint.sh /entrypoint.sh
11+
12+
RUN npm install
13+
ENTRYPOINT ["npm run sync"]

action.yml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
name: 'Hello World'
2-
description: 'Greet someone and record the time'
1+
name: 'Sync repo docs to notion'
2+
description: 'Sync all you repository md files to notion wiki'
33
inputs:
44
who-to-greet: # id of input
55
description: 'Who to greet'
@@ -9,5 +9,7 @@ outputs:
99
time: # id of output
1010
description: 'The time we greeted you'
1111
runs:
12-
using: 'node16'
13-
main: 'index.js'
12+
using: 'docker'
13+
image: 'Dockerfile'
14+
args:
15+
- ${{ inputs.who-to-greet }}

index.js

Lines changed: 100 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,101 @@
1-
const core = require('@actions/core');
2-
const github = require('@actions/github');
3-
4-
try {
5-
// `who-to-greet` input defined in action metadata file
6-
const nameToGreet = core.getInput('who-to-greet');
7-
console.log(`Hello ${nameToGreet}!`);
8-
const time = (new Date()).toTimeString();
9-
core.setOutput("time", time);
10-
// Get the JSON webhook payload for the event that triggered the workflow
11-
const payload = JSON.stringify(github.context.payload, undefined, 2)
12-
console.log(`The event payload: ${payload}`);
13-
} catch (error) {
14-
core.setFailed(error.message);
1+
const fs = require("fs");
2+
const { globSync } = require("glob");
3+
const { markdownToBlocks } = require('@tryfabric/martian');
4+
const { Client } = require("@notionhq/client");
5+
const { nextTick } = require("process");
6+
const { root } = require("@tryfabric/martian/build/src/markdown");
7+
8+
// TODO: add sleep interval for all requests
9+
// TODO: fix tables width
10+
// TODO: NEXT: add content-only update (no pages re-creation)
11+
12+
['FOLDER', 'NOTION_TOKEN', 'NOTION_ROOT_ID'].forEach((varName) => {
13+
if (!process.env[varName]) {
14+
console.log(`${varName} not provided`)
15+
process.exit(1)
16+
}
17+
})
18+
19+
const notionUrlMatch = process.env.NOTION_ROOT_ID.match(/[^-]*$/)
20+
if (notionUrlMatch == null) {
21+
throw new SyntaxError('Provided page was not in a valid format, url must end with "-<page-id>"');
1522
}
23+
const notionPageId = notionUrlMatch[0]
24+
25+
const notion = new Client({
26+
auth: process.env.NOTION_TOKEN
27+
})
28+
29+
notion.pages.retrieve({ page_id: notionPageId }).then((rootPage) => {
30+
// console.log("Root page-> ", rootPage)
31+
32+
var files = globSync(`${process.env.FOLDER}/**/*.md`, { ignore: 'node_modules/**' })
33+
// console.log("Files to sync ->", files)
34+
35+
notion.blocks.children.list({block_id: notionPageId}).then((blocksResponse) => {
36+
const blockIdsToRemove = blocksResponse.results.map((e) => e.id)
37+
38+
// sequencially delete all page blocks
39+
var doDelete = function(idToDelete) {
40+
if (!idToDelete) return;
41+
42+
notion.blocks.delete({
43+
block_id: idToDelete
44+
}).then(function() {
45+
console.log("Block deleted:", idToDelete)
46+
if (idToDelete != blockIdsToRemove[blockIdsToRemove.length - 1]) {
47+
nextDeleteId = blockIdsToRemove[blockIdsToRemove.indexOf(idToDelete) + 1]
48+
doDelete(nextDeleteId)
49+
}
50+
})
51+
}
52+
53+
doDelete(blockIdsToRemove[0])
54+
console.log('Block deletion complete')
55+
56+
var doCreate = (filePath) => {
57+
const mdContent = fs.readFileSync(filePath, 'utf8')
58+
const newBlocks = markdownToBlocks(mdContent)
59+
60+
var title
61+
62+
try {
63+
title = newBlocks[0][newBlocks[0].type].rich_text[0].text.content
64+
} catch (error) {
65+
console.log("Cannot extract page title from", newBlocks[0])
66+
process.exit(1)
67+
}
68+
69+
notion.pages.create({
70+
parent: {
71+
type: "page_id",
72+
page_id: rootPage.id
73+
},
74+
properties: {
75+
"title": {
76+
"title": [{ "text": { "content": title} }], type: "title"
77+
}
78+
},
79+
}).then((pageResponse) => {
80+
console.log('Page created', pageResponse)
81+
82+
notion.blocks.children.append({ block_id: pageResponse.id, children: newBlocks }).then(() => {
83+
// process next page
84+
if (filePath != files[files.length - 1]) {
85+
doCreate(files[files.indexOf(filePath) + 1])
86+
}
87+
})
88+
89+
}).catch((error) => {
90+
console.log("Page creation failed", error)
91+
process.exit(1)
92+
})
93+
}
94+
95+
doCreate(files[0])
96+
})
97+
98+
}).catch((error) => {
99+
console.log("Root page not found", error.body)
100+
process.exit(1)
101+
})

0 commit comments

Comments
 (0)