Extract to same directory, media from different .docx files converted with Pandoc

Viewed 410

Goal

I'm converting some .docx to .md with pandoc. These .docx have images that, after conversion, were being placed in a directory (markdown-repository/media/) and it's URL was being referenced in the resulting .md file.

So the goal is to have the resulting .md files with links pointing to the proper images stored in markdown-repository/media/. For this to happen, all images under markdown-repository/media/ need to have an unique name.

The problem

For each conversion, the images were being smashed by the last conversion, because pandocs doesn't track the image names, it creates image1.png, image2.png, image3.png, etc... for each converted file.

My suggestion

  1. Create a folder to store media for each file, and this folder that contains the media, would have the name of the file converted.
  2. Generate random, unique name, for the images.
  3. Replace the links in the .md file with the generated images names.

Example:

fileA.docx fileB.docx

Step 1

Convert the .docx to .md:

pandoc --extract-media=/result-media/output-media-for-fileA/ -f docx -t markdown fileA.docx  -o fileA.md

pandoc --extract-media=/result-media/output-media-for-fileB/ -f docx -t markdown fileB.docx  -o fileB.md

At this point we will have under /result-media/output-media-for-fileA/ 3 images

image1.png
image2.png
image3.png

and in the fileA.md these 3 links pointing to those images:

![](/result-media/output-media-for-fileA/image1.png)  
![](/result-media/output-media-for-fileA/image2.png)  
![](/result-media/output-media-for-fileA/image3.png)  

Note: the same for fileB (i will not put here to be more simple, just replace fileA for fileB in the links)

Step 2

Then generate unique file names for the images under /result-media/output-media-for-fileA/ and /result-media/output-media-for-fileB/ and somehow save "the logs" so that we can then replace the image name with the new name inside the fileA.md and fileB.md

Note: This step is where i'm having most difficulty.

Step 3

Then i could just move all images with unique name, to my main folder markdown-repository/media/ .

Resources

This problem was already asked on Pandoc forum but it seems that Pandoc doesn't have any feature to handle this so i believe that with the help of linux shell scripting we can turn around.

https://pandoc.org/MANUAL.html

2 Answers

Get-ChildItem . -Filter *.docx | Foreach-Object { pandoc --from docx --to markdown --extract-media= --wrap=none $_ -o $_.Name.Replace('.docx', '.md')

This worked for me:

Get-ChildItem . -Recurse -Filter *.docx | 
Foreach-Object { pandoc --from docx --to markdown --extract-media=$($_.DirectoryName) --wrap=none $_.FullName -o $_.FullName.Replace('.docx', '.md')}

The script converts all docx to md and keeps the folder structure. What was missing in the --extract-media command was the directory location to create the .\media directory. The $.DirectoryName needs to be expanded, $($.DirectoryName); otherwise, powershell will read it as text as opposed to a variable which will result in unexpected results.

In regards to the --wrap parameter, from pandoc:

--wrap=auto|none|preserve

Determine how text is wrapped in the output (the source code, not the rendered version). With auto (the default), pandoc will attempt to wrap lines to the column width specified by --columns (default 72). With none, pandoc will not wrap lines at all. With preserve, pandoc will attempt to preserve the wrapping from the source document (that is, where there are nonsemantic newlines in the source, there will be nonsemantic newlines in the output as well). Automatic wrapping does not currently work in HTML output. In ipynb output, this option affects wrapping of the contents of markdown cells.

Related