I am in the process of formatting a couple of hundred markdown files and am trying to automate that process. I am having two problems, but I guess I can ask two questions ;)
Goal
I am trying to achieve the following:
- Every main title starting with # should have two newlines above and one newline below.
- Every subtitle starting with two or more # should have one newline above and one newline below.
Example:
More text here
# The main title
## Another title
## The third title
More text
# The second main title
more text here
## Second subsection
# Third title
more text
Should be formatted to
More text here
# The main title
## Another title
## The third title
More text
# The second main title
more text here
## Second subsection
# Third title
more text
Attempt
I am not sure if this is best suited for regex or a well defined macro?
My attempt at regex has so far been
[#]+.*\n[^\n]
This is at least able to identify every title that is missing a newline underneath.
However, I have no idea how I would use the expression above to insert a newline after the title. Any help would be appreciated.
man perlre
)?M-x re-builder
otherwise how can you tell if you're using appropriate syntax? See also emacs.stackexchange.com/q/5568/454