4

Google Code Prettify is working fine, but when I am using the Markdown content and show the content from the database, Prettify isn't working properly (link of live result).

This is what I am doing:

  • Getting content with

    <textarea name="article_content" id="wmd-input" class="wmd-panel"></textarea>

  • Then store output after (I am using PDO)

    Markdown($_POST['article_content'])

But in the result, the part I wrote in the code section is working fine if I don't use Markdown, but when I get the content from the textarea and use Markdown, it's not working.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user007
  • 71
  • 9

2 Answers2

2

Look at the HTML code. You missed the class="prettyprint" off the pre tag.

<pre><code>try {
    $db-&gt;beginTransaction();

    $db-&gt;exec("SOME QUERY");

    $stmt = $db-&gt;prepare("SOME OTHER QUERY?");
    $stmt-&gt;execute(array($value));

    $stmt = $db-&gt;prepare("YET ANOTHER QUERY??");
    $stmt-&gt;execute(array($value2, $value3));

    $db-&gt;commit();
} catch(PDOException $ex) {
    //Something went wrong rollback!
    $db-&gt;rollBack();
    echo $ex-&gt;getMessage();
}
</code></pre>

should be

<pre class="prettyprint"><code>try {
    $db-&gt;beginTransaction();

    $db-&gt;exec("SOME QUERY");

    $stmt = $db-&gt;prepare("SOME OTHER QUERY?");
    $stmt-&gt;execute(array($value));

    $stmt = $db-&gt;prepare("YET ANOTHER QUERY??");
    $stmt-&gt;execute(array($value2, $value3));

    $db-&gt;commit();
} catch(PDOException $ex) {
    //Something went wrong rollback!
    $db-&gt;rollBack();
    echo $ex-&gt;getMessage();
}
</code></pre>

To fix the problem of having <pre> auto generated, you can try this:

$newcontent = str_replace('<pre>', '<pre class="prettyprint">', $_POST['article_content']);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
HenchHacker
  • 1,616
  • 1
  • 10
  • 16
  • love you buddy its working but where i need to add this in wmd js since it add the `
    ` automatically
    – user007 Nov 24 '12 at 12:34
  • Add the class using javascript after the tags have been added? You can always add a wrapper tag and put the class on that (but not sure if prettyprint was done on pre.prettyprint or .prettyprint so you will have to try it and see). – HenchHacker Nov 24 '12 at 13:28
  • could you add this `$cont = $_POST['article_content']; $newcontent= (str_replace( '
    ', '
    ',$cont));` in answer so i can accept your answer
    – user007 Nov 24 '12 at 13:32
  • Is the example code actually formatted correctly? Is ">" literal? If it is, can you [explain it](https://stackoverflow.com/posts/13541174/edit) in your answer? And if not, format it? (But ***without*** "Edit:", "Update:", or similar - the answer should appear as if it was written today.) – Peter Mortensen Aug 14 '21 at 10:46
0

For auto generated <pre> you can also use:

$('pre').addClass('prettyprint');
suhailvs
  • 20,182
  • 14
  • 100
  • 98