How to treat single newline as real line break in PHP Markdown?

前端 未结 4 1704
说谎
说谎 2021-02-06 01:44

I was reading http://github.github.com/github-flavored-markdown/

I would like to implement that \"Newline modification\" in PHP Markdown:

Best I could think of

4条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-06 02:26

    As an ad-hoc script you can just run this on your string before running the markdown script

    $text = preg_replace_callback("/^[\w\<][^\n]*\n+/msU",function($x){
    $x = $x[0];
    $x = preg_match("/\n{2}/",$x,$match)? $x: trim($x)."  \r\n";
    return $x;
    },$text);
    
    $my_html = Markdown($text);
    

    Based on the github flavored markdown

    text.gsub!(/^[\w\<][^\n]*\n+/) do |x|
        x =~ /\n{2}/ ? x : (x.strip!; x << "  \n")
    end
    

    P.S. I'm not the best at regex and I don't know what programming language github uses so I improvised

提交回复
热议问题