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
)? – Felipe Lema Jul 12 '18 at 21:49The end goal is to write tests to detect these cases, and trigger at least a warning when running travis in GitHub. So regex for detection would be preferred.
– Øistein Søvik Jul 12 '18 at 22:05M-x re-builder
otherwise how can you tell if you're using appropriate syntax? See also https://emacs.stackexchange.com/q/5568/454 – phils Apr 09 '19 at 04:23evil
tag, as the OP's answer to their own question was entirely evil-centric. – phils Aug 01 '20 at 08:32