JSDoc在文档中添加实际代码

Mar*_*aio 43 javascript documentation jsdoc

你知道<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);
}
Run Code Online (Sandbox Code Playgroud)

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

Sea*_*sey 37

使用

<pre><code>

....

</code></pre>
Run Code Online (Sandbox Code Playgroud)

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

  • 这看起来很奇怪.它是无效的HTML.Pre元素可能包含代码元素,但不是相反. (5认同)
  • 这不适用于 TypeScript。`@example` 答案效果很好。 (5认同)

Jos*_*son 35

@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);
}
Run Code Online (Sandbox Code Playgroud)

  • 这也是VS Code的Intellisense支持的方法。https://github.com/Microsoft/vscode/issues/30062 (4认同)
  • 来自您链接的文档:@example标记不用于生成"内联"示例,如果您需要,您需要使用<code>标记嵌入@description块中的HTML标记,例如. (3认同)
  • 不过,VS Code(在撰写本文时)似乎不支持 HTML 标签。:( 我尝试了 `&lt;pre&gt;` 和 `&lt;code&gt;`,它们只是在 Intellisense 中按字面意思编写。但是,我刚刚意识到 Markdown 似乎受支持!所以用反引号 (`) 包围内联代码是有效的。 :) (3认同)
  • 强制执行 Google JavaScript StyleGuide 的 gjslint 不允许 @example 标签...因此您可能需要使用 &lt;pre&gt;&lt;code&gt;&lt;/code&gt;&lt;/pre&gt; 解决方案。 (2认同)

off*_*ker 26

Jsdoc3有一个降价插件,但默认情况下它是关闭的../node_modules/jsdoc/conf.json.EXAMPLE通过... 启用默认配置文件

"plugins": [
    "plugins/markdown"
],
Run Code Online (Sandbox Code Playgroud)

...并且您对文档有很好的语法支持,包括代码.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
*/
Run Code Online (Sandbox Code Playgroud)

  • 在 Visual Studio Code 中,Markdown 支持似乎是开箱即用的 (4认同)

Eri*_*ric 6

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

/** 
 * 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");
};
Run Code Online (Sandbox Code Playgroud)

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

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

  • 对不起,我刚刚意识到这不是你想要做的。你介意我留下这条评论以防它对某人有帮助吗? (2认同)

Lee*_*Gee 6

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

从链接的文档中:

/**
 * Solves equations of the form a * x = b
 * @example
 * // returns 2
 * globalNS.method1(5, 10);
 * @example
 * // returns 3
 * globalNS.method(5, 15);
 * @returns {Number} Returns the value of x for the equation.
 */
globalNS.method1 = function (a, b) {
    return b / a;
};
Run Code Online (Sandbox Code Playgroud)

带标题的示例:

/**
 * Solves equations of the form a * x = b
 * @example <caption>Example usage of method1.</caption>
 * // returns 2
 * globalNS.method1(5, 10);
 * @returns {Number} Returns the value of x for the equation.
 */
globalNS.method1 = function (a, b) {
    return b / a;
};
Run Code Online (Sandbox Code Playgroud)


SGD*_*SGD 5

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