irongit

A Neovim plugin enabling you to solve LeetCode problems.

docs(readme): hooks

kawreauthored
parent 7f0a354commit 8eafd39567286d666260ef228dfb25b6120525c8Browse files

5 files changed, +38 -17

+18-2README.md
@@ -123,10 +123,12 @@ To see full configuration types see [template.lua](./lua/leetcode/config/templat
123123 width = "40%", ---@type string | integer
124124 },
125125
126- ---@type table<lc.hook, function[]>
127126 hooks = {
127+ ---@type fun()[]
128128 LeetEnter = {},
129- LeetQuestionEnter = {},
129+
130+ ---@type fun(question: { lang: string })[]
131+ LeetQuestionNew = {},
130132 },
131133 }
132134 ```
@@ -183,6 +185,20 @@ Whether to log [leetcode.nvim] status notifications
183185 logging = true
184186 ```
185187
188+### hooks
189+
190+List of functions that get executed on specified event
191+
192+```lua
193+hooks = {
194+ ---@type fun()[]
195+ LeetEnter = {},
196+
197+ ---@type fun(question: { lang: string })[]
198+ LeetQuestionNew = {},
199+},
200+```
201+
186202 ## 📋 Commands
187203
188204 ### `Leet` opens menu dashboard
+1-3lua/leetcode/api/headers.lua
@@ -9,9 +9,7 @@ headers.get = function()
99 ["Referer"] = "https://leetcode.com/",
1010 ["Origin"] = "https://leetcode.com",
1111 ["Host"] = "leetcode.com",
12- ["Cookie"] = c
13- and ("LEETCODE_SESSION=%s;csrftoken=%s"):format(c.leetcode_session, c.csrftoken)
14- or "",
12+ ["Cookie"] = cookie.to_str(),
1513 ["Content-Type"] = "application/json",
1614 ["Accept"] = "application/json",
1715 ["x-csrftoken"] = c and c.csrftoken or nil,
+13-8lua/leetcode/cache/cookie.lua
@@ -19,9 +19,9 @@ function cookie.update(str)
1919 file:write(str, "w")
2020
2121 local auth_api = require("leetcode.api.auth")
22- local a_ok, err = pcall(auth_api.user)
22+ local _, err = auth_api.user()
2323
24- if not a_ok then
24+ if err then
2525 cookie.delete()
2626 error(err)
2727 end
@@ -47,15 +47,20 @@ end
4747 function cookie.parse(cookie_str)
4848 local c = {}
4949
50- local csrf_ok, csrf = pcall(string.match, cookie_str, "csrftoken=[^;]+")
51- assert(csrf_ok and csrf, "Bad csrf token format")
52- c.csrftoken = csrf:sub(11)
50+ local csrf_ok, csrf = pcall(string.match, cookie_str, "csrftoken=(.-);")
51+ assert(csrf_ok and csrf and csrf ~= "", "Bad csrf token format")
52+ c.csrftoken = csrf
5353
54- local ls_ok, ls = pcall(string.match, cookie_str, "LEETCODE_SESSION=[^;]+")
55- assert(ls_ok and ls, "Bad leetcode session token format")
56- c.leetcode_session = ls:sub(18)
54+ local ls_ok, ls = pcall(string.match, cookie_str, "LEETCODE_SESSION=(.-);")
55+ assert(ls_ok and ls and ls ~= "", "Bad leetcode session token format")
56+ c.leetcode_session = ls
5757
5858 return c
5959 end
6060
61+function cookie.to_str()
62+ local c = cookie.get()
63+ return c and ("LEETCODE_SESSION=%s;csrftoken=%s;"):format(c.leetcode_session, c.csrftoken) or ""
64+end
65+
6166 return cookie
+5-3lua/leetcode/config/template.lua
@@ -37,7 +37,7 @@
3737
3838 ---@alias lc.hook
3939 ---| "LeetEnter"
40----| "LeetQuestionEnter"
40+---| "LeetQuestionNew"
4141
4242 ---@class lc.UserConfig
4343 local M = {
@@ -86,10 +86,12 @@ local M = {
8686 width = "40%", ---@type string | integer
8787 },
8888
89- ---@type table<lc.hook, function[]>
9089 hooks = {
90+ ---@type fun()[]
9191 LeetEnter = {},
92- LeetQuestionEnter = {},
92+
93+ ---@type fun(question: { lang: string })[]
94+ LeetQuestionNew = {},
9395 },
9496 }
9597
+1-1lua/leetcode/ui/question.lua
@@ -39,7 +39,7 @@ function Question:mount()
3939 vim.api.nvim_set_current_dir(config.home:absolute())
4040 vim.cmd("$tabe " .. self.file:absolute())
4141
42- utils.exec_hooks("LeetQuestionEnter", {
42+ utils.exec_hooks("LeetQuestionNew", {
4343 lang = self.lang,
4444 })
4545