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
- Create a folder to store media for each file, and this folder that contains the media, would have the name of the file converted.
- Generate random, unique name, for the images.
- 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:



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.