Skip to content

Commit 3706dae

Browse files
committed
refactor: remove typescript and stick with only lua
1 parent 4cc1bc1 commit 3706dae

39 files changed

+741
-10998
lines changed

lua/forem-nvim/api.lua

+116-93
Original file line numberDiff line numberDiff line change
@@ -1,111 +1,134 @@
1-
--[[ Generated with https://github.com/TypeScriptToLua/TypeScriptToLua ]]
2-
local ____exports = {}
1+
local M = {}
32
local curl = require("plenary.curl")
43
local job = require("plenary.job")
54
local notify = require("forem-nvim.notify")
65
local article = require("forem-nvim.article")
7-
____exports.key = function() return vim.env.FOREM_API_KEY end
8-
local baseUrl = "https://dev.to/api"
9-
local function handleAsyncError(response)
10-
notify.error("Error: " .. tostring(response.body.error))
6+
7+
function M.key()
8+
return vim.env.FOREM_API_KEY
119
end
12-
____exports.handleError = function(response, onSuccess)
13-
local startStatus = string.sub(response.status, 1, 2)
14-
local ____temp_0
15-
if startStatus == "20" then
16-
____temp_0 = onSuccess(response.body)
17-
else
18-
____temp_0 = notify.error("Error: " .. tostring(response.body.error))
19-
end
20-
return ____temp_0
10+
11+
local BASE_URL = "https://dev.to/api"
12+
13+
local function handle_async_error(response)
14+
notify.error("Error: " .. tostring(response.body.error))
15+
end
16+
17+
function M.handle_error(response, on_success)
18+
local start_status = string.sub(response.status, 1, 2)
19+
20+
if start_status == "20" then
21+
on_success(response.body)
22+
else
23+
notify.error("Error: " .. tostring(response.body.error))
24+
end
2125
end
22-
local function request(requestFunction, endpoint, options)
23-
local parameters = vim.tbl_extend(
24-
"force",
25-
{
26-
url = baseUrl .. endpoint,
27-
headers = {
28-
["api-key"] = ____exports.key(),
29-
content_type = "application/json",
30-
accept = "application/vnd.forem.api-v1+json"
31-
}
32-
},
33-
options
26+
27+
local function request(request_function, endpoint, options)
28+
local parameters = vim.tbl_extend(
29+
"force",
30+
{
31+
url = BASE_URL .. endpoint,
32+
headers = {
33+
["api-key"] = M.key(),
34+
content_type = "application/json",
35+
accept = "application/vnd.forem.api-v1+json"
36+
}
37+
},
38+
options
39+
)
40+
local response = request_function(parameters)
41+
if response.body then
42+
return vim.tbl_extend(
43+
"force",
44+
response,
45+
{ body = vim.fn.json_decode(response.body) }
3446
)
35-
local response = requestFunction(parameters)
36-
if response.body then
37-
return vim.tbl_extend(
38-
"force",
39-
response,
40-
{body = vim.fn.json_decode(response.body)}
41-
)
42-
end
43-
return response
47+
end
48+
return response
4449
end
45-
local function requestAsync(method, endpoint, options, onSuccess, onError)
46-
return job:new({
47-
command = "curl",
48-
args = {
49-
"-X",
50-
method,
51-
"-H",
52-
"Content-Type: application/json",
53-
"-H",
54-
"Accept: application/vnd.forem.api-v1+json",
55-
"-H",
56-
"api-key: " .. tostring(____exports.key()),
57-
"-d",
58-
vim.fn.json_encode(options),
59-
baseUrl .. endpoint
60-
},
61-
on_exit = function(job, code)
62-
vim.schedule(function()
63-
local result = table.concat(
64-
job:result(),
65-
"\n"
66-
)
67-
local response = vim.fn.json_decode(result)
68-
if code == 0 then
69-
onSuccess(response)
70-
return
71-
end
72-
handleAsyncError(response)
73-
if onError then
74-
onError(response)
75-
end
76-
end)
50+
51+
local function request_async(method, endpoint, options, on_success, on_error)
52+
-- TODO: Check if this could be done with `vim.system`
53+
return job:new({
54+
command = "curl",
55+
args = {
56+
"-X",
57+
method,
58+
"-H",
59+
"Content-Type: application/json",
60+
"-H",
61+
"Accept: application/vnd.forem.api-v1+json",
62+
"-H",
63+
"api-key: " .. tostring(M.key()),
64+
"-d",
65+
vim.fn.json_encode(options),
66+
BASE_URL .. endpoint
67+
},
68+
on_exit = function(this_job, code)
69+
vim.schedule(function()
70+
local result = table.concat(
71+
this_job:result(),
72+
"\n"
73+
)
74+
local response = vim.fn.json_decode(result)
75+
if code == 0 then
76+
on_success(response)
77+
return
7778
end
78-
}):start()
79+
handle_async_error(response)
80+
if on_error then
81+
on_error(response)
82+
end
83+
end)
84+
end
85+
}):start()
7986
end
80-
local function get(endpoint, onSuccess, onError)
81-
return requestAsync(
82-
"GET",
83-
endpoint,
84-
{},
85-
onSuccess,
86-
onError
87-
)
87+
88+
local function get(endpoint, on_success, on_error)
89+
return request_async(
90+
"GET",
91+
endpoint,
92+
{},
93+
on_success,
94+
on_error
95+
)
8896
end
97+
8998
local function put(endpoint, body)
90-
return request(curl.put, endpoint, {body = body})
99+
return request(curl.put, endpoint, { body = body })
91100
end
101+
92102
local function post(endpoint, body)
93-
return request(curl.post, endpoint, {body = body})
103+
return request(curl.post, endpoint, { body = body })
94104
end
95-
____exports.myArticles = function(onSuccess, onError) return get("/articles/me/all", onSuccess, onError) end
96-
____exports.saveArticle = function(id, content) return put(
105+
106+
function M.my_articles(on_success, on_error) return get("/articles/me/all", on_success, on_error) end
107+
108+
function M.save_article(id, content)
109+
return put(
97110
"/articles/" .. tostring(id),
98-
vim.fn.json_encode({article = {body_markdown = content}})
99-
) end
100-
____exports.newArticle = function(title) return post(
111+
vim.fn.json_encode({ article = { body_markdown = content } })
112+
)
113+
end
114+
115+
function M.new_article(title)
116+
return post(
101117
"/articles",
102-
vim.fn.json_encode({article = {body_markdown = article.getTemplate(title)}})
103-
) end
104-
____exports.feed = function(onSuccess, onError) return get("/articles", onSuccess, onError) end
105-
____exports.getArticle = function(id, onSuccess, onError) return get(
118+
vim.fn.json_encode({ article = { body_markdown = article.get_template(title) } })
119+
)
120+
end
121+
122+
function M.feed(on_success, on_error) return get("/articles", on_success, on_error) end
123+
124+
function M.get_article(id, on_success, on_error)
125+
return get(
106126
"/articles/" .. tostring(id),
107-
onSuccess,
108-
onError
109-
) end
110-
____exports.getArticleByPath = function(path, onSuccess, onError) return get("/articles/" .. path, onSuccess, onError) end
111-
return ____exports
127+
on_success,
128+
on_error
129+
)
130+
end
131+
132+
function M.get_article_by_path(path, on_success, on_error) return get("/articles/" .. path, on_success, on_error) end
133+
134+
return M

lua/forem-nvim/article.lua

+14-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
--[[ Generated with https://github.com/TypeScriptToLua/TypeScriptToLua ]]
2-
local ____exports = {}
3-
____exports.getBodyLines = function(article) return vim.split(article.body_markdown or "", "\n") end
4-
____exports.getTemplate = function(title) return ("---\ntitle: " .. title) .. "\npublished: false\ndescription:\ntags:\n# cover_image: https://direct_url_to_image.jpg\n# Use a ratio of 100:42 for best results.\n---\n\n" end
5-
return ____exports
2+
local M = {}
3+
4+
-- TODO: Create class for Article
5+
6+
function M.get_body_lines(article)
7+
return vim.split(article.body_markdown or "", "\n")
8+
end
9+
10+
function M.get_template(title)
11+
return ("---\ntitle: " .. title) ..
12+
"\npublished: false\ndescription:\ntags:\n# cover_image: https://direct_url_to_image.jpg\n# Use a ratio of 100:42 for best results.\n---\n\n"
13+
end
14+
15+
return M

lua/forem-nvim/buffer.lua

+55-43
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,61 @@
1-
--[[ Generated with https://github.com/TypeScriptToLua/TypeScriptToLua ]]
2-
local ____exports = {}
1+
local M = {}
32
local Article = require("forem-nvim.article")
4-
local ____util = require("forem-nvim.util")
5-
local setLocals = ____util.setLocals
6-
local getOption = ____util.getOption
7-
____exports.setBasicOptions = function()
8-
setLocals({{"filetype", "markdown"}, {"modified", false}})
3+
local util = require("forem-nvim.util")
4+
local set_locals = util.set_locals
5+
6+
M.set_basic_options = function()
7+
set_locals({
8+
filetype = "markdown",
9+
modified = false
10+
})
911
end
10-
____exports.write = function(buffer, lines, offset)
11-
local modifiable = getOption(vim.opt_local.modifiable)
12-
vim.opt_local.modifiable = true
13-
vim.api.nvim_buf_set_lines(
14-
buffer,
15-
offset or 0,
16-
-1,
17-
false,
18-
lines
19-
)
20-
vim.opt_local.modifiable = modifiable
12+
13+
M.write = function(buffer, lines, offset)
14+
local modifiable = vim.opt_local.modifiable:get()
15+
vim.opt_local.modifiable = true
16+
vim.api.nvim_buf_set_lines(
17+
buffer,
18+
offset or 0,
19+
-1,
20+
false,
21+
lines
22+
)
23+
vim.opt_local.modifiable = modifiable
2124
end
22-
____exports.getContent = function()
23-
local buffer = vim.api.nvim_get_current_buf()
24-
local lines = vim.api.nvim_buf_get_lines(buffer, 0, -1, true)
25-
return {
26-
content = table.concat(lines, "\n"),
27-
bufnr = buffer
28-
}
25+
26+
M.get_content = function()
27+
local buffer = vim.api.nvim_get_current_buf()
28+
local lines = vim.api.nvim_buf_get_lines(buffer, 0, -1, true)
29+
return {
30+
content = table.concat(lines, "\n"),
31+
bufnr = buffer
32+
}
2933
end
30-
____exports.openMyArticle = function(article)
31-
vim.cmd(":edit forem://my-article/" .. tostring(article.id))
32-
local buffer = vim.api.nvim_get_current_buf()
33-
____exports.write(
34-
buffer,
35-
Article.getBodyLines(article)
36-
)
37-
____exports.setBasicOptions()
38-
setLocals({{"buftype", "acwrite"}, {"swapfile", false}})
34+
35+
M.open_my_article = function(article)
36+
vim.cmd(":edit forem://my-article/" .. tostring(article.id))
37+
local buffer = vim.api.nvim_get_current_buf()
38+
M.write(
39+
buffer,
40+
Article.get_body_lines(article)
41+
)
42+
M.set_basic_options()
43+
set_locals({ buftype = "acwrite", swapfile = false })
3944
end
40-
____exports.loadArticle = function(article)
41-
vim.cmd(":edit forem://article/" .. article.title)
42-
setLocals({{"linebreak", true}, {"textwidth", 80}})
43-
local buffer = vim.api.nvim_get_current_buf()
44-
local body = Article.getBodyLines(article)
45-
____exports.write(buffer, body)
46-
____exports.setBasicOptions()
47-
setLocals({{"modifiable", false}, {"spell", false}, {"buftype", "nowrite"}, {"swapfile", false}})
45+
46+
M.load_article = function(article)
47+
vim.cmd(":edit forem://article/" .. article.title)
48+
set_locals({ linebreak = true, textwidth = 80 })
49+
local buffer = vim.api.nvim_get_current_buf()
50+
local body = Article.get_body_lines(article)
51+
M.write(buffer, body)
52+
M.set_basic_options()
53+
set_locals({
54+
modifiable = false,
55+
spell = false,
56+
buftype = "nowrite",
57+
swapfile = false
58+
})
4859
end
49-
return ____exports
60+
61+
return M

0 commit comments

Comments
 (0)