Browse Source

Markdown.pl: process atx-style before setext-style headers

Now that the recognition requirements for atx-style headers have
been tightened a bit, process them ahead of setext-style headers
to avoid having a horizontal rule immediately after one of the
atx-style headers being grabbed as a setext-style header.

Previously this:

##### My H5
---

Would end up as a single `<h2>##### My H5</h2>`.  Now it will more
correctly become `<h5>My H5</h5><hr />` instead.

This has been a longstanding problem that goes at least as far
back as version 1.0.1 and probably further.

Signed-off-by: Kyle J. McKay <mackyle@gmail.com>
master
Kyle J. McKay 5 years ago
parent
commit
e414dcb279
  1. 49
      Markdown.pl

49
Markdown.pl

@ -1401,6 +1401,30 @@ sub _DoHeaders {
$h1 = $h if $h ne "";
} : sub {};
# atx-style headers:
# # Header 1
# ## Header 2
# ## Header 2 with closing hashes ##
# ...
# ###### Header 6
#
$text =~ s{
^(\#{1,6}) # $1 = string of #'s
[ ]*
((?:(?:(?<![#])[^\s]|[^#\s]).*?)?) # $2 = Header text
[ ]*
\n+
}{
my $h_level = length($1);
my $h = $2;
$h =~ s/#+$//;
$h =~ s/\s+$//;
my $id = $h eq "" ? "" : _GetNewAnchorId($h);
&$geth1($h) if $h_level == 1 && $h ne "";
$id = " id=\"$id\"" if $id ne "";
"<h$h_level$id>" . _RunSpanGamut($h) . "</h$h_level>\n\n";
}egmx;
# Setext-style headers:
# Header 1
# ========
@ -1433,31 +1457,6 @@ sub _DoHeaders {
"<h3$id>" . _RunSpanGamut($h) . "</h3>\n\n";
}egmx;
# atx-style headers:
# # Header 1
# ## Header 2
# ## Header 2 with closing hashes ##
# ...
# ###### Header 6
#
$text =~ s{
^(\#{1,6}) # $1 = string of #'s
[ ]*
((?:(?:(?<![#])[^\s]|[^#\s]).*?)?) # $2 = Header text
[ ]*
\n+
}{
my $h_level = length($1);
my $h = $2;
$h =~ s/#+$//;
$h =~ s/\s+$//;
my $id = $h eq "" ? "" : _GetNewAnchorId($h);
&$geth1($h) if $h_level == 1 && $h ne "";
$id = " id=\"$id\"" if $id ne "";
"<h$h_level$id>" . _RunSpanGamut($h) . "</h$h_level>\n\n";
}egmx;
$opt{h1} = $h1 if defined($h1) && $h1 ne "";
return $text;
}

Loading…
Cancel
Save