dotnet / interactive

.NET Interactive combines the power of .NET with many other languages to create notebooks, REPLs, and embedded coding experiences. Share code, explore data, write, and learn across your apps in ways you couldn't before.
MIT License
2.8k stars 374 forks source link

How to render HTML contained in a C# string #3499

Closed rzippo closed 3 months ago

rzippo commented 3 months ago

I have C# code that creates a string with HTML code, like this one

<!DOCTYPE html>
<html>
<head>
    <title>MathJax Test</title>
    <script src='https://polyfill.io/v3/polyfill.min.js?features=es6'></script>
    <script id='MathJax-script' async src='https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js'></script>
</head>
<body>

    <div id='output-0'></div>
    <script>
        document.getElementById('output-0').innerHTML = '\\( \\alpha + \\beta \\)';
        MathJax.typeset();
    </script>
</body>
</html>

If this HTML is put in its own HTML cell, it renders correctly like this immagine

But if I try to render the C# string containing the HTML, I only get the text. immagine

How do I get the string to be interpreted and rendered as HTML?

jonsequitur commented 3 months ago

This is happening because the Display method will HTML encode the string.

When you know that the string is already valid HTML and you don't want to encode it, you can use DisplayAs("text/html"):

image

rzippo commented 3 months ago

It works, thanks!