Skip to content

Instantly share code, notes, and snippets.

@biomathcode
Last active December 23, 2021 14:43
Show Gist options
  • Save biomathcode/d2a5a268938c8fb78b722afdb53460ed to your computer and use it in GitHub Desktop.
Save biomathcode/d2a5a268938c8fb78b722afdb53460ed to your computer and use it in GitHub Desktop.
Publishing articles from notion to dev community
const { Client } = require("@notionhq/client");
const axios = require('axios')
const notion2md = require("notion-to-md");
const databaseId = 'YOUR_DATABASE_ID';
const notion = new Client({
auth: "YOUR_NOTION_TOKEN",
});
const apiUrl = "https://dev.to/api"
const apiToken = "DEV_ACCESS_TOKEN"
const n2m = new notion2md({ notionClient: notion });
const postToDev = async (
apiUrl,
apiToken,
title,
bodyMarkdown,
tags
) => {
const post = await axios({
url: apiUrl + '/articles',
headers: {
'api-key': apiToken,
},
method: 'POST',
data: {
article: {
title: title,
published: true,
body_markdown: bodyMarkdown,
tags: tags,
},
},
});
return post
};
const UpdateToDev = async (
id,
apiUrl,
apiToken,
title,
bodyMarkdown,
tags,
) => {
const update = await axios({
url : apiUrl+ "/articles/" + id,
headers: {
'api-key': apiToken,
},
method: 'PUT',
data: {
article: {
title: title,
body_markdown: bodyMarkdown,
tags: tags,
},
}
})
return update;
}
const getMarkdown = async(pageId) => {
const mdblocks = await n2m.pageToMarkdown(pageId);
const mdString = await n2m.toMarkdownString(mdblocks);
return mdString;
}
async function getDatabase () {
//getting the id of the pages that we want to publish
const response = await notion.databases.query({
database_id: databaseId,
filter: {
property: 'PublishStatus',
'select': {
equals: "toPublish"
}
}
});
if(response.results.length === 0) {
console.log('No article to publish')
console.log(response.results);
} else {
response.results.map((result) => {
//returns an array of tag names
const tags = result.properties.Tags.multi_select.map(tag => tag.name)
const title = result.properties.Pages.title[0].plain_text;
getMarkdown(result.id)
.then(function(res) {
postToDev(apiUrl, apiToken, title, res, tags)
.then(function(post) {
if(post.status=== 200 || post.status === 201) {
notion.pages.update({
page_id: result.id,
properties: {
'PublishStatus': {
select: {
name: 'Published'
}
},
'DevUrl': {
url: post.data.url
},
'PostId': {
number: Number(post.data.id)
}
}
}).then(updated => console.log(updated))
}
})
});
})
}
const responseUpdate = await notion.databases.query({
database_id: databaseId,
filter: {
property: 'PublishStatus',
'select': {
equals: "toUpdate"
}
}
});
if(responseUpdate.results.length !== 0) {
responseUpdate.results.map((result) => {
const postId = result.properties.PostId.number
const newTags = result.properties.Tags.multi_select.map(tag => tag.name)
const newTitle = result.properties.Pages.title[0].plain_text;
getMarkdown(result.id)
.then(function (res) {
UpdateToDev(postId, apiUrl, apiToken, newTitle, res, newTags)
.then(function(update) {
if(update.status === 200 || update.status === 201) {
notion.pages.update({
page_id: result.id,
properties: {
'PublishStatus': {
select: {
name: 'Published'
}
},
'DevUrl': {
url: update.data.url
},
'PostId': {
number: Number(update.data.id)
}
}
}).then((updated) => console.log(updated))
}
else {
console.log('post not found')
}
})
})
})
}
}
getDatabase();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment