showdownjs / showdown

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

HighlightJS integration ignores nohighlight and plaintext type #826

Closed DoubleF3lix closed 2 years ago

DoubleF3lix commented 4 years ago

Using the markdown below and

```plaintext
.minecraft
  saves
    <WORLD NAME>
      datapacks
        <datapack name>
          pack.mcmeta
          data
            <namespace>
              advancements
              dimension
              dimension_type
              functions
              loot_tables
              predicates
              recipes
              structures
              tags
                blocks
                entity_types
                fluids
                functions
                items
              worldgen
                biome
                configured_carver
                configured_feature
                configured_structure_feature
                configured_surface_builder
                noise_settings
                processor_list
                template_pool   
            minecraft
              tags
                functions
                  load.json
                  tick.json 
``` (ignore)

And this JavaScript to load the file:

async function displayMarkdownContent(name) {
    showdown.extension("codehighlight", function() {
        function htmlunencode(text) {
          return (
            text
              .replace(/&amp;/g, "&")
              .replace(/&lt;/g, "<")
              .replace(/&gt;/g, ">")
            );
        }
        return [
          {
            type: 'output',
            filter: function (text, converter, options) {
              // use new shodown's regexp engine to conditionally parse codeblocks
              var left  = "<pre><code\\b[^>]*>",
                  right = "</code></pre>",
                  flags = "g",
                  replacement = function (wholeMatch, match, left, right) {
                    // unescape match to prevent double escaping
                    match = htmlunencode(match);
                    return left + hljs.highlightAuto(match).value + right;
                  };
              return showdown.helper.replaceRecursiveRegExp(text, replacement, left, right, flags);
            }
          }
        ];
    });
    const markdownText = await fetch(`/java_datapack_tutorial/guides/${name}.md`).then(r => r.text());
    let markdownOutput = new showdown.Converter({extensions: ["codehighlight"]}).makeHtml(markdownText);
    document.getElementById(`display-${name}-markdown`).innerHTML = markdownOutput;
    console.log(`Displaying /java_datapack_tutorial/guides/${name}.md`);
}

I expected there to be no syntax highlighting, but instead it looks like this: image

The same issue happens if the language is set to nohighlight

DoubleF3lix commented 2 years ago

Not sure what I changed, but this doesn't happen anymore.