jgm/pandoc

Pandoc errors when full filter path is not specified.

raghur opened this issue · 10 comments

Looks like this is something that's broken recently (used to work with 1.16 & 1.17)
Pandoc version -

D:\code\easyblogger>pandoc --version
pandoc 1.19.1

I have a pandoc filter called mermaid-filter which is installed with npm install -g raghur/mermaid-filter

D:\code\easyblogger\assets>which mermaid-filter.cmd
C:\Users\raghuramanr\AppData\Roaming\npm\mermaid-filter.cmd

This used to work - but now doesnt:

D:\code\easyblogger\assets>pandoc -t html -i test.md -F mermaid-filter.cmd                   
module.js:472                                                                                
    throw err;                                                                               
    ^                                                                                        
                                                                                             
Error: Cannot find module 'D:\code\easyblogger\assets\node_modules\mermaid-filter\index.js'  
    at Function.Module._resolveFilename (module.js:470:15)                                   
    at Function.Module._load (module.js:418:25)                                              
    at Module.runMain (module.js:605:10)                                                     
    at run (bootstrap_node.js:420:7)                                                         
    at startup (bootstrap_node.js:139:9)                                                     
    at bootstrap_node.js:535:3                                                               
pandoc: Error running filter mermaid-filter.cmd                                              
Filter returned error status 1                                                               

when called as a filter with just the cmd file, it cannot find node_modules correctly. Replacing with full path to the filter in the command above makes it succeed but isn't ideal..

Also this works as well (just to emphasize that I have a sane env)

D:\code\easyblogger\assets>pandoc -t json -i test.md | mermaid-filter.cmd
We're really sorry, but anonymous uploading in your country has been disabled. Please <a href="/register">register for an account</a> and try uploading again. (img\diagram-2.png)
{"pandoc-api-version":[1,17,0,4],"meta":{},"blocks":[{"t":"Header","c":[1,["this-is-a-test-file",[],[]],[{"t":"Str","c":"This"},{"t":"Space"},{"t":"Str","c":"is"},{"t":"Space"},{"t":"Str","c":"a"},{"t":"Space"},{"t":"Str","c":"test"},{"t":"Space"},{"t":"Str","c":"file"}]]},{"t":"Para","c":[{"t":"Image","c":[["",[],[]],[],["",""]]}]},{"t":"Para","c":[{"t":"Str","c":"This"},{"t":"Space"},{"t":"Str","c":"should"},{"t":"Space"},{"t":"Str","c":"get"},{"t":"Space"},{"t":"Str","c":"converted"}]}]}
jgm commented

THe generated cmd file uses ~dp0 to resolve teh path of the script. Looks like when specified as just the cmd file in pandoc -F, this ~dp0 results in the CWD rather than the location of the batch file...

Definitely not a pandoc issue - but I wonder if this is something in how windows behaves :(

D:\code\easyblogger\assets>pandoc -t html -i test.md -F mermaid-filter.cmd                  
"mermaid-filter.cmd" D:\code\easyblogger\assets\                                            
module.js:472                                                                               
    throw err;                                                                              
    ^                                                                                       
                                                                                            
Error: Cannot find module 'D:\code\easyblogger\assets\node_modules\mermaid-filter\index.js' 
    at Function.Module._resolveFilename (module.js:470:15)                                  
    at Function.Module._load (module.js:418:25)                                             
    at Module.runMain (module.js:605:10)                                                    
    at run (bootstrap_node.js:418:7)                                                        
    at startup (bootstrap_node.js:139:9)                                                    
    at bootstrap_node.js:533:3                                                              
Error running filter mermaid-filter.cmd                                                     
Filter returned error status 1                                                              
                                                                                            
D:\code\easyblogger\assets>mermaid-filter.cmd                                               
mermaid-filter.cmd C:\Users\raghuramanr\AppData\Roaming\npm\                                

So fixed at my end by modifying the generated cmd file to use ~dp$PATH:0 instead of ~dp0
Thanks again for pandoc - this just was a CMD interpreter issue on win 8.1. When it used to work earlier, I was on Win 7 and I don't remember having this trouble.

@IF EXIST "%~dp0\node.exe" (
    echo 1 1>&2
  "%~dp0\node.exe"  "%~dp$PATH:0\node_modules\mermaid-filter\index.js" %*
) ELSE (
  @SETLOCAL
  @SET PATHEXT=%PATHEXT:;.JS;=;%
  REM @echo %0 %~dp0 %~dp$PATH:0 1>&2
  node "%~dp$PATH:0\node_modules\mermaid-filter\index.js" %*
)
jgm commented

Hopefully editing the globally installed .cmd file isn't the long term fix!

I agree - it's ugly... it's however the only thing that worked for me.... Also have to redo it every time the npm package is upgraded/reinstalled

mb21 commented

Definitely not a pandoc issue - but I wonder if this is something in how windows behaves :(

I'm not a Windows guy either, but if you guys know how to fix this for windows, suggestions are welcome...

larsw commented

Maybe it is possible to have a postinstall script in the mermaid-filter package that overwrite the cmd file with the monkey-patched version if the install target is Windows? Just a thought.

The following PowerShell could be used as a post install script:

$Path = "$env:APPDATA\npm\mermaid-filter.cmd"
$Content = (Get-Content $Path ).Replace('node  "%~dp0','node  "%~dp$PATH:0')
$Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding $False
[System.IO.File]::WriteAllLines($Path, $Content, $Utf8NoBomEncoding)

my solution is "pass to wsl".

Write ./publish.sh in Windows folder.

pandoc ./input.md --from=markdown --to=docx --standalone -F $HOME/.volta/bin/mermaid-filter --reference-doc=./template.docx --output=./output.docx

Next, run this command

wsl.exe -d arch -- ./publish.sh

In command passing, passed command is executes in ./, so don't mind directory location.
( If not in $env:SystemDrive, It may not be mounted and requires attention.)

If you do exactly the same as me, as a prerequisite, volta and mermaid-filter must be installed in WSL2 Archlinux.

wsl.exe -d arch -- volta install mermaid-filter