Skip to content

Commit cdd26ab

Browse files
authored
Merge pull request #1 from takoba/fix-url-parsing
improve page_id parsing from url with query-string
2 parents f257971 + b0613c3 commit cdd26ab

File tree

3 files changed

+9
-25
lines changed

3 files changed

+9
-25
lines changed

package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
},
2424
"dependencies": {
2525
"@notionhq/client": "^0.4.13",
26-
"@slack/bolt": "^3.9.0",
27-
"url-parse": "^1.5.4"
26+
"@slack/bolt": "^3.9.0"
2827
}
2928
}

src/app.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Client } from '@notionhq/client'
22
import {App, ContextBlock, DividerBlock, KnownBlock, SectionBlock} from '@slack/bolt'
3-
import URLParse from 'url-parse'
3+
import { URL } from 'url'
44

55
const app = new App({
66
token: process.env.SLACK_BOT_TOKEN,
@@ -12,14 +12,17 @@ const notion = new Client({
1212
})
1313

1414

15-
app.message(/(https?:\/\/([A-z0-9\-]*\.)?notion\.so\/[A-z0-9\-_#\/]+)/, async ({ message, context, say}) => {
15+
app.message(/(https?:\/\/(www\.)?notion\.so\/[A-z0-9\-_]+\/[A-z0-9\-_#?=&;]+)/, async ({ message, context, say}) => {
1616
console.debug("DEBUG: dump `message`", message)
1717
console.debug("DEBUG: dump `context`", context)
1818

19-
const url = context.matches[0]
19+
const url = context.matches[0].replace(/&/, '&')
2020

21-
const parsedUrl = new URLParse(url)
22-
const page_id = parsedUrl.pathname.replace(/^\/[0-9A-z]+\//, '').split('-').splice(-1)[0]
21+
const parsedUrl = new URL(url)
22+
const parsePageIdFromPathname = (pathname: string) => pathname.replace(/^\/[0-9A-z\-_]+\//, '').split('-').splice(-1)[0]
23+
const page_id: string = parsedUrl.searchParams.get('p') !== null
24+
? parsedUrl.searchParams.get('p') ?? parsePageIdFromPathname(parsedUrl.pathname)
25+
: parsePageIdFromPathname(parsedUrl.pathname)
2326
const block_id = parsedUrl.hash.slice(1)
2427

2528
const page = await notion.pages.retrieve({ page_id })

yarn.lock

-18
Original file line numberDiff line numberDiff line change
@@ -1010,11 +1010,6 @@ qs@6.9.6:
10101010
resolved "https://registry.yarnpkg.com/qs/-/qs-6.9.6.tgz#26ed3c8243a431b2924aca84cc90471f35d5a0ee"
10111011
integrity sha512-TIRk4aqYLNoJUbd+g2lEdz5kLWIuTMRagAXxl78Q0RiVjAOugHmeKNGdd3cwo/ktpf9aL9epCfFqWDEKysUlLQ==
10121012

1013-
querystringify@^2.1.1:
1014-
version "2.2.0"
1015-
resolved "https://registry.yarnpkg.com/querystringify/-/querystringify-2.2.0.tgz#3345941b4153cb9d082d8eee4cda2016a9aef7f6"
1016-
integrity sha512-FIqgj2EUvTa7R50u0rGsyTftzjYmv/a3hO345bZNrqabNqjtgiDMgmo4mkUjd+nzU5oF3dClKqFIPUKybUyqoQ==
1017-
10181013
range-parser@~1.2.1:
10191014
version "1.2.1"
10201015
resolved "https://registry.yarnpkg.com/range-parser/-/range-parser-1.2.1.tgz#3cf37023d199e1c24d1a55b84800c2f3e6468031"
@@ -1030,11 +1025,6 @@ raw-body@2.4.2, raw-body@^2.3.3:
10301025
iconv-lite "0.4.24"
10311026
unpipe "1.0.0"
10321027

1033-
requires-port@^1.0.0:
1034-
version "1.0.0"
1035-
resolved "https://registry.yarnpkg.com/requires-port/-/requires-port-1.0.0.tgz#925d2601d39ac485e091cf0da5c6e694dc3dcaff"
1036-
integrity sha1-kl0mAdOaxIXgkc8NpcbmlNw9yv8=
1037-
10381028
retry@^0.13.1:
10391029
version "0.13.1"
10401030
resolved "https://registry.yarnpkg.com/retry/-/retry-0.13.1.tgz#185b1587acf67919d63b357349e03537b2484658"
@@ -1185,14 +1175,6 @@ unpipe@1.0.0, unpipe@~1.0.0:
11851175
resolved "https://registry.yarnpkg.com/unpipe/-/unpipe-1.0.0.tgz#b2bf4ee8514aae6165b4817829d21b2ef49904ec"
11861176
integrity sha1-sr9O6FFKrmFltIF4KdIbLvSZBOw=
11871177

1188-
url-parse@^1.5.4:
1189-
version "1.5.4"
1190-
resolved "https://registry.yarnpkg.com/url-parse/-/url-parse-1.5.4.tgz#e4f645a7e2a0852cc8a66b14b292a3e9a11a97fd"
1191-
integrity sha512-ITeAByWWoqutFClc/lRZnFplgXgEZr3WJ6XngMM/N9DMIm4K8zXPCZ1Jdu0rERwO84w1WC5wkle2ubwTA4NTBg==
1192-
dependencies:
1193-
querystringify "^2.1.1"
1194-
requires-port "^1.0.0"
1195-
11961178
utils-merge@1.0.1:
11971179
version "1.0.1"
11981180
resolved "https://registry.yarnpkg.com/utils-merge/-/utils-merge-1.0.1.tgz#9f95710f50a267947b2ccc124741c1028427e713"

0 commit comments

Comments
 (0)