Skip to content

Commit

Permalink
Merge pull request #1 from takoba/fix-url-parsing
Browse files Browse the repository at this point in the history
improve page_id parsing from url with query-string
  • Loading branch information
takoba committed Jan 30, 2022
2 parents f257971 + b0613c3 commit cdd26ab
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 25 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
},
"dependencies": {
"@notionhq/client": "^0.4.13",
"@slack/bolt": "^3.9.0",
"url-parse": "^1.5.4"
"@slack/bolt": "^3.9.0"
}
}
13 changes: 8 additions & 5 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Client } from '@notionhq/client'
import {App, ContextBlock, DividerBlock, KnownBlock, SectionBlock} from '@slack/bolt'
import URLParse from 'url-parse'
import { URL } from 'url'

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


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

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

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

const page = await notion.pages.retrieve({ page_id })
Expand Down
18 changes: 0 additions & 18 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1010,11 +1010,6 @@ qs@6.9.6:
resolved "https://registry.yarnpkg.com/qs/-/qs-6.9.6.tgz#26ed3c8243a431b2924aca84cc90471f35d5a0ee"
integrity sha512-TIRk4aqYLNoJUbd+g2lEdz5kLWIuTMRagAXxl78Q0RiVjAOugHmeKNGdd3cwo/ktpf9aL9epCfFqWDEKysUlLQ==

querystringify@^2.1.1:
version "2.2.0"
resolved "https://registry.yarnpkg.com/querystringify/-/querystringify-2.2.0.tgz#3345941b4153cb9d082d8eee4cda2016a9aef7f6"
integrity sha512-FIqgj2EUvTa7R50u0rGsyTftzjYmv/a3hO345bZNrqabNqjtgiDMgmo4mkUjd+nzU5oF3dClKqFIPUKybUyqoQ==

range-parser@~1.2.1:
version "1.2.1"
resolved "https://registry.yarnpkg.com/range-parser/-/range-parser-1.2.1.tgz#3cf37023d199e1c24d1a55b84800c2f3e6468031"
Expand All @@ -1030,11 +1025,6 @@ raw-body@2.4.2, raw-body@^2.3.3:
iconv-lite "0.4.24"
unpipe "1.0.0"

requires-port@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/requires-port/-/requires-port-1.0.0.tgz#925d2601d39ac485e091cf0da5c6e694dc3dcaff"
integrity sha1-kl0mAdOaxIXgkc8NpcbmlNw9yv8=

retry@^0.13.1:
version "0.13.1"
resolved "https://registry.yarnpkg.com/retry/-/retry-0.13.1.tgz#185b1587acf67919d63b357349e03537b2484658"
Expand Down Expand Up @@ -1185,14 +1175,6 @@ unpipe@1.0.0, unpipe@~1.0.0:
resolved "https://registry.yarnpkg.com/unpipe/-/unpipe-1.0.0.tgz#b2bf4ee8514aae6165b4817829d21b2ef49904ec"
integrity sha1-sr9O6FFKrmFltIF4KdIbLvSZBOw=

url-parse@^1.5.4:
version "1.5.4"
resolved "https://registry.yarnpkg.com/url-parse/-/url-parse-1.5.4.tgz#e4f645a7e2a0852cc8a66b14b292a3e9a11a97fd"
integrity sha512-ITeAByWWoqutFClc/lRZnFplgXgEZr3WJ6XngMM/N9DMIm4K8zXPCZ1Jdu0rERwO84w1WC5wkle2ubwTA4NTBg==
dependencies:
querystringify "^2.1.1"
requires-port "^1.0.0"

utils-merge@1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/utils-merge/-/utils-merge-1.0.1.tgz#9f95710f50a267947b2ccc124741c1028427e713"
Expand Down

0 comments on commit cdd26ab

Please sign in to comment.