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

EntryParser.php calls buggy YamlFrontMatter::parse() from spatie #3

Open
eklausme opened this issue Jul 12, 2021 · 0 comments
Open

Comments

@eklausme
Copy link

eklausme commented Jul 12, 2021

According The package wrongly parses a Markdown code inside a Markdown post the routine parse() in YamlFrontMatter does not handle Markdown code correctly.

I corrected EntryParser.php. Here is the relevant, corrected code in the class:

    public function parseEntry($filePath)
    {
        $content = file_get_contents($filePath);

        $n3dash = 0;    // count number of triple dashes
        $pos1 = 0;
        $pos2 = 0;
        $len = strlen($content);

        for ($pos=0;; $pos+=3) {
                $pos = strpos($content,"---",$pos);
                if ($pos === false) {  // no pair of triple dashes at all
                        $data = [];
                        $data['content_raw'] = $content;
                        return $data;
                }
                // Are we at end or is next character white space?
                if ( $pos + 3 == $len  ||  ctype_space(substr($content,$pos+3,1)) ) {
                        if ($n3dash == 0  &&  ($pos == 0 || $pos > 0 && substr($content,$pos-1,1)=="\n")) {
                                $n3dash = 1;    // found first triple dash
                                $pos1 = $pos + 3;
                        } else if ($n3dash == 1  &&  substr($content,$pos-1,1) == "\n") {
                                // found 2nd properly enclosed triple dash
                                $n3dash = 2; $pos2 = $pos + 3; break;
                        }
                }
        }
        $matter = substr($content,$pos1,$pos2-3-$pos1);
        $body = substr($content,$pos2);
        $matter = Yaml::parse($matter);

        // $object = EntryParser::parse(file_get_contents($filePath));
        //$data = $object->matter();
        //$data['content_raw'] = $object->body();

        $data = $matter;
        $data['content_raw'] = $body;

        return $data;
    }

With that code change triple dashes in Markdown code are handled correctly. See, for example, Saaze docs: Entries.

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

1 participant