0

I'm using marked.js to render code that we want to store (ultimately as JSON). However, the I can't get triple-back-ticked convention to render correctly. I'm sure user error on my part but how would I to get the following to work?

<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
<div id="content"></div>
<script>
let str = marked.parse("here is the body of arguments ```\n\nclass User\n  def say_my_name\n  puts 'my name'\n  end\nend```");
document.getElementById('content').innerHTML = str;
</script>

which renders for me like:

enter image description here

2 Answers 2

2

What you have is

here is the body of arguments ```

class User
  def say_my_name
  puts 'my name'
  end
end```

But that is not valid. The code fences should always be on a new line:

let str = marked.parse("here is the body of arguments \n```\n\nclass User\n  def say_my_name\n  puts 'my name'\n  end\nend\n```");
document.getElementById('content').innerHTML = str;
<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
<div id="content"></div>

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

Comments

1

You've got the newlines mixed up around the first trio of backticks:

let str = marked.parse("here is the body of arguments\n ```\nclass User\n  def say_my_name\n  puts 'my name'\n  end\nend```");

document.getElementById('content').innerHTML = str;
<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
<div id="content"></div>

See how it's monospace now?

1 Comment

You'll probably want another newline before the closing backticks too otherwise it all gets rendered on a single line ignoring the newlines in the code block

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.