KM Macro for creating a TOC from text headings

Here is another potentially useful Keyboard Maestro macro. This time for creating a TOC from headings within the text. You have to be in editor mode, with the cursor in the text. Once triggered, it will select all the text, copy it, parse it and return a structured TOC in a display window and to the clipboard.

Example:


# Markdown
#LoremIpsum

[[Lorem Ipsum]]

# H1
## H2
### H3
#### H4
##### H5
###### H6
##### H5
#### H4
### H3
## H2
# H1

The macro will return:


## Content
- [[#Markdown]]
- [[#H1]]
	- [[#H2]]
		- [[#H3]]
			- [[#H4]]
				- [[#H5]]
					- [[#H6]]
				- [[#H5]]
			- [[#H4]]
		- [[#H3]]
	- [[#H2]]
- [[#H1]]

[[#Content]]

MacOS only.

4 Likes

Clever! Thanks!

1 Like

For long documents :wink:

Forgot to mention that the [[#Content]] is for going back to the contents section. Being able to go up and down via keyboard shortcuts is pretty nice.

@bernardo_v: I installed KM today, and imported this macro. Very neat for when a document is exported to e.g. PDF.

[[#Content]] is at the end of the TOC list, so when you say “it is for going back to the contents section” I assume you mean for jumping from the bottom of the list back to the top of that list. Is that right or am I missing something?

My apology of this is a stupid question.

Precisely! I was using it for jumping around quickly with the keyboard.