Skip to content

Commit

Permalink
A quasi real time vim markdown preview plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
naosense committed Aug 3, 2018
0 parents commit 38a0ae7
Show file tree
Hide file tree
Showing 248 changed files with 8,124 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
autoload/java/
.idea/
target/
*.class

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.war
*.ear
*.jar

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
108 changes: 108 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
[TOC]

作为Vim粉丝一枚,一直在寻找一款好用的Markdown预览插件,功能要简单,能在书写之余偶尔撇一下效果即可,毕竟Markdown语法都烂熟于心了。先是偶然看到了Chrome一款插件[Markdown Viewer](https://chrome.google.com/webstore/detail/markdown-viewer/ckkdlimhmcjmikdlpkmbgfkaikojcbjk),风格非常喜欢,遗憾的是不能与Vim同步,后来又发现一款Vim插件[markdown-preview.vim](https://github.com/iamcco/markdown-preview.vim),但是我装了好几次,在我的机器上老是报错。故事本来到此就结束了,但是程序猿乞肯如此轻易屈服,程序猿要让这天,再遮不住眼,要这地,再埋不了心,程序猿要用自己的双手开天辟地!经过程序猿在键盘上一顿猛干,[markdown-preview-sync](https://github.com/pingao777/markdown-preview-sync)横空出世了!好了,就吹到这吧。

运行效果如下:

![snapshot-en](http://ozgrgjwvp.bkt.clouddn.com/markdown-preview-sync/en.png)

![snapshot-ch](http://ozgrgjwvp.bkt.clouddn.com/markdown-preview-sync/ch.png)

下面简单介绍下这款插件:

### 特性

- 代码高亮
- MathJax
- 自定义CSS
- GFM-Table
- 目录TOC

### 安装准备

- Jre8及以上
- Vim支持python2或python3

### 安装方式

如果你使用[pathogen](https://github.com/tpope/vim-pathogen),可以运行如下命令:

```git
cd your-path-to-bundle
git clone git@github.com:pingao777/markdown-preview-sync.git
```

如果你没有使用任何插件管理工具,直接将/autoload和/plugin目录中的文件分别放在Vim的/autoload和/plugin目录即可。

注:/src目录是项目源代码,如果不关注直接删掉就可以了。

### 设置

```vim
" Chrome和Firefox都可以,推荐使用Chrome
" 可以这样设置Chrome路径
let g:markdown_preview_sync_chrome_path = ""
" 设置Firefox浏览器路径
let g:markdown_preview_sync_firefox_path = ""
" (Optional)设置自定义CSS主题,将你的CSS文件放在autoload/java/webapp/css文件夹下,
" 以“主题名-theme.css”方式命名,然后设置如下变量
let g:markdown_preview_sync_theme = "主题名"
" 配置快捷键
autocmd filetype markdown nnoremap <F9> :MarkSyncPreview<cr>
autocmd filetype markdown nnoremap <S-F9> :MarkSyncClose<cr>
```

最后,欢迎大家多多提意见和建议。

---

### Feature

- Code Highlight
- MathJax
- Custom CSS
- GFM-Table
- TOC

### Prerequisite

- Jre8 or above
- Vim with python2 or python3 support

### Installation

If you use [pathogen](https://github.com/tpope/vim-pathogen), do this:

```git
cd your-path-to-bundle
git clone git@github.com:pingao777/markdown-preview-sync.git
```

If you don't use any plugin manager, just copy files in /autoload and /plugin to Vim's /autoload and /plugin directory.

Note: /src directory is source code, you can simply delete it if you don't need it.

### Setting

```vim
" Both Chrome and Firefox are good, but Chrome is prefer
" Set Chrome path
let g:markdown_preview_sync_chrome_path = ""
" Set Firefox path
let g:markdown_preview_sync_firefox_path = ""
" (Optional)Set your own css theme, put your css file in
" autoload/java/webapp/css directory with a name: name-theme.css,
" then set
let g:markdown_preview_sync_theme = "name"
" Set key
autocmd filetype markdown nnoremap <F9> :MarkSyncPreview<cr>
autocmd filetype markdown nnoremap <S-F9> :MarkSyncClose<cr>
```

Last but not least, comments and issues are welcome.
143 changes: 143 additions & 0 deletions autoload/mpsync.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
if !has("python") && !has("python3")
echo "vim has to be compiled with +python or +python3 to run this"
finish
endif

if exists("g:markdown_preview_sync_loaded")
finish
endif

let g:markdown_preview_sync_loaded = 1

if !exists("g:markdown_preview_sync_port")
let g:markdown_preview_sync_port = 7788
endif

if !exists("g:markdown_preview_sync_theme")
let g:markdown_preview_sync_theme = "github"
endif

let s:plugin_root_dir = fnamemodify(resolve(expand("<sfile>:p")), ":h")

python << EOF
import sys
from os.path import normpath, join
import vim
plugin_root_dir = vim.eval('s:plugin_root_dir')
python_root_dir = normpath(join(plugin_root_dir, 'python'))
sys.path.insert(0, python_root_dir)
import java_vim_bridge
java_vim_bridge.set_port(vim.eval('g:markdown_preview_sync_port'))
EOF

function! s:start()
execute "silent !start /b java -jar \"" . s:plugin_root_dir . "\"/java/markdown-preview-sync.jar " . g:markdown_preview_sync_port . " " . g:markdown_preview_sync_theme
endfunction

function! s:is_start()
return exists("g:markdown_preview_sync_start")
endfunction

function! s:open()
if exists("g:markdown_preview_sync_chrome_path")
execute "silent !start " . g:markdown_preview_sync_chrome_path . " --app=http://127.0.0.1:" . g:markdown_preview_sync_port . "/index"
elseif exists("g:markdown_preview_sync_firefox_path")
execute "silent !start " . g:markdown_preview_sync_firefox_path . " http://127.0.0.1:" . g:markdown_preview_sync_port . "/index"
else
echoerr "Not set browser path"
endif
endfunction

function! s:sync()
python <<EOF
path = vim.eval('expand("%:p")')
content = vim.eval('join(getline(1, line("$")), "\n")')
current = int(vim.eval('line(".")'))
if current == 1:
java_vim_bridge.sync(path, content, 1)
else:
bottom = int(vim.eval('line("w$")'))
java_vim_bridge.sync(path, content, bottom)
EOF
endfunction

function! s:close()
python <<EOF
path = vim.eval('expand("%:p")')
java_vim_bridge.close(path)
EOF
endfunction

function! s:stop()
python <<EOF
path = vim.eval('expand("%:p")')
java_vim_bridge.stop()
EOF
endfunction

function! s:autocmd()
augroup markdown_preview_sync
autocmd!
autocmd cursormoved,cursormovedi <buffer> call s:trigger_sync()
autocmd bufwrite <buffer> call s:sync()
autocmd vimleave * call s:stop()
augroup end
endfunction

function! s:trigger_sync()
if !exists("b:old_current")
let b:old_current = 0
endif
if !exists("b:old_bottom ")
let b:old_bottom = 0
endif
if !exists("b:old_last ")
let b:old_last = 0
endif

let l:new_current = line(".")
let l:new_bottom = line("w$")
let l:new_last = line("$")
if b:old_last ==# l:new_last
if b:old_bottom > l:new_bottom
if b:old_bottom - l:new_bottom >= 5
call s:sync()
let b:old_current = l:new_current
let b:old_bottom = l:new_bottom
let b:old_last = l:new_last
endif
elseif b:old_bottom < l:new_bottom
call s:sync()
let b:old_current = l:new_current
let b:old_bottom = l:new_bottom
let b:old_last = l:new_last
elseif b:old_current != l:new_current
call s:sync()
let b:old_current = l:new_current
let b:old_bottom = l:new_bottom
let b:old_last = l:new_last
endif
else
call s:sync()
let b:old_current = l:new_current
let b:old_bottom = l:new_bottom
let b:old_last = l:new_last
endif


endfunction

function! mpsync#preview()
if !s:is_start()
call s:start()
let g:markdown_preview_sync_start = 1
endif
call s:open()
call s:autocmd()
endfunction

function! mpsync#close()
if s:is_start()
call s:close()
endif
endfunction
40 changes: 40 additions & 0 deletions autoload/python/java_vim_bridge.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# -*- coding: utf-8 -*-
"""Bridge connect vim to java
@author wocanmei
@date 2018-07-15 17:56:25
"""

import base64

try:
from urllib.parse import urlencode
from urllib.request import urlopen, Request
except ImportError:
from urllib import urlencode
from urllib2 import urlopen, Request


url_pre = 'http://127.0.0.1:port'


def set_port(port):
global url_pre
url_pre = url_pre.replace('port', port)


def sync(path, content, bottom):
params = {'path': path, 'content': base64.b64encode(content), 'bottom': bottom}
request = Request(url_pre + '/sync', data = urlencode(params))
urlopen(request)


def close(path):
params = {'path': path}
request = Request(url_pre + '/close?' + urlencode(params))
urlopen(request)


def stop():
request = Request(url_pre + '/stop')
urlopen(request)
2 changes: 2 additions & 0 deletions plugin/mpsync.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
command! -nargs=0 MarkSyncPreview call mpsync#preview()
command! -nargs=0 MarkSyncClose call mpsync#close()
Loading

0 comments on commit 38a0ae7

Please sign in to comment.