Skip to content

Commit c02e6a9

Browse files
committed
getting somewhere (ratelimited)
1 parent 20f9efa commit c02e6a9

File tree

1 file changed

+43
-5
lines changed

1 file changed

+43
-5
lines changed

bin/shelf.js

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,20 +72,58 @@ const cleanupDataFields = async (notionResponse) => {
7272
}
7373
if (review && review.rich_text.length > 0) {
7474
// Necessary to stitch rich_text components together like this to preserve any hyperlinks
75-
cleanBook.review = review.rich_text.reduce((finalText, current) => {
76-
if (current.type !== "text") return finalText;
77-
if (current.href === null) return (finalText += current.plain_text);
78-
return (finalText += `<a href=\"${current.href}\">${current.plain_text}</a>`);
79-
}, "");
75+
cleanBook.review = reduceRichTextToHTMLString(review.rich_text);
8076
}
8177
if (link && link.url !== null) cleanBook.link = link.url;
8278

79+
/**
80+
* Setting up some more robust review infrastructure here alongside the existing one.
81+
*
82+
* Here's the loose plan:
83+
* 1. If there is no review, but there are blocks, then parse and squish all the blocks together as the review
84+
* 2. Once this is working properly, we can shift the review data from a property to entirely this.
85+
*
86+
* Note that final state is not necessarily easiest implementation for this code, but for my personal workflow.
87+
*/
88+
// if (!cleanBook.review) {
89+
const pageBlocks = await notion.blocks.children.list({
90+
block_id: book.id,
91+
page_size: 100,
92+
});
93+
if (pageBlocks.results.length > 0) {
94+
const htmlReview = reduceBlocksToSingleHTMLString(pageBlocks.results);
95+
if (htmlReview !== "") cleanBook.review = htmlReview;
96+
}
97+
// }
98+
8399
return cleanBook;
84100
});
85101

86102
return Promise.all(cleanBooks);
87103
};
88104

105+
const reduceRichTextToHTMLString = (richTextArray) => {
106+
// Function currently drops any text styling (since I don't plan to use that),
107+
// but wouldn't be too hard to add in future.
108+
return richTextArray.reduce((finalText, current) => {
109+
if (current.type !== "text") return finalText;
110+
if (current.href === null) return (finalText += current.plain_text);
111+
return (finalText += `<a href=\"${current.href}\">${current.plain_text}</a>`);
112+
}, "");
113+
};
114+
115+
const reduceBlocksToSingleHTMLString = (blockList) => {
116+
console.dir(blockList, { depth: null });
117+
return blockList.reduce((finalText, currentBlock) => {
118+
// Only interested in text stuff
119+
if (currentBlock.type !== "paragraph" || !current.paragraph.rich_text) {
120+
return finalText;
121+
}
122+
123+
return (finalText += `<p>${reduceRichTextToHTMLString(currentBlock.paragraph.rich_text)}</p>`);
124+
}, "");
125+
};
126+
89127
const possiblySaveNewSituImage = async (title, finished, situ) => {
90128
if (!finished || finished.date === null || !title.title) {
91129
return;

0 commit comments

Comments
 (0)