joeyespo / grip

Preview GitHub README.md files locally before committing them.
MIT License
6.45k stars 423 forks source link

table of contents? #289

Open robertmarkbram opened 5 years ago

robertmarkbram commented 5 years ago

Is there a way to generate a table of contents when using --export?

Love this tool btw.

robertmarkbram commented 5 years ago

I am using the following in a bash script as a pre-processing step to achieve this atm.

   rm -r temp.md
   touch temp.md
   echo -e "\n\nTOC\n" >> temp.md
   IFS=$'\n';
   for line in $(cat README.md | grep -E '^#+ ') ; do
      heading=$(echo "${line}" | sed -re 's/[ \t]*$//' -e 's/^#+ (.*)$/\1/')
      link=$(echo "${line}" | sed -re 's/[ \t]*$//' -e 's/^#+ (.*)/\1/' -e 's/ /-/g' -e 's/.*/\L&/g')
      bullet=$(echo "${line}" | sed -re 's/^(#*)# .*/\1- /' -e 's/#/  /g')
      echo "${bullet}[${heading}](#${link})" >> temp.md
   done
   echo -e "\n" >> temp.md
   cat README.md >> temp.md
   grip temp.md --export readme.html
t0stiman commented 5 years ago

I use doctoc.