How to create dynamic content within syntaxhighlig

2019-08-29 04:04发布

问题:

I want to display a property name based on user input and display this inside of SyntaxHighlighter. Another post says this is supposed to be easy.

JS

    $('#inputText').keyup(function () {
        var outputValue = $('#codeTemplate').html();//Take the output of codeTemplate
        $('#codeContent').html(outputValue);//Stick the contents of code template into codeContent

        var finalOutputValue = $('#codeContent').html();//Take the content of codeContent and insert it into the sample label
        $('.popover #sample').html(finalOutputValue);

        SyntaxHighlighter.highlight();
    });               

    SyntaxHighlighter.all();

Markup

<div style="display: none;">
    <label class="propertyName"></label>
    <label id="codeTemplate">
        <label class="propertyName"></label>
        //Not using Dynamic object and default Section (appSettings):
        var actual = new Configuration().Get("Chained.Property.key");

        //more code
    </label>            

    <pre id="codeContent" class="brush: csharp;">

    </pre>
</div>

<div id="popover-content" style="display: none">
    <label id="sample">

    </label>
</div>

This outputs plain text. As if SyntaxHighlighter never ran. I suspect that the issue has to do with the fact that <pre> doesn't exist after the page is rendered. However, updating

SyntaxHighlighter.config.tagName = "label";

along with pre to label did not work either.

回答1:

There were many problems that had to be overcome to get this to function. I feel this is best explained with code:

JS

<script>        
    $(function () {        
        $('#Key').popover({
            html: true,
            trigger: 'focus',
            position: 'top',
            content: function () {
                loadCodeData(true);
                console.log('content updated');
                var popover = $('#popover-content');
                return popover.html();//inserts the data into .popover-content (a new div with matching class name for the id)
            }
        });       

        $('#Key').keyup(function () {
            loadCodeData();
        });

        function loadCodeData(loadOriginal) {
            var userData = $('#Key').val();
            var codeTemplate = $('#codeTemplate').html();
            var tokenizedValue = codeTemplate.toString().replace('$$propertyNameToken', userData);
            $('#codeContent').html(tokenizedValue);
            $('#codeContent').attr('class', 'brush: csharp');//!IMPORTANT: re-append the class so SyntaxHighlighter will process the div again

            SyntaxHighlighter.highlight();

            var syntaxHighlightedResult = $('#codeContent').html();//Take the content of codeContent and insert it into the div                   

            var popover;
            if(loadOriginal)
                popover = $('#popover-content');//popover.content performs the update of the generated class for us so well we need to do is update the popover itself
            else {
                popover = $('.popover-content');//otherwise we have to update the dynamically generated popup ourselves.
            }

            popover.html(syntaxHighlightedResult);
        }

        SyntaxHighlighter.config.tagName = 'div';//override the default pre because pre gets converted to another tag on the client.  
        SyntaxHighlighter.all();
    });
</script>

Markup

<div style="display: none;">
    <label id="codeTemplate">
        //Not using Dynamic object and default Section (appSettings):
        var actual = new Configuration().Get("$$propertyNameToken");

        //Using a type argument:
        int actual = new Configuration().Get&lt;int>("asdf");

        //And then specifying a Section:
        var actual = new Configuration("SectionName").Get("test");

        //Using the Dynamic Object and default Section:
        var actual = new Configuration().NumberOfRetries();

        //Using a type argument:
        int actual = new Configuration().NumberOfRetries&lt;int>();

        //And then specifying a Section:
        var actual = new Configuration("SectionName").NumberOfRetries(); 
    </label>            

    <div id="codeContent" class="brush: csharp;">

    </div>
</div>

<div id="popover-content" style="display: none">

</div>