src/Ciconia/Extension/Core/LinkExtension.php
public function processReferencedLink(Text $text, array $options = array())
    {
        if (!$text->contains('[')) {
            return;
        }

        /** @noinspection PhpUnusedParameterInspection */
        $text->replace('{
            #(                   # wrap whole match in $1
              \[
                (' . $this->getNestedBrackets() . ')    # link text = $2
              \]

              [ ]?              # one optional space
              (?:\n[ ]*)?       # one optional newline followed by spaces

              \[
                (.*?)       # id = $3
              \]
            #)
        }xs', function (Text $whole, Text $linkText, Text $id = null) use ($options) {
            if (is_null($id) || (string) $id == '') {
                $id = new Text($linkText);
            }

            $id->lower();

            if ($this->markdown->getUrlRegistry()->exists($id)) {
                $url = new Text($this->markdown->getUrlRegistry()->get($id));
                $url->escapeHtml()->replace('/(?<!\\\\)_/', '\\\\_');
                $this->markdown->emit('escape.special_chars', [$url]);

                $linkOptions = [
                    'href' => $url->getString(),
                ];

                if ($this->markdown->getTitleRegistry()->exists($id)) {
                    $title = new Text($this->markdown->getTitleRegistry()->get($id));
                    $linkOptions['title'] = $title->escapeHtml()->getString();
                }

                return $this->getRenderer()->renderLink($linkText->getString(), $linkOptions);
            } else {
                if ($options['strict']) {
                    throw new SyntaxError(
                        sprintf('Unable to find id "%s" in Reference-style link', $id),
                        $this, $whole, $this->markdown
                    );
                }

                return $whole;
            }
        });
    }
public function processReferencedLink(Text $text, array $options = array())
    {
        if (!$text->contains('[')) {
            return;
        }

        /** @noinspection PhpUnusedParameterInspection */
        $text->replace('{
            #(                   # wrap whole match in $1
              \[
                (' . $this->getNestedBrackets() . ')    # link text = $2
              \]

              [ ]?              # one optional space
              (?:\n[ ]*)?       # one optional newline followed by spaces

              \[
                (.*?)       # id = $3
              \]
            #)
        }xs', function (Text $whole, Text $linkText, Text $id = null) use ($options) {
            if (is_null($id) || (string) $id == '') {
                $id = new Text($linkText);
            }

            $id->lower();

            if ($this->markdown->getUrlRegistry()->exists($id)) {
                $url = new Text($this->markdown->getUrlRegistry()->get($id));
                $url->escapeHtml()->replace('/(?<!\\\\)_/', '\\\\_');
                $this->markdown->emit('escape.special_chars', [$url]);

                $linkOptions = [
                    'href' => $url->getString(),
                ];

                if ($this->markdown->getTitleRegistry()->exists($id)) {
                    $title = new Text($this->markdown->getTitleRegistry()->get($id));
                    $linkOptions['title'] = $title->escapeHtml()->getString();
                }

                return $this->getRenderer()->renderLink($linkText->getString(), $linkOptions);
            } else {
                if ($options['strict']) {
                    throw new SyntaxError(
                        sprintf('Unable to find id "%s" in Reference-style link', $id),
                        $this, $whole, $this->markdown
                    );
                }

                return $whole;
            }
        });
    }
Handle reference-style links: [link text] [id]

@param Text  $text
@param array $options