JSDoc 在文档中添加真实代码

发布于 2024-09-03 18:58:27 字数 386 浏览 10 评论 0原文

你知道 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 显示为代码(如果没有突出显示语法,至少像预格式化或带有灰色背景的东西)。

Do you know if there is some sort of <code /> tag in JSDoc? I need to add pieces of code in my documentation like this:

/**
 * 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);
}

I need the code in the comments to be displayed by JSDoc as code (if not syntax highlighted, at least like pre-formatted or something with grey background).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

几味少女 2024-09-10 18:58:28

@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);
}

@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);
}
以往的大感动 2024-09-10 18:58:28

使用

<pre><code>

....

</code></pre>

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

Use

<pre><code>

....

</code></pre>

This is whats used in many official docs, and will for instance receive syntax hightlighting with some tools

私野 2024-09-10 18:58:28

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

Jsdoc3 has a markdown plugin but it is turned off by default. Enable it the default config file ./node_modules/jsdoc/conf.json.EXAMPLE via ...

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

... and you have nice syntax support for docs, including for code. Markdown uses three backticks (```) to demarcate code blocks. For to use the original example:

/**
* 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
*/
栀梦 2024-09-10 18:58:28

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

/** 
 * The ReplaceSlang method replaces the string "hi" with "hello".
 *   <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 文档)

You can put any HTML in JSDoc and it will be copied out. Heres an example of what I use:

/** 
 * The ReplaceSlang method replaces the string "hi" with "hello".
 *   <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");
};

To make sure the button is not in the summary, add a sentence and a dot (.) before it.

You need to find some way to include your javascript file in the output of JSDoc so that they are loaded. (Your code otherwise does not exist as javascript in the output of JSDoc – you can modify the template for that : see JsPlate documentation)

謸气贵蔟 2024-09-10 18:58:28

使用 @example 适用于大多数情况,但 HTML 保留字符需要转换为 HTML 实体: < > 等等,否则 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;
};

带有标题的示例:

/**
 * 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;
};

Using @example works for most cases, but HTML reserved characters need to be translated to HTML entities: < > and so on, otherwise the HTML will be rendered and not displayed as code.

From the linked documentation:

/**
 * 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;
};

An example with a caption:

/**
 * 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;
};
貪欢 2024-09-10 18:58:28

对于 jsdoc3 ... 似乎工作得很好。它还使代码保持内联,同时添加

 创建一个段落(无论如何,这是它应该做的)。 浏览器支持似乎没问题,所以我认为没有任何理由不使用它。

For jsdoc3 <code>...</code> seems to work just fine. It also keeps the code inline while adding in <pre> creates a paragraph (which is what it should do anyways). Browser support seems to be ok so I don't see any reason to not use it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文