Skip to content

Commit

Permalink
add options for special browser to open preview page
Browse files Browse the repository at this point in the history
  • Loading branch information
iamcco committed Nov 3, 2018
1 parent 7e8c7df commit 4d6dcd9
Show file tree
Hide file tree
Showing 15 changed files with 148 additions and 12 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ let g:mkdp_command_for_global = 0
" default: 0
let g:mkdp_open_to_the_world = 0
" switch browser to open preview page
" default: ''
let g:mkdp_browser = ''
" options for markdown render
" mkit: markdown-it options for render
" katex: katex options for math
Expand Down
1 change: 1 addition & 0 deletions app/lib/util/opener.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {};
59 changes: 59 additions & 0 deletions app/lib/util/opener.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
/*
* fork from https://github.com/domenic/opener
*/
const child_process_1 = tslib_1.__importDefault(require("child_process"));
const os_1 = tslib_1.__importDefault(require("os"));
module.exports = function opener(args, tool, callback) {
let platform = process.platform;
args = [].concat(args);
if (tool) {
args.unshift(tool);
}
// Attempt to detect Windows Subystem for Linux (WSL).
// WSL itself as Linux (which works in most cases), but in
// this specific case we need to treat it as actually being Windows.
// The "Windows-way" of opening things through cmd.exe works just fine here,
// whereas using xdg-open does not, since there is no X Windows in WSL.
if (platform === 'linux' && os_1.default.release().indexOf('Microsoft') !== -1) {
platform = 'win32';
}
// http://stackoverflow.com/q/1480971/3191, but see below for Windows.
let command;
switch (platform) {
case 'win32': {
command = 'cmd.exe';
break;
}
case 'darwin': {
command = 'open';
if (tool) {
args.unshift('-a');
}
break;
}
default: {
command = 'xdg-open';
break;
}
}
if (platform === 'win32') {
// On Windows, we really want to use the "start" command.
// But, the rules regarding arguments with spaces, and escaping them with quotes,
// can get really arcane. So the easiest way to deal with this is to pass off the
// responsibility to "cmd /c", which has that logic built in.
//
// Furthermore, if "cmd /c" double-quoted the first parameter,
// then "start" will interpret it as a window title,
// so we need to add a dummy empty-string window title: http://stackoverflow.com/a/154090/3191
//
// Additionally, on Windows ampersand needs to be escaped when passed to "start"
args = args.map(value => {
return value.replace(/&/g, '^&');
});
args = ['/c', 'start', '""'].concat(args);
}
return child_process_1.default.execFile(command, args, null, callback);
};
2 changes: 1 addition & 1 deletion app/out/404.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<!DOCTYPE html><html><head><meta charSet="utf-8" class="next-head"/><link rel="preload" href="/_next/static/Bl3Xk6SKdeG3dxMfwaFwz/pages/404.js" as="script"/><link rel="preload" href="/_next/static/Bl3Xk6SKdeG3dxMfwaFwz/pages/_app.js" as="script"/><link rel="preload" href="/_next/static/Bl3Xk6SKdeG3dxMfwaFwz/pages/_error.js" as="script"/><link rel="preload" href="/_next/static/runtime/webpack-42652fa8b82c329c0559.js" as="script"/><link rel="preload" href="/_next/static/chunks/commons.1c1b5ebd55cbcad2529a.js" as="script"/><link rel="preload" href="/_next/static/runtime/main-94dad8183cac85e3af97.js" as="script"/></head><body><div id="__next"><div data-reactroot="">404</div></div><script>__NEXT_DATA__ = {"props":{"pageProps":{}},"page":"/404","query":{},"buildId":"Bl3Xk6SKdeG3dxMfwaFwz","nextExport":true};__NEXT_LOADED_PAGES__=[];__NEXT_REGISTER_PAGE=function(r,f){__NEXT_LOADED_PAGES__.push([r, f])}</script><script async="" id="__NEXT_PAGE__/404" src="/_next/static/Bl3Xk6SKdeG3dxMfwaFwz/pages/404.js"></script><script async="" id="__NEXT_PAGE__/_app" src="/_next/static/Bl3Xk6SKdeG3dxMfwaFwz/pages/_app.js"></script><script async="" id="__NEXT_PAGE__/_error" src="/_next/static/Bl3Xk6SKdeG3dxMfwaFwz/pages/_error.js"></script><script src="/_next/static/runtime/webpack-42652fa8b82c329c0559.js" async=""></script><script src="/_next/static/chunks/commons.1c1b5ebd55cbcad2529a.js" async=""></script><script src="/_next/static/runtime/main-94dad8183cac85e3af97.js" async=""></script></body></html>
<!DOCTYPE html><html><head><meta charSet="utf-8" class="next-head"/><link rel="preload" href="/_next/static/262vxEr5Glefa1A~zX7ZC/pages/404.js" as="script"/><link rel="preload" href="/_next/static/262vxEr5Glefa1A~zX7ZC/pages/_app.js" as="script"/><link rel="preload" href="/_next/static/262vxEr5Glefa1A~zX7ZC/pages/_error.js" as="script"/><link rel="preload" href="/_next/static/runtime/webpack-42652fa8b82c329c0559.js" as="script"/><link rel="preload" href="/_next/static/chunks/commons.1c1b5ebd55cbcad2529a.js" as="script"/><link rel="preload" href="/_next/static/runtime/main-94dad8183cac85e3af97.js" as="script"/></head><body><div id="__next"><div data-reactroot="">404</div></div><script>__NEXT_DATA__ = {"props":{"pageProps":{}},"page":"/404","query":{},"buildId":"262vxEr5Glefa1A~zX7ZC","nextExport":true};__NEXT_LOADED_PAGES__=[];__NEXT_REGISTER_PAGE=function(r,f){__NEXT_LOADED_PAGES__.push([r, f])}</script><script async="" id="__NEXT_PAGE__/404" src="/_next/static/262vxEr5Glefa1A~zX7ZC/pages/404.js"></script><script async="" id="__NEXT_PAGE__/_app" src="/_next/static/262vxEr5Glefa1A~zX7ZC/pages/_app.js"></script><script async="" id="__NEXT_PAGE__/_error" src="/_next/static/262vxEr5Glefa1A~zX7ZC/pages/_error.js"></script><script src="/_next/static/runtime/webpack-42652fa8b82c329c0559.js" async=""></script><script src="/_next/static/chunks/commons.1c1b5ebd55cbcad2529a.js" async=""></script><script src="/_next/static/runtime/main-94dad8183cac85e3af97.js" async=""></script></body></html>
2 changes: 1 addition & 1 deletion app/out/index.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<!DOCTYPE html><html><head><meta charSet="utf-8" class="next-head"/><title class="next-head">「」</title><link rel="shortcut icon" type="image/ico" href="/_static/favicon.ico" class="next-head"/><link rel="stylesheet" href="/_static/page.css" class="next-head"/><link rel="stylesheet" href="/_static/markdown.css" class="next-head"/><link rel="stylesheet" href="/_static/highlight.css" class="next-head"/><link rel="stylesheet" href="/_static/[email protected]" class="next-head"/><script type="text/javascript" src="/_static/tweenlite.min.js" class="next-head"></script><script type="text/javascript" src="/_static/mermaid.min.js" class="next-head"></script><link rel="preload" href="/_next/static/Bl3Xk6SKdeG3dxMfwaFwz/pages/index.js" as="script"/><link rel="preload" href="/_next/static/Bl3Xk6SKdeG3dxMfwaFwz/pages/_app.js" as="script"/><link rel="preload" href="/_next/static/Bl3Xk6SKdeG3dxMfwaFwz/pages/_error.js" as="script"/><link rel="preload" href="/_next/static/runtime/webpack-42652fa8b82c329c0559.js" as="script"/><link rel="preload" href="/_next/static/chunks/commons.1c1b5ebd55cbcad2529a.js" as="script"/><link rel="preload" href="/_next/static/runtime/main-94dad8183cac85e3af97.js" as="script"/></head><body><div id="__next"><div id="page-ctn"><header id="page-header"><h3><svg viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"><path fill-rule="evenodd" d="M3 5h4v1H3V5zm0 3h4V7H3v1zm0 2h4V9H3v1zm11-5h-4v1h4V5zm0 2h-4v1h4V7zm0 2h-4v1h4V9zm2-6v9c0 .55-.45 1-1 1H9.5l-1 1-1-1H2c-.55 0-1-.45-1-1V3c0-.55.45-1 1-1h5.5l1 1 1-1H15c.55 0 1 .45 1 1zm-8 .5L7.5 3H2v9h6V3.5zm7-.5H9.5l-.5.5V12h6V3z"></path></svg></h3></header><section class="markdown-body"></section></div></div><script>__NEXT_DATA__ = {"props":{"pageProps":{}},"page":"/","query":{},"buildId":"Bl3Xk6SKdeG3dxMfwaFwz","nextExport":true};__NEXT_LOADED_PAGES__=[];__NEXT_REGISTER_PAGE=function(r,f){__NEXT_LOADED_PAGES__.push([r, f])}</script><script async="" id="__NEXT_PAGE__/" src="/_next/static/Bl3Xk6SKdeG3dxMfwaFwz/pages/index.js"></script><script async="" id="__NEXT_PAGE__/_app" src="/_next/static/Bl3Xk6SKdeG3dxMfwaFwz/pages/_app.js"></script><script async="" id="__NEXT_PAGE__/_error" src="/_next/static/Bl3Xk6SKdeG3dxMfwaFwz/pages/_error.js"></script><script src="/_next/static/runtime/webpack-42652fa8b82c329c0559.js" async=""></script><script src="/_next/static/chunks/commons.1c1b5ebd55cbcad2529a.js" async=""></script><script src="/_next/static/runtime/main-94dad8183cac85e3af97.js" async=""></script></body></html>
<!DOCTYPE html><html><head><meta charSet="utf-8" class="next-head"/><title class="next-head">「」</title><link rel="shortcut icon" type="image/ico" href="/_static/favicon.ico" class="next-head"/><link rel="stylesheet" href="/_static/page.css" class="next-head"/><link rel="stylesheet" href="/_static/markdown.css" class="next-head"/><link rel="stylesheet" href="/_static/highlight.css" class="next-head"/><link rel="stylesheet" href="/_static/[email protected]" class="next-head"/><script type="text/javascript" src="/_static/tweenlite.min.js" class="next-head"></script><script type="text/javascript" src="/_static/mermaid.min.js" class="next-head"></script><link rel="preload" href="/_next/static/262vxEr5Glefa1A~zX7ZC/pages/index.js" as="script"/><link rel="preload" href="/_next/static/262vxEr5Glefa1A~zX7ZC/pages/_app.js" as="script"/><link rel="preload" href="/_next/static/262vxEr5Glefa1A~zX7ZC/pages/_error.js" as="script"/><link rel="preload" href="/_next/static/runtime/webpack-42652fa8b82c329c0559.js" as="script"/><link rel="preload" href="/_next/static/chunks/commons.1c1b5ebd55cbcad2529a.js" as="script"/><link rel="preload" href="/_next/static/runtime/main-94dad8183cac85e3af97.js" as="script"/></head><body><div id="__next"><div id="page-ctn"><header id="page-header"><h3><svg viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"><path fill-rule="evenodd" d="M3 5h4v1H3V5zm0 3h4V7H3v1zm0 2h4V9H3v1zm11-5h-4v1h4V5zm0 2h-4v1h4V7zm0 2h-4v1h4V9zm2-6v9c0 .55-.45 1-1 1H9.5l-1 1-1-1H2c-.55 0-1-.45-1-1V3c0-.55.45-1 1-1h5.5l1 1 1-1H15c.55 0 1 .45 1 1zm-8 .5L7.5 3H2v9h6V3.5zm7-.5H9.5l-.5.5V12h6V3z"></path></svg></h3></header><section class="markdown-body"></section></div></div><script>__NEXT_DATA__ = {"props":{"pageProps":{}},"page":"/","query":{},"buildId":"262vxEr5Glefa1A~zX7ZC","nextExport":true};__NEXT_LOADED_PAGES__=[];__NEXT_REGISTER_PAGE=function(r,f){__NEXT_LOADED_PAGES__.push([r, f])}</script><script async="" id="__NEXT_PAGE__/" src="/_next/static/262vxEr5Glefa1A~zX7ZC/pages/index.js"></script><script async="" id="__NEXT_PAGE__/_app" src="/_next/static/262vxEr5Glefa1A~zX7ZC/pages/_app.js"></script><script async="" id="__NEXT_PAGE__/_error" src="/_next/static/262vxEr5Glefa1A~zX7ZC/pages/_error.js"></script><script src="/_next/static/runtime/webpack-42652fa8b82c329c0559.js" async=""></script><script src="/_next/static/chunks/commons.1c1b5ebd55cbcad2529a.js" async=""></script><script src="/_next/static/runtime/main-94dad8183cac85e3af97.js" async=""></script></body></html>
1 change: 0 additions & 1 deletion app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"dependencies": {
"log4js": "^3.0.6",
"neovim": "^4.2.1",
"opener": "^1.5.1",
"socket.io": "^2.1.1",
"tslib": "^1.9.3"
}
Expand Down
16 changes: 10 additions & 6 deletions app/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ if (!/^(\/|C:\\)snapshot/.test(__dirname)) {
const { plugin } = require('./nvim')
const http = require('http')
const websocket = require('socket.io')
const opener = require('opener')

const opener = require('./lib/util/opener')
const logger = require('./lib/util/logger')('app/server')
const { getIP } = require('./lib/util/getIP')

const routes = require('./routes')

let clients = {}
Expand Down Expand Up @@ -108,11 +108,16 @@ async function startServer () {
})
clients = {}
}
function openBrowser ({ bufnr }) {
async function openBrowser ({ bufnr }) {
const openHost = openToTheWord ? getIP() : '127.0.0.1'
const url = `http://${openHost}:${port}/page/${bufnr}`
logger.info('open page: ', url)
opener(url)
const browser = await plugin.nvim.getVar('mkdp_browser')
logger.info(`open page [${browser || 'default'}]: `, url)
if (browser !== '') {
opener(url, browser)
} else {
opener(url)
}
}
plugin.init({
refreshPage,
Expand All @@ -126,4 +131,3 @@ async function startServer () {
}

startServer()

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "markdown-preview.vim",
"version": "0.0.3",
"version": "0.0.4",
"description": "markdown preview plugin for (neo)vim",
"bin": "./app/server.js",
"repository": "https://github.com/iamcco/markdown-preview.vim.git",
Expand All @@ -25,7 +25,6 @@
"neovim": "^4.2.1",
"next": "^7.0.2",
"next-routes": "^1.4.2",
"opener": "^1.5.1",
"react": "^16.5.2",
"react-dom": "^16.5.2",
"socket.io": "^2.1.1",
Expand Down
5 changes: 5 additions & 0 deletions plugin/mkdp.vim
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ if !exists('g:mkdp_open_to_the_world')
let g:mkdp_open_to_the_world = 0
endif

" switch browser to open preview page
if !exists('g:mkdp_browser')
let g:mkdp_browser = ''
endif

if !exists('g:mkdp_preview_options')
let g:mkdp_preview_options = {
\ 'mkit': {},
Expand Down
1 change: 0 additions & 1 deletion src/util/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,3 @@ if (!isRoot) {
module.exports = (name = 'mkdp'): log4js.Logger => {
return log4js.getLogger(name)
}

66 changes: 66 additions & 0 deletions src/util/opener.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* fork from https://github.com/domenic/opener
*/
import childProcess from 'child_process'
import os from 'os'

module.exports = function opener(
args: string | string[],
tool: string | undefined,
callback: (() => any) | undefined
) {
let platform = process.platform
args = [].concat(args)

if (tool) {
args.unshift(tool)
}

// Attempt to detect Windows Subystem for Linux (WSL).
// WSL itself as Linux (which works in most cases), but in
// this specific case we need to treat it as actually being Windows.
// The "Windows-way" of opening things through cmd.exe works just fine here,
// whereas using xdg-open does not, since there is no X Windows in WSL.
if (platform === 'linux' && os.release().indexOf('Microsoft') !== -1) {
platform = 'win32'
}

// http://stackoverflow.com/q/1480971/3191, but see below for Windows.
let command
switch (platform) {
case 'win32': {
command = 'cmd.exe'
break
}
case 'darwin': {
command = 'open'
if (tool) {
args.unshift('-a')
}
break
}
default: {
command = 'xdg-open'
break
}
}

if (platform === 'win32') {
// On Windows, we really want to use the "start" command.
// But, the rules regarding arguments with spaces, and escaping them with quotes,
// can get really arcane. So the easiest way to deal with this is to pass off the
// responsibility to "cmd /c", which has that logic built in.
//
// Furthermore, if "cmd /c" double-quoted the first parameter,
// then "start" will interpret it as a window title,
// so we need to add a dummy empty-string window title: http://stackoverflow.com/a/154090/3191
//
// Additionally, on Windows ampersand needs to be escaped when passed to "start"
args = args.map(value => {
return value.replace(/&/g, '^&')
})
args = ['/c', 'start', '""'].concat(args)
}

return childProcess.execFile(command, args, null, callback)
}

0 comments on commit 4d6dcd9

Please sign in to comment.