EzDevInfo.com

parsedown

Better Markdown Parser in PHP Better Markdown Parser in PHP

PHP Parsedown get all headings

I use Parsedown for transforming my Markdown into HTML like:

$pd->text('# My First heading');

In want automatically use the first site heading (h1) as an entry for a custom menu. I already had a look into the source, but couldn't figure out a good way to get all headings.

Perhaps there is something in elements function. Does anyone has an idea to get all or at least the first heading.


Source: (StackOverflow)

PHP file_get_contents not getting all content

I'm having a strange problem with the file_get_contents function of PHP, I'm currently creating an application which reads .md files. I have created a command which builds the .md files in a single html file which works perfectly. It puts all the html in the right files.

But if you are writing the .md file it's a pain to build the html to see what it looks like after every edit. So I created a php script which can be accessed by a browser and it'll do exactly the same as the command but instead of putting the html content in files it'll echo it out on your screen. But if you call this script it's not reading the entire file and it cuts off the last part of the html...

The .md File

### Test Readme for Category One

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam porttitor felis enim, molestie tincidunt erat feugiat a. 
Donec quis odio quis quam blandit convallis. Aenean eu lacus risus. Nulla congue non dui eu convallis. 

Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Integer ac ante laoreet, placerat diam in, 
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam porttitor felis enim, molestie tincidunt erat feugiat a. 

Donec quis odio quis quam blandit convallis. Aenean eu lacus risus. Nulla congue non dui eu convallis. 
Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Integer ac ante laoreet, placerat diam in, 
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam porttitor felis enim, molestie tincidunt erat feugiat a.

Donec quis odio quis quam blandit convallis. Aenean eu lacus risus. Nulla congue non dui eu convallis.                                                                                                                                  
Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Integer ac ante laoreet, placerat diam in, Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam porttitor felis enim, molestie tincidunt erat feugiat a. 
Donec quis odio quis quam blandit convallis. Aenean eu lacus risus. Nulla congue non dui eu convallis. 

The output

<h3>Test Readme for Category One</h3>

<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam porttitor felis enim, molestie tincidunt erat feugiat a. 
Donec quis odio quis quam blandit convallis. Aenean eu lacus risus. Nulla congue non dui eu convallis.</p>

<p>Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Integer ac ante laoreet, placerat diam in, 
Lorem ipsum dolor sit amet, consecte

As you can see it cuts part of the md file. I use this library to convert the md content to HTML https://github.com/erusev/parsedown-extra

Does anyone has a thought what it could be?

Thanks!

=== EDIT ===

I debugged it with this file: https://raw.githubusercontent.com/SeBuDesign/Write-Down/08be2a10281d99768f1d73ad7916b0bbc37903ea/docs/00_Category_One/00_Read_Me.md

Stored the contents of that file in a local file and perform a file_get_contents of that local file. It's the first file it processes, the following it the outcome:

### Test Readme Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam porttitor felis enim, molestie tincidunt erat feugiat a. Donec quis odio quis quam blandit convallis. Aenean eu lacus risus. Nulla congue non dui eu convallis. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Integer ac ante laoreet, placerat diam in, hendrerit augue. Mauris pharetra, dui sed facilisis condimentum, ex lorem lobortis ante, ut ultrices nibh felis sit amet nibh. Pellentesque eget interdum nibh. Aenean interdum felis at tellus bibendum aliquet. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus velit augue, interdum id metus id, iaculis porttitor purus.

For the people who want to see the function that gets the content:

