|
| 1 | +const { pipeExtend } = require("or-pipets") |
| 2 | + |
| 3 | +const pick = (key) => (obj) => obj[key] |
| 4 | + |
| 5 | +const ifTrue = (predicate, transformer, orElse) => (data) => |
| 6 | + predicate(data) ? transformer(data) : orElse(data) |
| 7 | + |
| 8 | +const id = (x) => x |
| 9 | + |
| 10 | +const annotateBold = ifTrue(pick("bold"), ({ content }) => ({ content: `**${content}**` }), id) |
| 11 | +const annotateItalic = ifTrue(pick("italic"), ({ content }) => ({ content: `_${content}_` }), id) |
| 12 | +const annotateCode = ifTrue(pick("code"), ({ content }) => ({ content: `\`${content}\`` }), id) |
| 13 | +const annotateStrikethrough = ifTrue( |
| 14 | + pick("strikethrough"), |
| 15 | + ({ content }) => ({ content: `~~${content}~~` }), |
| 16 | + id, |
| 17 | +) |
| 18 | +const annotateUnderline = ifTrue( |
| 19 | + pick("underline"), |
| 20 | + ({ content }) => ({ content: `<u>${content}</u>` }), |
| 21 | + id, |
| 22 | +) |
| 23 | +const annotateColor = ifTrue( |
| 24 | + ({ color }) => color != "default", |
| 25 | + ({ content, color }) => ({ content: `<span notion-color="${color}">${content}</span>` }), |
| 26 | + id, |
| 27 | +) |
| 28 | +const annotateLink = ifTrue( |
| 29 | + pick("link"), |
| 30 | + ({ content, link }) => ({ content: `[${content}](${link.url ? link.url : link})` }), |
| 31 | + id, |
| 32 | +) |
| 33 | + |
| 34 | +const stylize = pipeExtend(annotateBold) |
| 35 | + .pipeExtend(annotateItalic) |
| 36 | + .pipeExtend(annotateCode) |
| 37 | + .pipeExtend(annotateStrikethrough) |
| 38 | + .pipeExtend(annotateUnderline) |
| 39 | + .pipeExtend(annotateColor) |
| 40 | + .pipeExtend(annotateLink) |
| 41 | + |
| 42 | +exports.blockToString = (textBlocks) => |
| 43 | + textBlocks.reduce((text, textBlock) => { |
| 44 | + const data = { |
| 45 | + ...textBlock.text, |
| 46 | + ...textBlock.annotations, |
| 47 | + } |
| 48 | + |
| 49 | + if (textBlock.type == "mention") { |
| 50 | + if (textBlock.mention.type == "user") { |
| 51 | + data.content = textBlock.plain_text |
| 52 | + } |
| 53 | + |
| 54 | + if (textBlock.mention.type == "date") { |
| 55 | + if (textBlock.mention.date.end) { |
| 56 | + data.content = `${textBlock.mention.date.start} → ${textBlock.mention.date.start}` |
| 57 | + } else { |
| 58 | + data.content = textBlock.mention.date.start |
| 59 | + } |
| 60 | + |
| 61 | + data.content = `<time datetime="${data.content}">${data.content}</time>` |
| 62 | + } |
| 63 | + |
| 64 | + if (textBlock.mention.type == "page") { |
| 65 | + data.content = textBlock.plain_text |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + return text.concat(stylize.process(data).content) |
| 70 | + }, "") |
0 commit comments