Skip to content

Instantly share code, notes, and snippets.

@mofosyne
Created April 25, 2024 14:08
Show Gist options
  • Save mofosyne/8f2915c747af44cc71bf85954ffdb0b8 to your computer and use it in GitHub Desktop.
Save mofosyne/8f2915c747af44cc71bf85954ffdb0b8 to your computer and use it in GitHub Desktop.
Generate A Markdown TOC from a pdf via this bash function (Useful for Obsidian Note Taking)
# Usage: generate_markdown_toc "AS 3000-2018 Wiring Rules.pdf" 3
generate_markdown_toc() {
local pdf_file="$1"
local max_level="$2"
pdftk "$pdf_file" dump_data | awk -v max_level="$max_level" '/BookmarkTitle:/ {gsub("BookmarkTitle: ", ""); title=$0} /BookmarkPageNumber:/ {gsub("BookmarkPageNumber: ", ""); page=$0} /BookmarkLevel:/ {gsub("BookmarkLevel: ", ""); level=$0; if (level <= max_level) printf("%s- [ ] %s (Page %s)\n", sprintf("%" level*2 "s", ""), title, page)}'
}
@mofosyne
Copy link
Author

mofosyne commented Apr 25, 2024

Here's is awk but broken up to make it easier to understand

# Usage: generate_markdown_toc "AS 3000-2018 Wiring Rules.pdf" 3
generate_markdown_toc() {
    local pdf_file="$1"
    local max_level="$2"
	awk -v max_level="$max_level" '
	    /BookmarkTitle:/ {
	        gsub("BookmarkTitle: ", "");
	        title=$0
	    }
	    /BookmarkPageNumber:/ {
	        gsub("BookmarkPageNumber: ", "");
	        page=$0
	    }
	    /BookmarkLevel:/ {
	        gsub("BookmarkLevel: ", "");
	        level=$0;
	        if (level <= max_level) {
	            printf("%s- [ ] %s (Page %s)\n", sprintf("%" level*2 "s", ""), title, page)
	        }
	    }'
}

Here is the awk code specifically isolated for syntax highlighting

/BookmarkTitle:/ {
  gsub("BookmarkTitle: ", "");
  title=$0
}
/BookmarkPageNumber:/ {
  gsub("BookmarkPageNumber: ", "");
  page=$0
}
/BookmarkLevel:/ {
  gsub("BookmarkLevel: ", "");
  level=$0;
  if (level <= max_level) {
	  printf("%s- [ ] %s (Page %s)\n", sprintf("%" level*2 "s", ""), title, page)
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment