Skip to content

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

@eklausme

Description

@eklausme

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions