Skip to content

Commit

Permalink
fix: image path issue with msys2/vim (iamcco#604)
Browse files Browse the repository at this point in the history
* fix: image path issue with msys2/vim

1) Issue background
    a) on 'mingw32' terminal
    b) executable 'vim' in /usr/bin
    c) executable 'node' in /mingw32/bin.
    d) plugin 'markdow-preview.nvim' in ~/.vim/plugged
    e) app/route.js:68 fileDir is a  unix path
    f) image missing in browser preview
2) Issue fixing
    add code to app/route.js:
    a) extract info from environment variable MINGW_HOME
    b) prefixing imgPath with sth. like "E:\msys64\"
3) No issue alternative
    a) on 'ucrt64' terminal
    b) executable 'nvim' in /ucrt64/bin
    c) executable 'node' in /ucrt64/bin
    d) plugin 'markdown-preview.nvim' in ~/.vim/plugged
    e) app/route.js:68 fileDir is a win path
    f) image ok in browser preview

* fix: now work on any windows disk device

1) Based on info from MINGW_HOME is error-prone. Maybe the document is
   located on a windisk other than that of msys2 installation.
2) use 'cygpath.exe' to convert unix-like 'fileDir' to win-like one.
3) the conversion operation must follow the first assignment of 'fileDir'.
  • Loading branch information
zhenggennj authored Oct 14, 2023
1 parent a1ca115 commit 886dea0
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions app/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,26 @@ use(async (req, res, next) => {
const buffers = await plugin.nvim.buffers
const buffer = buffers.find(b => b.id === Number(req.bufnr))
if (buffer) {
const fileDir = await plugin.nvim.call('expand', `#${req.bufnr}:p:h`)
let fileDir = await plugin.nvim.call('expand', `#${req.bufnr}:p:h`)
logger.info('fileDir', fileDir)

const mingw_home=process.env.MINGW_HOME;
if (mingw_home){
if(! fileDir.includes(':')){
// fileDir is unix-like: /Z/x/y/...., 'Z' means Z:
// the win-like fileDir should be: Z:\x\y...
const cygpath = 'cygpath.exe'
const cmd=cygpath+' -w'+' -a '+fileDir ;
logger.info('cmd',cmd)

const { execSync } = require('node:child_process');
const result = execSync(cmd);
fileDir=result.toString('utf8').replace('\n','');

logger.info('New fileDir',fileDir);
}
}

let imgPath = decodeURIComponent(decodeURIComponent(req.asPath.replace(reg, '')))
imgPath = imgPath.replace(/\\ /g, ' ')
if (imgPath[0] !== '/' && imgPath[0] !== '\\') {
Expand All @@ -82,7 +100,8 @@ use(async (req, res, next) => {
}
}
}
logger.info('imgPath', imgPath)
logger.info('imgPath', imgPath);

if (fs.existsSync(imgPath) && !fs.statSync(imgPath).isDirectory()) {
if (imgPath.endsWith('svg')) {
res.setHeader('content-type', 'image/svg+xml')
Expand Down

0 comments on commit 886dea0

Please sign in to comment.