0

I want to use Markdown to preview to the user how a form field on a twig file will be styled using javascript.

Basically what I'm trying to do, is :

// This function is used to show the Description Preview 
$( "#server_new_profile_description" ).on('input propertychange', function() {
    // This should make the $(this).val() formated in markdown
    var descrString = $(this).val();
    $("#descriptionPrev div").html("{{- "+descrString+"|markdown|raw -}}");
});

What I get: If the input is *ABC*

the output will be: {{*ABC*|markdown|raw}}.

instead of ABC

Can anyone get me through this?

2 Answers 2

3

Twig is rendered server-side, which means it gets executed only once, when you request the page. So you will have to use javascript functions to achieve the markdown effects. This means you will need a markdown parser, for example: https://github.com/evilstreak/markdown-js

Sign up to request clarification or add additional context in comments.

Comments

0

I created HTML that looks like this.

<div id="server_new_profile_description"> 
<input type="text">
</div>

<div id="descriptionPrev">
</div>

And I would achieve what you are looking to do like this.

$( "#server_new_profile_description input" ).change(function() {
    var descrString = $(this).val();
    $("#descriptionPrev").html("{{*"+descrString+"*|markdown|raw -}}");
});

It grabs the input element that is inside the div with the server_new_profile_description ID and won change it grabs the value assigning it to a variable. That variable is then placed inside of the DIV with the ID of descriptionPrev and concatenated with the two string parts

1 Comment

Have you tested this? It is not working at all. I get exactly the same result but with stars this time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.