I would like to enable code folding for markdown in textmate.
Specifically, I would like any headings (text preceded by one or more '#' characters) to be the line that would toggle blocks between it and the next heading and/or horizontal rule. Example:
# Level 1 Heading - I'd like the block below to fold
The text I'd like to fold away
## Level 2 Heading
More text...
BBEdit seems to do this just fine for markdown. TextMate supports code folding just fine for languages that denote blocks with paired (and identically indented) curly braces.
I know TextMate offers two variables you can set in the language grammar file, but I'd need more talent with regular expressions than I possess to figure it out. Following is what currently resides in the markdown language grammar for the code folding settings:
foldingStartMarker = '(?x)
(<(?i:head|body|table|thead|tbody|tfoot|tr|div|select|fieldset|style|script|ul|ol|form|dl)\b.*?>
|<!--(?!.*-->)
|\{\s*($|\?>\s*$|//|/\*(.*\*/\s*$|(?!.*?\*/)))
)';
foldingStopMarker = '(?x)
(</(?i:head|body|table|thead|tbody|tfoot|tr|div|select|fieldset|style|script|ul|ol|form|dl)>
|^\s*-->
|(^|\s)\}
)';
Any help greatly appreciated!
TextMate flat-out can't do this.
TextMate's language parser reads files on a line-by-line basis. Which means that in order for code to be folded there needs to be a start marker AND an end marker. Like
<ul>and</ul>. In this case, since the end of a section is not marked by anything other than the start of the next section, the line-by-line parser cannot go back and say an empty line should mark the end of a code folded block.Does that make sense?