Skip to content

Commit

Permalink
ghmarkdown add options (#263)
Browse files Browse the repository at this point in the history
* ghmarkdown add options

- give user the power to easily change which application to use as they
may not want to use their default browser or do not want to fill history
with temporary files.

e.g. change browser to specifically chromium-freeworld and open in a new
incognito window:
chromium-freeworld --incognito --new-window %q

- allow user to set the URL to POST the request to. They may have their
own local processor.

* Bumped manifest version.

---------

Co-authored-by: Adam Harrison <[email protected]>
  • Loading branch information
SwissalpS and adamharrison authored Jan 5, 2025
1 parent 316afd4 commit e981509
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@
"id": "ghmarkdown",
"mod_version": "3",
"path": "plugins/ghmarkdown.lua",
"version": "0.2"
"version": "0.3"
},
{
"description": "Shows \"git blame\" information of a line *([screenshot](https://raw.githubusercontent.com/juliardi/lite-xl-gitblame/main/screenshot_1.png))*",
Expand Down
37 changes: 31 additions & 6 deletions plugins/ghmarkdown.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,34 @@ local command = require "core.command"
local common = require "core.common"
local config = require "core.config"
local keymap = require "core.keymap"
local config = require "core.config"
local common = require "core.common"


config.plugins.ghmarkdown = common.merge({
-- string.format pattern to use for system.exec
exec_format = PLATFORM == "Windows" and "start %s" or "xdg-open %q",
-- the url to send POST request to
url = "https://api.github.com/markdown/raw",
-- The config specification used by the settings gui
config_spec = {
name = "Github Markdown Preview",
{
label = "Exec Pattern",
description = "The string.format() pattern to pass to system.exec.",
path = "exec_format",
type = "string",
default = PLATFORM == "Windows" and "start %s" or "xdg-open %q"
},
{
label = "URL",
description = "The URL to POST the request to for formatting.",
path = "url",
type = "string",
default = "https://api.github.com/markdown/raw"
}
}
}, config.plugins.ghmarkdown)

config.plugins.ghmarkdown = common.merge({
-- Find information on how to generate your own token at
Expand Down Expand Up @@ -47,7 +75,7 @@ local html = [[
<body>
<script>
var xhr = new XMLHttpRequest;
xhr.open("POST", "https://api.github.com/markdown/raw");
xhr.open("POST", "${url}");
xhr.setRequestHeader("content-type", "text/plain");
xhr.setRequestHeader("authorization", "Bearer ${token}");
xhr.setRequestHeader("x-github-api-version", "2022-11-28");
Expand All @@ -70,6 +98,7 @@ command.add("core.docview!", {
local esc = { ['"'] = '\\"', ["\n"] = '\\n' }
local text = html:gsub("${(.-)}", {
title = dv:get_name(),
url = config.plugins.ghmarkdown.url,
content = content:gsub(".", esc),
token = config.plugins.ghmarkdown.github_token
})
Expand All @@ -80,11 +109,7 @@ command.add("core.docview!", {
fp:close()

core.log("Opening markdown preview for \"%s\"", dv:get_name())
if PLATFORM == "Windows" then
system.exec("start " .. htmlfile)
else
system.exec(string.format("xdg-open %q", htmlfile))
end
system.exec(string.format(config.plugins.ghmarkdown.exec_format, htmlfile))

core.add_thread(function()
coroutine.yield(5)
Expand Down

0 comments on commit e981509

Please sign in to comment.