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

Implementing an Extension #281

Closed
gamejerksnet opened this issue Feb 3, 2015 · 2 comments
Closed

Implementing an Extension #281

gamejerksnet opened this issue Feb 3, 2015 · 2 comments

Comments

@gamejerksnet
Copy link

So I followed the Wiki page and created an Extension class. What I don't know how to do is tie it into my Parsedown package telling it to use my new class while parsing text. Below is my new class:

<?php namespace App\Extensions;

use App\Repositories\User\UsersRepository;
use Parsedown;

class Extenstion extends Parsedown {

    /**
     * @var UsersRepository
     */
    private $usersRepository;

    function __construct(UsersRepository $usersRepository)
    {
        $this->InlineTypes['@'] []= 'UserTag';

        $this->inlineMarkerList .= '@';

        $this->usersRepository = $usersRepository;
    }

    protected function inlineUserTag($Excerpt)
    {
        if (preg_match('/^@([^\s]+)/', $Excerpt['text'], $matches))
        {
            $urlToUser  =   $this->usersRepository->findByDisplayName($matches[1])->url();

            return array(
                'extent' => strlen($matches[0]),
                'element' => array(
                    'name' => 'a',
                    'text' => '@' . $matches[1],
                    'attributes' => array(
                        'href' => $urlToUser,
                    ),
                ),
            );
        }
    }

}

The purpose of this extension is to turn @trinyx into a link to that user but keeping the same text.

@hkdobrev
Copy link
Contributor

hkdobrev commented Feb 3, 2015

Well, you should just use your class instead of Parsedown.

So instead of:

<?php

$Parsedown = new Parsedown();
$markup = $Parsedown->text('# Some Markdown Here');

you would use:

<?php

$Parsedown = new App\Extensions\Extension($userRepository);
$markup = $Parsedown->text('# Some Markdown Here with @usernames');

This is quite literally an extension. You just extend a class and use OOP. 🍻

@gamejerksnet
Copy link
Author

Oh lol I'm using Parsedown through Laravel and didn't realize it was like that. Thanks.

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