showdownjs / showdown

A bidirectional Markdown to HTML to Markdown converter written in Javascript
http://www.showdownjs.com/
MIT License
14.3k stars 1.57k forks source link

Text From Database With Showdown #305

Closed tasmanwebsolutions closed 7 years ago

tasmanwebsolutions commented 7 years ago

I am trying to convert my text from my database column

<script type="text/javascript">
var converter = new showdown.Converter(),
    text = '<?php echo $preview;?>',
    target = document.getElementById('output'),
    html = converter.makeHtml(text);
    target.innerHTML = html;
</script>

I echo text to view but says error SyntaxError: unterminated string literal

What do I need to do for text from database to be able to work with showdown

My text that I insert looks like this example below

Hello

How are you

jjjjjjjj
tivie commented 7 years ago

This is a question out of scope for this issue tracker since it's PHP related and not at all related to showdown (you can try www.stackoverflow.com, for instance).

However, the problem is that your string has linebreaks.

So the resulting code is something like this:

<script type="text/javascript">
var converter = new showdown.Converter(),
    text = '#Hello

How are you
    jjjjjjjj',
    target = document.getElementById('output'),
    html = converter.makeHtml(text);
    target.innerHTML = html;
</script>

which is invalid javascript.

obedm503 commented 7 years ago

@wolfgang1983 maybe try substituting the single quotes around the php code with backticks, which support multiline text.

<script type="text/javascript">
var converter = new showdown.Converter(),
    text = `<?php echo $preview;?>`,
    target = document.getElementById('output'),
    html = converter.makeHtml(text);
    target.innerHTML = html;
</script>

for more support from template strings check this

tasmanwebsolutions commented 7 years ago

I have now had to do couple changes on my insert database end with codeigniter


public function insert() {
$data = array(
'question' => $this->db->escape(trim(str_replace('\n', PHP_EOL, $this->input->post('html'))))
);

$this->db->where('question_id', '1');
$this->db->update('question', $data);
}

Works fine now.