Skip to content

Commit 50ac3fb

Browse files
author
Sergei Orlov
committed
✨ Add support for annotations in the page title (color, code, underline, etc.)
1 parent cb42f88 commit 50ac3fb

File tree

4 files changed

+80
-28
lines changed

4 files changed

+80
-28
lines changed

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
},
3434
"readme": "https://github.com/orlowdev/gatsby-source-notion-api#readme",
3535
"dependencies": {
36-
"node-fetch": "^2.6.1"
36+
"node-fetch": "^2.6.1",
37+
"or-pipets": "^1.0.1"
3738
},
3839
"devDependencies": {
3940
"ava": "^3.15.0",

src/block-to-string.js

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
}, "")

src/transformers/get-page-title.js

+3-27
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,9 @@
1+
const { blockToString } = require("../block-to-string")
2+
13
exports.getNotionPageTitle = (page) => {
24
const titleProperty = Object.keys(page.properties).find(
35
(key) => page.properties[key].type == "title",
46
)
57

6-
return page.properties[titleProperty].title
7-
.reduce((acc, chunk) => {
8-
if (chunk.type == "text") {
9-
return acc.concat(chunk.plain_text)
10-
}
11-
12-
if (chunk.type == "mention") {
13-
if (chunk.mention.type == "user") {
14-
return acc.concat(chunk.plain_text)
15-
}
16-
17-
if (chunk.mention.type == "date") {
18-
if (chunk.mention.date.end) {
19-
return acc.concat(`${chunk.mention.date.start}${chunk.mention.date.start}`)
20-
}
21-
22-
return acc.concat(chunk.mention.date.start)
23-
}
24-
25-
if (chunk.mention.type == "page") {
26-
return acc.concat(chunk.plain_text)
27-
}
28-
}
29-
30-
return acc
31-
}, "")
32-
.trim()
8+
return blockToString(page.properties[titleProperty].title)
339
}

yarn.lock

+5
Original file line numberDiff line numberDiff line change
@@ -2259,6 +2259,11 @@ optionator@^0.8.1:
22592259
type-check "~0.3.2"
22602260
word-wrap "~1.2.3"
22612261

2262+
or-pipets@^1.0.1:
2263+
version "1.0.1"
2264+
resolved "https://registry.yarnpkg.com/or-pipets/-/or-pipets-1.0.1.tgz#0699c99f95a535614a585cd7fb3a1a216a47b5c0"
2265+
integrity sha512-xgIIdB4w00pbhtNPYLF/caifwbc4KrE7jy4WqiDRzRrC2g3YVR4ypRwbKRA/bXzftQEA2SK8c+wKI2R7kqKiLQ==
2266+
22622267
ora@^5.0.0, ora@^5.2.0:
22632268
version "5.4.0"
22642269
resolved "https://registry.yarnpkg.com/ora/-/ora-5.4.0.tgz#42eda4855835b9cd14d33864c97a3c95a3f56bf4"

0 commit comments

Comments
 (0)