#heading Not working


Can’t tell if @codinghorror meant to come back to the December post – but if you did, I like @riking’s idea too! Save novices’ headaches with hashtags, but maximize compatibility with other parsers for ## and “lower”.

That said, it wasn’t that tough to pull off @riking’s fix outside of the parser – in my case, CommonMark.NET (the below isn’t horribly well tested, but you get the point).

src = System.Text.RegularExpressions.Regex.Replace(src, 
    @"^#{2,6}\S|^#\S[^\n\r]*#[ \t]*",
    delegate (System.Text.RegularExpressions.Match match)
        string strMatch = match.ToString();
        string hashes = new string(strMatch.TakeWhile(c => c.Equals('#')).ToArray());

        return (hashes
            + " "
            + strMatch.Substring(hashes.Length)).TrimEnd('#');

Apologies for not putting on the sad tragedy of micro-optimization theater, and I don’t necessarily love pulling a full string lint, but it’s pretty non-destructive to do your own cleaning run before handing off to the parser.

The first half of the RegEx (^#{2,6}\S) catches ##Two to six hashes, no space and the second half (^#\S[^\n\r]*#[ \t]*) grabs #no space, line ends with hash #. Or that’s the intention.

Likely some nasty edge cases, but that’s the idea. The TrimEnd should be fine if you’re simply rendering to html. Note: .NET seems wonky with multiline regexes, so these aren’t exactly what I expected.

1 Like