6

I have a folder structure like this, with recipe .md files in directories according to theme:

Recipes
    |- Mains
    |   |- recipe1.md
    |   |- recipe2.md
    |- Desserts
        |- recipe3.md
        |- recipe4.md

How can I compile all these markdown files into a single PDF book?

I need each recipe to occupy a separate page, with titled sections (chapters) defined by the folders in which recipes are found. I would also like a table of contents with each recipe name, what page it's on and what chapter it is in.

Can I do this using pandoc and LaTeX? Or maybe a command line programme to construct wikis?

1 Answer 1

3

I assume your recipeX.md files already have headlines by themselves? And they are all at level 2?

Then a small series of (Linux or macOS) shell command (or a little script) like the following should work (use the most recent version of Pandoc!):

 
cd Recipes ;
for i in */ ; do
   echo "# ${i%/}" ;
   echo " " ;
   for j in $i/*.md ; do
      cat $i/$j ;
      echo ; 
   done ;
done ; \
|      \
pandoc                        \
  --toc                       \
  --number-sections           \
  --top-level-division=part   \
  --output=my-recipe-book.pdf \
  -

Warning: do not miss the last '-' in the code above!
If your recipeX.md files do NOT have headlines, try this variation:

 
cd Recipes ;
for i in */ ; do
   echo "# ${i%/}" ;
   echo " " ;
   for j in $i/*.md ; do
      echo "## $(basename ${j%.md})" ;
      echo ;
      cat $i/$j ;
      echo ; 
   done ;
done ; \
|      \
pandoc                        \
  --toc                       \
  --number-sections           \
  --top-level-division=part   \
  --output=my-recipe-book.pdf \
  -
2
  • This looks promising but I get a syntax error near unexpected token `|' no matter how I try to fix it, both on Ubuntu and MacOS. Could you double check the syntax here and confirm it's correct? Commented Apr 22, 2019 at 19:48
  • @user1717828: Sorry, I'm on the road right now and cannot test. But you can simply leave out (delete) the one line with the "|" and "\" characters. They are there just to separate the two main commands for visual reasons. Commented Apr 23, 2019 at 5:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .