aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrxi <rxi@users.noreply.github.com>2020-05-24 14:10:17 +0100
committerrxi <rxi@users.noreply.github.com>2020-05-24 14:10:17 +0100
commitf44dcbb87123a87dbfff3f734fc338f0865daf69 (patch)
treef592e76e6444eca16c6f5914463ed11709affdfe
parent5dd2f7cda584d43b40af0db69e41312f98c71611 (diff)
downloadlite-xl-plugins-f44dcbb87123a87dbfff3f734fc338f0865daf69.tar.gz
lite-xl-plugins-f44dcbb87123a87dbfff3f734fc338f0865daf69.zip
Added `ghmarkdown` plugin
-rw-r--r--README.md1
-rw-r--r--plugins/ghmarkdown.lua74
2 files changed, 75 insertions, 0 deletions
diff --git a/README.md b/README.md
index 7861183..4c503b5 100644
--- a/README.md
+++ b/README.md
@@ -21,6 +21,7 @@ Plugin | Description
[`drawwhitespace`](plugins/drawwhitespace.lua?raw=1) | Draws tabs and spaces *([screenshot](https://user-images.githubusercontent.com/3920290/80573013-22ae5800-89f7-11ea-9895-6362a1c0abc7.png))*
[`eval`](plugins/eval.lua?raw=1) | Replaces selected Lua code with its evaluated result
[`exec`](plugins/exec.lua?raw=1) | Runs selected text through shell command and replaces with result
+[`ghmarkdown`](plugins/ghmarkdown.lua?raw=1) | Opens a preview of the current markdown file in a browser window *([screenshot](https://user-images.githubusercontent.com/3920290/82754898-f7394600-9dc7-11ea-8278-2305363ed372.png))*
[`gitstatus`](plugins/gitstatus.lua?raw=1) | Displays git branch and insert/delete count in status bar *([screenshot](https://user-images.githubusercontent.com/3920290/81107223-bcea3080-8f0e-11ea-8fc7-d03173f42e33.png))*
[`gofmt`](plugins/gofmt.lua?raw=1) | Auto-formats the current go file, adds the missing imports and the missing return cases
[`hidelinenumbers`](plugins/hidelinenumbers.lua?raw=1) | Hides the line numbers on the left of documents *([screenshot](https://user-images.githubusercontent.com/3920290/81692043-b8b19c00-9455-11ea-8d74-ad99be4b9c5f.png))*
diff --git a/plugins/ghmarkdown.lua b/plugins/ghmarkdown.lua
new file mode 100644
index 0000000..b49e248
--- /dev/null
+++ b/plugins/ghmarkdown.lua
@@ -0,0 +1,74 @@
+local core = require "core"
+local command = require "core.command"
+local keymap = require "core.keymap"
+
+
+local html = [[
+<html>
+ <style>
+ body {
+ margin:80 auto 100 auto;
+ max-width: 750px;
+ line-height: 1.6;
+ font-family: Open Sans, Arial;
+ color: #444;
+ padding: 0 10px;
+ }
+ h1, h2, h3 { line-height: 1.2; padding-top: 14px; }
+ hr { border: 0px; border-top: 1px solid #ddd; }
+ code, pre { background: #f3f3f3; padding: 8px; }
+ code { padding: 4px; }
+ a { text-decoration: none; color: #0366d6; }
+ a:hover { text-decoration: underline; }
+ table { border-collapse: collapse; }
+ table, th, td { border: 1px solid #ddd; padding: 6px; }
+ </style>
+ <head>
+ <title>${title}</title>
+ <head>
+ <body>
+ <script>
+ var xhr = new XMLHttpRequest;
+ xhr.open("POST", "https://api.github.com/markdown/raw");
+ xhr.setRequestHeader("Content-Type", "text/plain");
+ xhr.onload = function() { document.body.innerHTML = xhr.responseText; };
+ xhr.send("${content}");
+ </script>
+ </body>
+</html>
+]]
+
+
+command.add("core.docview", {
+ ["ghmarkdown:show-preview"] = function()
+ local dv = core.active_view
+
+ local content = dv.doc:get_text(1, 1, math.huge, math.huge)
+ local esc = { ['"'] = '\\"', ["\n"] = '\\n' }
+ local text = html:gsub("${(.-)}", {
+ title = dv:get_name(),
+ content = content:gsub(".", esc)
+ })
+
+ local htmlfile = ".lite_ghmarkdown_" .. os.tmpname():gsub("%W", "") .. ".html"
+ local fp = io.open(htmlfile, "w")
+ fp:write(text)
+ fp:close()
+
+ core.log("Opening markdown preview for \"%s\"", dv:get_name())
+ local path = system.absolute_path(".") .. "/" .. htmlfile
+ if PLATFORM == "Windows" then
+ system.exec("start " .. path)
+ else
+ system.exec(string.format("xdg-open %q", path))
+ end
+
+ core.add_thread(function()
+ coroutine.yield(5)
+ os.remove(htmlfile)
+ end)
+ end
+})
+
+
+keymap.add { ["ctrl+shift+m"] = "ghmarkdown:show-preview" }