Forum

Thread tagged as: Question

Markdown in text fields

Is it possible to parse text fields (not textarea fields) using Markdown? If not, is there any chance of this getting added?

David Newton

David Newton 0 points

  • 7 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

That would turn a text field into a paragraph, which would be weird, wouldn't it?

But yeah, you could do that.

<?php

class PerchFieldType_markdownytext extends PerchFieldType
{  
    public function get_raw($post=false, $Item=false)
    {
        if ($post===false) {
            $post = $_POST;
        }

        $id = $this->Tag->id();
        if (isset($post[$id])) {
            $raw = trim($post[$id]);

            $raw = stripslashes($raw);
            $value = $raw;

            // Fix markdown blockquote syntax - > gets encoded.
            $value = preg_replace('/[\n\r]&gt;\s/', "\n> ", $value);

            // Fix autolink syntax
            $value = preg_replace('#&lt;(http[a-zA-Z0-9-\.\/:]*)&gt;#', "<$1>", $value);

            $Markdown = new ParsedownExtra();
            $value = $Markdown->text($value);

            if (!class_exists('\\Michelf\\SmartyPants', false) && class_exists('SmartyPants', true)) { 
                // sneaky autoloading hack 
            }

            $SmartyPants = new \Michelf\SmartyPants;
            $value = $SmartyPants->transform($value);
            if (PERCH_HTML_ENTITIES==false) {
                $value = html_entity_decode($value, ENT_NOQUOTES, 'UTF-8');    
            }     

            $store = array(
                'raw' => $raw,
                'processed' => $value
            );

            $this->raw_item = $store;

            return $this->raw_item;
        }

        return null;
    }

    public function get_processed($raw=false)
    {
        if ($raw===false) $raw = $this->get_raw();

        $value = $raw;

        if (is_array($value)) {
            if (isset($value['processed'])) {
                $this->processed_output_is_markup = true;
                return $value['processed'];
            }

            if (isset($value['raw'])) {
                return $value['raw'];
            }
        }else{
            if (!strpos($this->Tag->id(),'HTML')) {
                $value = $this->get_raw(array($this->Tag->id()=>$value));
                return $value['processed'];    
            }
        }

        return $value;
    }

    public function get_search_text($raw=false)
    {
        if ($raw===false) $raw = $this->get_raw();

        if (is_array($raw)) {

            if (isset($raw['processed'])) {
                return strip_tags($raw['processed']);
            }

            if (isset($raw['raw'])) {
                return $raw['raw'];
            }
        }

        return $raw;
    }

    public function get_index($raw=false)
    {
        if ($raw===false) $raw = $this->get_raw();       
        $id = $this->Tag->id();
        $out = array();
        $out[] = array('key'=>$id, 'value'=>trim($this->get_search_text($raw)));
        return $out;
    }
}

Hmm, good point about turning it into a paragraph. I was thinking more of the inline-level markdown for formatting, not necessarily the block-level.

Thanks for this, I'll think about whether it's worth it.

Drew McLellan

Drew McLellan 2638 points
Perch Support

It looks like Parsedown has a line formatting syntax. So this line:

$value = $Markdown->text($value);

would be

$value = $Markdown->line($value);

Fantastic, thanks Drew!