Word count for markdown?
There is a new lua filter for that: https://pandoc.org/lua-filters.html#counting-words-in-a-document
Save the following code as wordcount.lua
-- counts words in a document
words = 0
wordcount = {
Str = function(el)
-- we don't count a word if it's entirely punctuation:
if el.text:match("%P") then
words = words + 1
end
end,
Code = function(el)
_,n = el.text:gsub("%S+","")
words = words + n
end,
CodeBlock = function(el)
_,n = el.text:gsub("%S+","")
words = words + n
end
}
function Pandoc(el)
-- skip metadata, just count body:
pandoc.walk_block(pandoc.Div(el.blocks), wordcount)
print(words .. " words in body")
os.exit(0)
end
and call pandoc like this:
pandoc --lua-filter wordcount.lua myfile.md
A somewhat manual solution:
- use
pandoc
to convert the markdown file to a MS Word document (*.docx
) or OpenOffice/LibreOffice Writer document (*.odt
) - open that document in LibreOffice1
- select everything (ctrl+a)
- Menu Tools>Word Count
1 OpenOffice would probably work the same, but I haven't tested that.