-
-
Notifications
You must be signed in to change notification settings - Fork 253
/
instant-markdown.vim
332 lines (282 loc) · 9.29 KB
/
instant-markdown.vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
" # Configuration
if !exists('g:instant_markdown_slow')
let g:instant_markdown_slow = 0
endif
if !exists('g:instant_markdown_autostart')
let g:instant_markdown_autostart = 1
endif
if !exists('g:instant_markdown_open_to_the_world')
let g:instant_markdown_open_to_the_world = 0
endif
if !exists('g:instant_markdown_allow_unsafe_content')
let g:instant_markdown_allow_unsafe_content = 0
endif
if !exists('g:instant_markdown_allow_external_content')
let g:instant_markdown_allow_external_content = 1
endif
if !exists('g:instant_markdown_mathjax')
let g:instant_markdown_mathjax = 0
endif
if !exists('g:instant_markdown_mermaid')
let g:instant_markdown_mermaid = 0
endif
if !exists('g:instant_markdown_theme')
let g:instant_markdown_theme = 'light'
endif
if !exists('g:instant_markdown_logfile')
let g:instant_markdown_logfile = (has('win32') || has('win64') ? 'NUL' : '/dev/null')
elseif filereadable(g:instant_markdown_logfile)
"Truncate the log file
call writefile([''], g:instant_markdown_logfile)
endif
if !exists('g:instant_markdown_autoscroll')
let g:instant_markdown_autoscroll = 1
endif
if !exists('g:instant_markdown_port')
let g:instant_markdown_port = 8090
endif
if !exists('g:instant_markdown_python')
let g:instant_markdown_python = 0
endif
" # Utility Functions
let s:ROOT_DIR = fnamemodify(resolve(expand('<sfile>:p')), ':h')
let s:shell_redirect = ' 1>> '. g:instant_markdown_logfile . ' 2>&1 '
" Simple system wrapper that ignores empty second args
function! s:system(cmd, stdin)
if strlen(a:stdin) == 0
call system(a:cmd)
else
call system(a:cmd, a:stdin)
endif
endfu
" Wrapper function to automatically execute the command asynchronously and
" redirect output in a cross-platform way. Note that stdin must be passed as a
" List of lines.
function! s:systemasync(cmd, stdinLines)
let cmd = a:cmd . s:shell_redirect
if has('win32') || has('win64')
call s:winasync(cmd, a:stdinLines)
elseif has('nvim')
let job_id = jobstart(cmd)
call chansend(job_id, join(a:stdinLines, "\n"))
call chanclose(job_id, 'stdin')
else
let cmd = cmd . ' &'
call s:system(cmd, join(a:stdinLines, "\n"))
endif
endfu
" Executes a system command asynchronously on Windows. The List stdinLines will
" be concatenated and passed as stdin to the command. If the List is empty,
" stdin will also be empty.
function! s:winasync(cmd, stdinLines)
" To execute a command asynchronously on windows, the script must use the
" "!start" command. However, stdin can't be passed to this command like
" system(). Instead, the lines are saved to a file and then piped into the
" command.
if len(a:stdinLines)
let tmpfile = tempname()
call writefile(a:stdinLines, tmpfile)
let command = 'type ' . tmpfile . ' | ' . a:cmd
else
let command = a:cmd
endif
exec 'silent !start /b cmd /c ' . command
endfu
function! s:refreshView()
let bufnr = expand('<bufnr>')
call s:systemasync("curl -X PUT -T - http://localhost:".g:instant_markdown_port,
\ s:bufGetLines(bufnr))
endfu
function! s:startDaemon(initialMDLines)
let env = ''
let argv = ''
if !g:instant_markdown_python
if g:instant_markdown_open_to_the_world
let env .= 'INSTANT_MARKDOWN_OPEN_TO_THE_WORLD=1 '
endif
if g:instant_markdown_allow_unsafe_content
let env .= 'INSTANT_MARKDOWN_ALLOW_UNSAFE_CONTENT=1 '
endif
if !g:instant_markdown_allow_external_content
let env .= 'INSTANT_MARKDOWN_BLOCK_EXTERNAL=1 '
endif
if g:instant_markdown_mathjax
let argv .= ' --mathjax'
endif
if g:instant_markdown_mermaid
let argv .= ' --mermaid'
endif
endif
if exists('g:instant_markdown_browser')
let argv .= " --browser '".g:instant_markdown_browser."'"
endif
let argv .= ' --port '.g:instant_markdown_port
if exists('g:instant_markdown_theme')
let argv .= ' --theme '.g:instant_markdown_theme
endif
if g:instant_markdown_python
call s:systemasync(env.'smdv --stdin'.argv, a:initialMDLines)
else
call s:systemasync(env.s:ResolveExecutable(s:ROOT_DIR).argv, a:initialMDLines)
endif
endfu
function! s:initDict()
if !exists('s:buffers')
let s:buffers = {}
endif
endfu
function! s:pushBuffer(bufnr)
call s:initDict()
let s:buffers[a:bufnr] = 1
endfu
function! s:popBuffer(bufnr)
call s:initDict()
call remove(s:buffers, a:bufnr)
endfu
function! s:killDaemon()
call s:systemasync("curl -X DELETE -w 'exit status: %{http_code}' http://localhost:".g:instant_markdown_port, [])
endfu
function! s:bufGetLines(bufnr)
let lines = getbufline(a:bufnr, 1, "$")
if g:instant_markdown_autoscroll
" inject row marker
" The marker is inserted after an empty line to prevent it from interfering with
" mathjax/latex equations (which do not allow empty lines). The marker needs to be inserted
" before a non-empty line.
" start of line followed by optional whitespace
" followed by one of '\r\n', '\r' or '\n' optionally followed by white
" space followed by non-whitespace '\S'
let pattern = '^\s*\(\(\r\n\|[\n\r]\).*\S\)\@='
" search backwards from cursor and don't wrap around and don't move cursor
let row_num = search(pattern,'bnW')
" The marker needs to be inserted on new line otherwise two paragraphs might be fused.
call insert(lines, '<a name="#marker" id="marker"></a>', row_num)
endif
return lines
endfu
" I really, really hope there's a better way to do this.
fu! s:myBufNr()
return str2nr(expand('<abuf>'))
endfu
" # Functions called by autocmds
"
" ## push a new Markdown buffer into the system.
"
" 1. Track it so we know when to garbage collect the daemon
" 2. Start daemon if we're on the first MD buffer.
" 3. Initialize changedtickLast, possibly needlessly(?)
fu! s:pushMarkdown()
let bufnr = s:myBufNr()
call s:initDict()
if len(s:buffers) == 0
call s:startDaemon(s:bufGetLines(bufnr))
endif
call s:pushBuffer(bufnr)
let b:changedtickLast = b:changedtick
endfu
" ## pop a Markdown buffer
"
" 1. Pop the buffer reference
" 2. Garbage collection
" * daemon
" * autocmds
fu! s:popMarkdown()
let bufnr = s:myBufNr()
silent au! instant-markdown * <buffer=abuf>
call s:popBuffer(bufnr)
if len(s:buffers) == 0
call s:killDaemon()
endif
endfu
" ## Refresh if there's something new worth showing
"
" 'All things in moderation'
fu! s:temperedRefresh()
if !exists('b:changedtickLast')
let b:changedtickLast = b:changedtick
elseif b:changedtickLast != b:changedtick
let b:changedtickLast = b:changedtick
call s:refreshView()
endif
endfu
fu! s:previewMarkdown()
call s:startDaemon(getline(1, '$'))
aug instant-markdown
if g:instant_markdown_slow
au CursorHold,BufWrite,InsertLeave <buffer> call s:temperedRefresh()
else
au CursorHold,CursorHoldI,CursorMoved,CursorMovedI <buffer> call s:temperedRefresh()
endif
au BufUnload <buffer> call s:cleanUp()
aug END
endfu
fu! s:cleanUp()
call s:killDaemon()
au! instant-markdown * <buffer>
endfu
if g:instant_markdown_autostart
" # Define the autocmds "
aug instant-markdown
au! * <buffer>
au BufEnter <buffer> call s:refreshView()
if g:instant_markdown_slow
au CursorHold,BufWrite,InsertLeave <buffer> call s:temperedRefresh()
else
au CursorHold,CursorHoldI,CursorMoved,CursorMovedI <buffer> call s:temperedRefresh()
endif
au BufUnload <buffer> call s:popMarkdown()
au BufWinEnter <buffer> call s:pushMarkdown()
aug END
endif
" Searches for the existence of a directory accross
" ancestral parents
function! s:TraverseAncestorDirSearch(rootDir) abort
let l:root = a:rootDir
let l:dir = 'node_modules'
while 1
let l:searchDir = l:root . '/' . l:dir
if isdirectory(l:searchDir)
return l:searchDir
endif
let l:parent = fnamemodify(l:root, ':h')
if l:parent == l:root
return -1
endif
let l:root = l:parent
endwhile
endfunction
function! s:ResolveExecutable(...) abort
let l:rootDir = a:0 > 0 ? a:1 : 0
let l:exec = -1
if executable('instant-markdown-d')
let l:exec = 'instant-markdown-d'
elseif isdirectory(l:rootDir)
let l:dir = s:TraverseAncestorDirSearch(l:rootDir)
if l:dir != -1
let l:exec = s:GetExecPath(l:dir)
endif
else
let l:exec = s:GetExecPath()
endif
if l:exec == -1
echoerr "Node.js server instant-markdown-d is unavailable. See https://github.com/instant-markdown/instant-markdown-d for installation instructions"
endif
return l:exec
endfunction
function! s:GetExecPath(...) abort
let l:rootDir = a:0 > 0 ? a:1 : -1
let l:dir = l:rootDir != -1 ? l:rootDir . '/.bin/' : ''
let l:path = l:dir . 'instant-markdown-d'
if executable(l:path)
return l:path
else
return l:dir . 'instant-markdown-d'
endif
endfunction
function! s:instant_markdown_d_path()
let l:pluginExec = s:ResolveExecutable(s:ROOT_DIR)
echom l:pluginExec
endfu
command! -buffer InstantMarkdownPreview call s:previewMarkdown()
command! -buffer InstantMarkdownStop call s:cleanUp()
command! InstantMarkdownDPath call s:instant_markdown_d_path()