Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Suggestion] Headers are always on a new line #542

Closed
bkuhl opened this issue Nov 23, 2017 · 1 comment
Closed

[Feature Suggestion] Headers are always on a new line #542

bkuhl opened this issue Nov 23, 2017 · 1 comment

Comments

@bkuhl
Copy link

bkuhl commented Nov 23, 2017

When using this library I ran into challenges when parsing web page's HTML where headers were not always on a new line. I wrote a regex to adjust this so that it was always parsed correctly. I'm posting this issue here in case someone wants to adapt it to this project.

    public function headersNeverAtEndOfLine(string $markdown) : string
    {
        // any non-whitespace charcater
        $regex = "([\S])";

        // don't match pound signs that are part of a markdown link, are already next to a newline
        // or are part of a larger markdown header, such as "##### My Header"
        $regex .= "[^\n(#]";

        // match h1-h5 equivalents
        $regex .= "(#{1,5})";

        return trim(preg_replace("/".$regex."/", "$1".PHP_EOL."$2", $markdown));
    }

And then the test to show what this is actually doing:

    /** @test */
    public function verifyHeadersNeverAtEndOfLine()
    {
        $original = <<<EOF
This is a quick # Header test to ##See how
### Headers are #### Never at the end of an #####Existing line
EOF;
        $expected = <<<EOF
This is a quick
# Header test to
##See how
### Headers are
#### Never at the end of an
#####Existing line
EOF;

        $this->assertEquals($expected, $this->parsesMarkdown->headersNeverAtEndOfLine($original));
    }
@aidantwoods
Copy link
Collaborator

Hey there! I think we'll probably stick with CommonMark here – i.e. require atx headings to start on a new line for compatibility, but thank you for the suggestion anyway :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants