A Neovim plugin enabling you to solve LeetCode problems.
docs(readme): hooks
5 files changed, +38 -17
+18-2README.md
| @@ -123,10 +123,12 @@ To see full configuration types see [template.lua](./lua/leetcode/config/templat | ||
| 123 | 123 | width = "40%", ---@type string | integer |
| 124 | 124 | }, |
| 125 | 125 | |
| 126 | - ---@type table<lc.hook, function[]> | |
| 127 | 126 | hooks = { |
| 127 | + ---@type fun()[] | |
| 128 | 128 | LeetEnter = {}, |
| 129 | - LeetQuestionEnter = {}, | |
| 129 | + | |
| 130 | + ---@type fun(question: { lang: string })[] | |
| 131 | + LeetQuestionNew = {}, | |
| 130 | 132 | }, |
| 131 | 133 | } |
| 132 | 134 | ``` |
| @@ -183,6 +185,20 @@ Whether to log [leetcode.nvim] status notifications | ||
| 183 | 185 | logging = true |
| 184 | 186 | ``` |
| 185 | 187 | |
| 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 | + | |
| 186 | 202 | ## 📋 Commands |
| 187 | 203 | |
| 188 | 204 | ### `Leet` opens menu dashboard |
+1-3lua/leetcode/api/headers.lua
| @@ -9,9 +9,7 @@ headers.get = function() | ||
| 9 | 9 | ["Referer"] = "https://leetcode.com/", |
| 10 | 10 | ["Origin"] = "https://leetcode.com", |
| 11 | 11 | ["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(), | |
| 15 | 13 | ["Content-Type"] = "application/json", |
| 16 | 14 | ["Accept"] = "application/json", |
| 17 | 15 | ["x-csrftoken"] = c and c.csrftoken or nil, |
+13-8lua/leetcode/cache/cookie.lua
| @@ -19,9 +19,9 @@ function cookie.update(str) | ||
| 19 | 19 | file:write(str, "w") |
| 20 | 20 | |
| 21 | 21 | local auth_api = require("leetcode.api.auth") |
| 22 | - local a_ok, err = pcall(auth_api.user) | |
| 22 | + local _, err = auth_api.user() | |
| 23 | 23 | |
| 24 | - if not a_ok then | |
| 24 | + if err then | |
| 25 | 25 | cookie.delete() |
| 26 | 26 | error(err) |
| 27 | 27 | end |
| @@ -47,15 +47,20 @@ end | ||
| 47 | 47 | function cookie.parse(cookie_str) |
| 48 | 48 | local c = {} |
| 49 | 49 | |
| 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 | |
| 53 | 53 | |
| 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 | |
| 57 | 57 | |
| 58 | 58 | return c |
| 59 | 59 | end |
| 60 | 60 | |
| 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 | + | |
| 61 | 66 | return cookie |
+5-3lua/leetcode/config/template.lua
| @@ -37,7 +37,7 @@ | ||
| 37 | 37 | |
| 38 | 38 | ---@alias lc.hook |
| 39 | 39 | ---| "LeetEnter" |
| 40 | ----| "LeetQuestionEnter" | |
| 40 | +---| "LeetQuestionNew" | |
| 41 | 41 | |
| 42 | 42 | ---@class lc.UserConfig |
| 43 | 43 | local M = { |
| @@ -86,10 +86,12 @@ local M = { | ||
| 86 | 86 | width = "40%", ---@type string | integer |
| 87 | 87 | }, |
| 88 | 88 | |
| 89 | - ---@type table<lc.hook, function[]> | |
| 90 | 89 | hooks = { |
| 90 | + ---@type fun()[] | |
| 91 | 91 | LeetEnter = {}, |
| 92 | - LeetQuestionEnter = {}, | |
| 92 | + | |
| 93 | + ---@type fun(question: { lang: string })[] | |
| 94 | + LeetQuestionNew = {}, | |
| 93 | 95 | }, |
| 94 | 96 | } |
| 95 | 97 |
+1-1lua/leetcode/ui/question.lua
| @@ -39,7 +39,7 @@ function Question:mount() | ||
| 39 | 39 | vim.api.nvim_set_current_dir(config.home:absolute()) |
| 40 | 40 | vim.cmd("$tabe " .. self.file:absolute()) |
| 41 | 41 | |
| 42 | - utils.exec_hooks("LeetQuestionEnter", { | |
| 42 | + utils.exec_hooks("LeetQuestionNew", { | |
| 43 | 43 | lang = self.lang, |
| 44 | 44 | }) |
| 45 | 45 |