0

我正在使用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>

这对我来说就像:

在此处输入图像描述

4

2 回答 2

2

你所拥有的是

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>

于 2022-02-23T16:28:53.447 回答
1

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

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>

看看它现在是怎样的等宽空间?

于 2022-02-23T16:27:51.637 回答