08/12/2022
How to generate DOCX from Markdown
Markdown is a format, which is usually used for communication and providing documentation for developers and technical products. Markdown supports a lot of necessary features, which help with organizing your documentation. Yet, for sharing the documents, usually used DOCX standard, which can be edited via specialized software, despite markdown can be edited in any text editor. For converting documents, we can use pandoc utility, which can achieve exactly desired behavior by converting from one format to another. Pandoc can be extended by Lua filters and tweaked per our needs.
local cacheLinks = {} -- Stores the found local links in the repository
function file_exists(name)
local f=io.open(name,"r")
if f~=nil then io.close(f) return true else return false end
end
function Link (link, target, title, attr)
local linkTarget = link.target -- where the link points to
if file_exists(linkTarget) then
print("Found the link: " .. linkTarget)
table.insert(cacheLinks, linkTarget)
return {}, {}, {}, {} -- return empty table, which will remove Link from the flow of the document
end
return link
end
function Pandoc(blocks, meta)
local paragraphs = {}
for i = 1, #cacheLinks do -- iterate over the local links table
local fh = io.open(cacheLinks[i]) -- read the contents of that linked file
local contents = pandoc.read( -- parse it with pandoc with markdown parser
fh:read '*a',
"markdown",
PANDOC_READER_OPTIONS
).blocks
blocks.blocks:extend(contents) -- append parsed data to the end of the document
end
return blocks, meta
end
Then, inside your pipeline, you should call the following:
pandoc --lua-filter fix-links-multiple-files.lua -o output.docx -f markdown -t docx README.md --toc
Photo by Kindel Media: https://www.pexels.com/photo/a-close-up-shot-of-paper-clipped-documents-7054757/