treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Markdown that doesn't strip data attributes

  • My goal on the forums is for you to be able to embed CodePens if you want.

    All that you need to do is:

      <pre class="codepen" data-type="result" data-href="idhuG" data-user="juliangarnier"><code></code></pre>
    

    But... Vanilla forums strips the data attributes which is what makes this work. It leaves classes and ID's alone, so there is hope.

    This is the Markdown file that it uses to process the text : https://gist.github.com/3692655

    Any ideas?

  • Sorry Chris I stink at php, but have you considered asking this question on StackOverflow?

  • So it just return something like this?

    <pre><code></code></pre>

    I think it have something to do with these

    function doCodeBlocks($text) {
        $text = preg_replace_callback('{
            (?:\n\n|\A\n?)
            (             # $1 = the code block -- one or more lines, starting with a space/tab
              (?>
              [ ]{'.$this->tab_width.'}  # Lines must start with a tab or a tab-width of spaces
              .*\n+
              )+
            )
            ((?=^[ ]{0,'.$this->tab_width.'}\S)|\Z) # Lookahead for non-space at line-start, or end of doc
          }xm',
          array(&$this, '_doCodeBlocks_callback'), $text);
    
        return $text;
      }
    function _doCodeBlocks_callback($matches) {
        $codeblock = $matches[1];
    
        $codeblock = $this->outdent($codeblock);
        $codeblock = htmlspecialchars($codeblock, ENT_NOQUOTES);
    
        # trim leading newlines and trailing newlines
        $codeblock = preg_replace('/\A\n+|\n+\z/', '', $codeblock);
    
        $codeblock = "<pre><code>$codeblock\n</code></pre>";
        return "\n\n".$this->hashBlock($codeblock)."\n\n";
      }
    

    Anyway, I agree with @Mottie, ask StackOverflow

  • Do you want to allow them to embed arbitrary HTML, or still have some limitations, but allow data-* attributes?

  • @TabAtkins I guess either way would be fine.

    I have no real problem with arbitrary HTML being posted - with the exception of security problems like script tags and such, but this forum software like strips that stuff.

    @Mottie @Mobilpadde I will ask there, thanks.

  • The problem lies in regexp used to parse the HTML blocks: search for (?=[\s"\'/a-zA-Z0-9]) and replace with (?=[\s"\'/a-zA-Z0-9-]) and it should work.

  • Here is the correct version http://pastebin.com/6fZkxNC6

  • @InerciaCreativa - Thanks for digging in! That appears twice in the file and I replaced them both but no dice.