变量的 Markdown 预格式化固定宽度代码块语言

Markdown preformatted fixed-width code block language for variables

如何为变量制作Markdown预格式化定宽代码块?
我是说代码块语言

示例:

`*${variable}*`      // *bold text*        ok
`_${variable}_`      // _italic text_      ok
````${variable}````  // ```pre-formatted fixed-width code block```   not work

如果我理解正确,你的问题归结为转义反引号。

在JavaScript中,当您需要在模板字符串中使用反引号字符时,您必须将其转义:

const stringWithBacktick = `\``;

因此,您的模板字符串可能如下所示:

const preformatted = `\`\`\`${variable}\`\`\``;
console.log(marked(preformatted));

或者,您可以像这样用三重反引号连接模板字符串:

const preformatted = `${variable}`;
console.log(marked("```\n" + preformatted + "\n```"));

或者,以更可重用的方式:

const preOpen = "```\n";
const preClose = "\n```";
const preformatted = `${preOpen}${variable}${preClose}`;
console.log(marked(preformatted));