-
-
Notifications
You must be signed in to change notification settings - Fork 593
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: updated notion-parser version for automatic image rendering
- Loading branch information
1 parent
8bc2235
commit 9d6b3fd
Showing
6 changed files
with
564 additions
and
784 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,34 +12,34 @@ | |
"javascript" | ||
], | ||
"scripts": { | ||
"watch": "webpack --watch", | ||
"develop": "webpack --watch", | ||
"build": "webpack", | ||
"build-prod": "webpack --env mode='production'", | ||
"feed": "node dist/index.js" | ||
}, | ||
"author": "Ravgeet Dhillon <[email protected]>", | ||
"license": "MIT", | ||
"dependencies": { | ||
"@instantish/martian": "^1.0.3", | ||
"@notionhq/client": "^0.4.3", | ||
"@notionhq/client": "^0.4.9", | ||
"@tryfabric/martian": "^1.1.0", | ||
"dotenv": "^10.0.0", | ||
"rss-parser": "^3.12.0", | ||
"turndown": "^7.1.1" | ||
}, | ||
"devDependencies": { | ||
"@babel/core": "^7.15.8", | ||
"@babel/preset-env": "^7.15.8", | ||
"babel-loader": "^8.2.2", | ||
"@babel/core": "^7.16.0", | ||
"@babel/preset-env": "^7.16.4", | ||
"babel-loader": "^8.2.3", | ||
"eslint": "^7.32.0", | ||
"eslint-config-airbnb": "^18.2.1", | ||
"eslint-config-airbnb-base": "^14.2.1", | ||
"eslint-config-prettier": "^8.3.0", | ||
"eslint-plugin-import": "^2.25.2", | ||
"eslint-plugin-import": "^2.25.3", | ||
"eslint-plugin-prettier": "^4.0.0", | ||
"eslint-webpack-plugin": "^3.0.1", | ||
"prettier": "^2.4.1", | ||
"webpack": "^5.58.2", | ||
"webpack-cli": "^4.9.0" | ||
"eslint-webpack-plugin": "^3.1.1", | ||
"prettier": "^2.5.1", | ||
"webpack": "^5.64.4", | ||
"webpack-cli": "^4.9.1" | ||
}, | ||
"engines": { | ||
"node": ">=10.16.0 <=14.x.x", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,24 @@ | ||
import getNewFeedItems from './feed'; | ||
import { addFeedItemToNotion, deleteOldUnreadItemsFromNotion } from './notion'; | ||
import { | ||
addFeedItemToNotion, | ||
deleteOldUnreadFeedItemsFromNotion, | ||
} from './notion'; | ||
import htmlToNotionBlocks from './parser'; | ||
|
||
async function index() { | ||
const feedItems = await getNewFeedItems(); | ||
|
||
for (let i = 0; i < feedItems.length; i++) { | ||
const item = feedItems[i]; | ||
|
||
const notionItem = { | ||
title: item.title, | ||
link: item.link, | ||
content: htmlToNotionBlocks(item.content), | ||
}; | ||
|
||
await addFeedItemToNotion(notionItem); | ||
} | ||
|
||
await deleteOldUnreadItemsFromNotion(); | ||
await deleteOldUnreadFeedItemsFromNotion(); | ||
} | ||
|
||
index(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,16 @@ | ||
import { markdownToBlocks } from '@instantish/martian'; | ||
import { markdownToBlocks } from '@tryfabric/martian'; | ||
import TurndownService from 'turndown'; | ||
|
||
function htmlToMarkdown(htmlContent) { | ||
function htmlToMarkdownJSON(htmlContent) { | ||
const turndownService = new TurndownService(); | ||
return turndownService.turndown(htmlContent); | ||
} | ||
|
||
function jsonToNotionBlocks(markdownContent) { | ||
const notionBlocks = markdownToBlocks(markdownContent); | ||
return notionBlocks; | ||
return markdownToBlocks(markdownContent); | ||
} | ||
|
||
export default function htmlToNotionBlocks(htmlContent) { | ||
const imageUrlRegex = | ||
/(http(s?):)([/|.|\w|\s|-])*\.(?:jpg|jpeg|webp|gif|png)/gi; | ||
|
||
const markdownContent = htmlToMarkdown(htmlContent); | ||
const notionBlocks = jsonToNotionBlocks(markdownContent); | ||
const notionBlocksWithImages = notionBlocks.map((block) => { | ||
if (block.type === 'paragraph') { | ||
if (imageUrlRegex.test(block.paragraph.text[0].text.content)) { | ||
return { | ||
type: 'image', | ||
image: { | ||
type: 'external', | ||
external: { | ||
url: block.paragraph.text[0].text.content, | ||
}, | ||
}, | ||
}; | ||
} | ||
} | ||
return block; | ||
}); | ||
return notionBlocksWithImages; | ||
const markdownJson = htmlToMarkdownJSON(htmlContent); | ||
return jsonToNotionBlocks(markdownJson); | ||
} |