/**
     * Parses the md files of the given path
     *
     * @return string
     */
    public function parseItems()
    {
        $iterate = $this->getRecursiveIterator();

        $html = "";
        foreach ($iterate as $fullPath => $fileInfo) {
            /** @var $fileInfo SplFileInfo */

            // Check if it's a directory or a .md file
            if ($fileInfo->getExtension() == 'md') {

                $fileName = StringHelper::removeMdExtension(StringHelper::removeOrderChars($fileInfo->getFilename()));

                $id = "";
                for ($depth = $iterate->getDepth() - 1; $depth >= 0; $depth--) {
                    $id .= StringHelper::removeOrderChars($iterate->getSubIterator($depth)->current()->getFilename())."/";
                }
                $id .= $fileName;

                $headerName = str_replace("_", " ", $fileName);
                $html .= "<h1 id='{$id}'>{$headerName}</h1>";

                $mdContent = file_get_contents($fileInfo->getRealPath());
                print_r($mdContent);die();
                $parseDown = new ParsedownExtra();
                $html .= $parseDown->text($mdContent);
            }
        }

        return $html;
}

Source: (StackOverflow)

Advertisements

Parsedown, add sub/superscript

I am trying to add sub/superscript to Parsedown.

Parsedown's functions seem like a jungle to me. I've been trying to understand it but have been unable to decipher it.

Turning ~text~ into <sub>text</sub> seems to be more of a challenge than I'd thought.

Wrapping my head around the structure of his code, is just something that I can't and any help would be extremely appreciated.


Source: (StackOverflow)

Extend class in PHP

I am trying to extend a class:

class CustomParsedown extends Parsedown {
    protected function blockComment($Line) { return; }
    protected function blockCommentContinue($Line, array $Block) { return; }
    protected function blockHeader($Line) { return; }
    protected function blockSetextHeader($Line, array $Block = NULL) { return; }
}

function markdown($markdown) {
    return CustomParsedown::instance()->setMarkupEscaped(true)->text($markdown);
}

If I run markdown() with markdown from another page, the changes in the code do not go into effect. For example, I can still create a heading. Am I extending the class correctly?


Source: (StackOverflow)

Literate PHP to test examples in README

I'd like to include the examples from my README.md in my testsuite. So I can be sure that all examples still work and I don't need to write the same examples again as PHPUnit test cases.

Has anybody already written support for this?


Source: (StackOverflow)

How to add custom text on Markdown' Parsedown PHP parser

I have this Markdown text:

![alt](/md.png "title")

And after converting it to HTML with Markdown' Parsedown PHP parser (http://parsedown.org/) I get:

<p><img alt="alt" src="/md.png" title="title" /></p>

I need to add certain values before and after the conversion as well as inside the code such that the desired result would be like

<a rel='nofollow' href='#' class='some_class'>
  <p><img class='some_classy_image' alt="alt" src="/md.png" title="title" /></p>
</a>

How can I achieve this in a easy way? I'm open to an answer using Parsedown or another PHP parser out there that exposes this functions in a easier way.


Source: (StackOverflow)

Laravel 5 Parsedown returning plain html tags to browser

I've installed parsedown (am using laravel 5) for parsing markdown and when I run it it is changing the markdown to html but the browser is plainly showing the parsed markdown instead of applying those particular styles for example when I run the following

{{Parsedown::instance()->text('Hello _Parsedown_!')}}

I expect when I run it in the browser: Hello Parsedown!

but instead am getting the following: <p>Hello <em>Parsedown</em>!</p> and when I view the browsers page source I get the following &lt;p&gt;Hello &lt;em&gt;Parsedown&lt;/em&gt;!&lt;/p&gt; What could be the problem?


Source: (StackOverflow)

Strange behaviour when rendering vtiger text fields with markdown: Wrong indent of item lists

I experienced a very strange behaviour: I wrote a custom function for PDF-Maker that sends the text of a custom field through Markdown (I used Parsedown). (The idea is that we can easily do some simple formatting in text fields in vtiger where we don't have any advanced editor.)

I have the following list in the text field:

* a
* b
* c

The output of Parsedown causes all items from the second line on to be rendered as a nested list:

<ul>
<li>a
<ul>
<li>b</li>
<li>c</li>
</ul></li>
</ul>
  • a
    • b
    • c

Of course, if I paste the text direktly to parsedown, it gets rendered correctly:

<ul>
<li>a</li>
<li>b</li>
<li>c</li>
</ul>

What happens here? Any hint ...?


Source: (StackOverflow)