Skip to content

Commit

Permalink
feat: updated notion-parser version for automatic image rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
ravgeetdhillon committed Dec 6, 2021
1 parent 8bc2235 commit 9d6b3fd
Show file tree
Hide file tree
Showing 6 changed files with 564 additions and 784 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ module.exports = {
'no-unused-vars': ['off'],
'no-plusplus': ['off'],
'no-await-in-loop': ['off'],
'no-console': 'off',
},
};
1,284 changes: 541 additions & 743 deletions package-lock.json

Large diffs are not rendered by default.

22 changes: 11 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions src/feed.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export default async function getNewFeedItems() {

for (let i = 0; i < feeds.length; i++) {
const { feedUrl } = feeds[i];
console.log(`Fetching feed items from ${feedUrl}`);
const feedItems = await getNewFeedItemsFrom(feedUrl);
allNewFeedItems = [...allNewFeedItems, ...feedItems];
}
Expand Down
9 changes: 5 additions & 4 deletions src/index.js
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();
31 changes: 5 additions & 26 deletions src/parser.js
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);
}

0 comments on commit 9d6b3fd

Please sign in to comment.