如何存储使用带 marked.js 的三重引号反引号字符串

How to store use triple-quoted backtick strings with marked.js

我正在使用 marked.js 呈现我们要存储的代码(最终为 JSON)。但是,我无法获得正确呈现的三重回勾约定。我确定是我的用户错误,但我将如何使以下内容起作用?

<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>

这对我来说是这样的:

您在前三个反引号周围混淆了换行符:

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>

看到它现在是等宽的了吗?

你拥有的是

here is the body of arguments ```

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

但这是无效的。代码围栏应始终在新行上:

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>