67

<code />你知道JSDoc中是否有某种标签吗?我需要在我的文档中添加如下代码:

/**
 * This function does something see example below:
 *
 * var x = foo("test"); //it will show "test" message
 *
 * @param {string} str: string argument that will be shown in message
 */
function foo(str)
{
   alert(str);
}

我需要 JSDoc 将注释中的代码显示为代码(如果没有突出显示语法,至少像预先格式化或带有灰色背景的东西)。

4

6 回答 6

61

@example http://code.google.com/p/jsdoc-toolkit/wiki/TagExample

/**
 * This function does something see example below:
 * @example
 * var x = foo("test"); //it will show "test" message
 *
 * @param {string} str: string argument that will be shown in message
 */
function foo(str)
{
   alert(str);
}
于 2010-07-13T18:32:31.770 回答
46

采用

<pre><code>

....

</code></pre>

这是许多官方文档中使用的内容,例如,将通过一些工具接收语法高亮显示

于 2010-06-02T20:33:09.000 回答
37

Jsdoc3 有一个 markdown 插件,但默认是关闭的。./node_modules/jsdoc/conf.json.EXAMPLE通过...启用默认配置文件

"plugins": [
    "plugins/markdown"
],

...并且您对文档(包括代码)有很好的语法支持。Markdown 使用三个反引号 ( ```) 来划分代码块。使用原始示例:

/**
* This function does something see example below:
* ```
* var x = foo("test"); //it will show "test" message
* ```
* @param {string} str: string argument that will be shown in message
*/
于 2015-09-10T15:59:29.430 回答
6

您可以将任何 HTML 放入 JSDoc 中,它将被复制出来。这是我使用的示例:

/** 
 * The ReplaceSlang method replaces the string &quot;hi&quot; with &quot;hello&quot;.
 *   <script language="javascript">
 *     function testFunc() {
 *       alert(ReplaceSlang(prompt("Enter sample argument")));
 *     }
 *   </script>
 *   <input type="button" value="Test" onclick="testFunc()" />
 * @param {String} str The text to transform
 * @return {String}
 */
exports.ReplaceSlang = function(str) {
  return str.replace("hi", "hello");
};

要确保该按钮不在摘要中,请在其前面添加一个句子和一个点 (.)。

您需要找到某种方法将您的 javascript 文件包含在 JSDoc 的输出中,以便加载它们。(否则您的代码在 JSDoc 的输出中不作为 javascript 存在 - 您可以为此修改模板:请参阅JsPlate 文档

于 2010-08-23T17:37:51.507 回答
5

对于jsdoc3 <code>...</code>似乎工作得很好。它还保持代码内联,同时添加<pre>创建一个段落(无论如何它应该这样做)。浏览器支持似乎没问题,所以我看不出有任何理由不使用它。

于 2015-03-18T11:07:27.347 回答
4

使用@example适用于大多数情况,但需要将 HTML 保留字符转换为文字:&lt; &gt;等等,否则 HTML 将被渲染而不显示为代码。

于 2014-05-29T09:09:38.903 回答