Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Image files in markdown using relative paths not found converting to docx #3342

Closed
wsmelton opened this issue Jan 8, 2017 · 7 comments
Closed

Comments

@wsmelton
Copy link

wsmelton commented Jan 8, 2017

pandoc version:

pandoc.exe 1.19.1
Compiled with pandoc-types 1.17.0.4, texmath 0.9, highlighting-kate 0.6.3
Default user data directory: C:\Users\wshaw\AppData\Roaming\pandoc
Copyright (C) 2006-2016 John MacFarlane
Web:  http://pandoc.org
This is free software; see the source for copying conditions.
There is no warranty, not even for merchantability or fitness
for a particular purpose.

Command being used:

pandoc.exe -s -S myfile.md -o myfile.docx

I have tested converting my post to a docx file with the image paths shown both in markdown style and HTML (using the img tag). I have to use a relative path for referencing images in my post based on where I have the site hosted (GitHub pages), and that requires that I have a leading backslash:

![](/img/myimage.png)

The above will generate the below error:

[pandoc.exe warning] Could not find image `/img/codewithcode_clonedbatools2.png', skipping...

However an interesting thing is the HTML tag with the same relative path, pandoc does not spit out the warning, but still does not include the image in the docx file.

When I try to convert a given md file with pandoc it cannot find the images unless I remove that leading backslash. As soon as I update the image references to the below line the docx file is generated successfully with images included as expected:

![](/img/myimage.png)

Does a flag exist for pandoc where I wouldn't have to remove the leading backslash or is this just the way it works?

NOTE: I don't mind having to do that as a work around (just a quick find/replace), just curious to know why it cannot handle it.

@jgm
Copy link
Owner

jgm commented Jan 8, 2017

That's an absolute path, not a relative path (though it's a relative URI). Pandoc will look for that file at /img/myimage.png on your file system, not at ./img/myimage.png.

@tarleb
Copy link
Collaborator

tarleb commented Jan 8, 2017

You could use a program acting on pandoc's internal AST instead of doing a manual search/replace operation if you'd like to automate the post-processing of your document: Panflute is a nice and very complete library for this. There is also my small but experimental panlunatic writer, which relies on pandoc's internal lua interpreter to do some light-weight editing. Below is a script that would turn absolute image paths into relative image paths.

-- Make image paths relative by removing any leading slash.
--
-- Save into relimg.lua and make sure `panlunatic.lua` and `dkjson.lua` are in the same dir.
-- Invoke with:
--    pandoc -t relimg.lua [READER-OPTIONS] | pandoc -f json [WRITER-OPTIONS]

panlunatic = require("panlunatic")
setmetatable(_G, {__index = panlunatic})

function Image(s, src, tit, attr)
  return panlunatic.Image(s, src:gsub("^/", ""), tit, attr)
end

@jgm
Copy link
Owner

jgm commented Jan 8, 2017 via email

@tarleb
Copy link
Collaborator

tarleb commented Jan 8, 2017

My plan was to:

  1. enable simple filtering via the custom-writer hack (mostly done);
  2. write a true filtering library in lua (in progress, not much to show yet);
  3. use the library abstractions as the basis for StackValue instances;
  4. integrate library.

I'm still getting used to the lua-way of doing things, progress is slow.

@jgm
Copy link
Owner

jgm commented Jan 8, 2017 via email

@jgm
Copy link
Owner

jgm commented Jan 11, 2017

Anyway, closing this issue -- it does not seem to be a bug, and a workaround has been provided.

@jgm jgm closed this as completed Jan 11, 2017
@offray
Copy link

offray commented Aug 18, 2017

I had a similar issue while trying to implement the Pandoc PDF exportation method in Grafoscopio, with a lot of relative images not found. The solution was to include a command to move to the directory where local routes are defined, before running the Pandoc exportation command.

If useful, here is the Pharo/Smalltalk code that makes this is this:

pandocCommand := 'cd ', self markdownFile parent fullName,'; ',
		'pandoc ', self pandocOptionsComputed, ' ',
		self markdownFile fullName, ' -o ', self pdfFile.
ExternalUnixOSProcess command: pandocCommand.

where markdownFile gives us the source markdown file, pandocOptionsComputed gives us the options we what to use for that particular export (template, font size, and so on) and pdfFile, give us
the output file where we want our exportation to happen. A similar option should be available for
other languages or scripts. The important here is to start your exportation script/method by locating
(cd) to the directory where your relative images are located, before running Pandoc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants