{"meta":{"title":"注释代码示例","intro":"可以批注较长的代码示例，以说明它们的工作原理以及用户如何自定义它们以用于其他用途。","product":"贡献 GitHub 文档","breadcrumbs":[{"href":"/zh/contributing","title":"贡献 GitHub 文档"},{"href":"/zh/contributing/writing-for-github-docs","title":"为 GitHub 文档撰写内容"},{"href":"/zh/contributing/writing-for-github-docs/annotating-code-examples","title":"批注代码示例"}],"documentType":"article"},"body":"# 注释代码示例\n\n可以批注较长的代码示例，以说明它们的工作原理以及用户如何自定义它们以用于其他用途。\n\n## 关于代码注释\n\n代码注释通过描述代码示例的用途和原因来帮助解释较长的代码示例。 注释以两个窗格布局呈现在代码示例旁边，因此我们可以编写更长的批注，而不会使代码本身难以阅读。 我们只批注完整的代码示例，而不是代码片段。 代码注释并非每个代码示例都是必需的，仅当明确需要它们时才应使用。\n\n代码注释对各种受众都很有用。 通常，代码注释将用于向新用户解释关键概念或向更有经验的用户解释特定选择。\n\n对于新用户，代码注释是一种超越代码示例的简要概述并解释每行代码用途的方法，以便有人可以理解代码，就好像朋友或同事在引导他们完成代码一样。\n\n对于更有经验的用户，代码注释可以帮助他们了解代码示例，然后根据他们的具体需求对其进行定制。 注释可以解释为什么以某种方式编写代码，以便了解基础知识。\n\n你可以在一个项目中批注多个代码示例，但请记住，每个注释都会增加项目的复杂性，并为使用屏幕阅读器的用户添加重复的导航任务。 如果一个项目中有多个代码示例，请考虑是否可以将它们组合成一个示例。\n\n## 启用和添加代码注释\n\n1. 为文章指定 `layout: inline` 前置数据属性。\n1. 使用三重反引号创建代码示例。\n1. 在三重反引号后为代码示例指定一种语言，后跟 `annotate`。 例如，` ```yaml annotate` 或 ` ```ruby annotate`。\n1. 在代码示例中使用注释标记（`#`、`//`、<code><&#33--</code>、`%%`）添加注释。 必须为编写代码示例时使用的语言使用注释标记。 例如，`#` 表示 YAML，`//` 表示 JavaScript。\n   * 批注的代码示例必须以单行注释开头。 如果不想向第一行代码添加注释，可以从空白注释开始。\n   * 注释应用于注释标记下方行到下一注释标记或代码块末尾的代码。\n\n### 注释规则\n\n以下规则适用于所有代码注释。\n\n* 不支持多行样式注释，例如 `/*`。\n* 以代码注释开头的符号与注释内容之间必须留有一个空格。\n  * **使用：**`# comment`\n  * **避免：**`#comment`\n* 要创建空白注释，请插入一个注释标记，然后确保该标记后面没有文本。 如果示例的某些行不需要注释，则空白注释非常有用。\n* 以 `#!` 开头的字符串将在代码块中呈现，并且不被视为注释。\n* 注释标记后的任何内容都将使用 Markdown 进行分析。 链接、版本控制和其他样式将会呈现，就好像它们是用 Markdown 编写的一样。\n* 多个连续序列的评论将合并成一个注释。\n* 不以注释标记开头且为空或仅包含空格的行将被忽略。\n* 必须使用单行注释开始代码部分。 如果代码的第一行（或部分）不需要注释，则可以使用不带文本的注释标记来创建空白注释。\n* 对于 HTML 样式，应在注释后面添加结束标记 `<!-- -->`，以保持语法突出显示。\n\n## 代码注释最佳做法\n\n通过代码块前的简介来介绍代码示例的总体用途，并使用注释来解释特定代码行的用途以及它们这样做的原因。\n\n优先处理代码批注的清晰度，同时尝试尽量缩短代码注释。 用户使用代码示例作为其自己工作的基础，因此注释应帮助用户了解编写的示例，以及他们如何调整示例以用于其他用途。\n\n编写代码注释时，请考虑受众，不要认为用户会知道以某种方式编写示例的原因。\n\n注释可用于显示它们批注的代码的预期结果，但整个代码示例的结果应采用最适合受众的方式：代码示例的简介或在示例之后讨论。\n\n如果更改了代码示例，请检查所有注释是否仍然有效。\n\n## 带注释代码示例\n\n以下示例展示呈现的代码注释的外观以及创建它们的原始代码。\n\n### 渲染示例\n\n以下代码示例展示了一个工作流，该工作流在打开拉取请求时对其发布欢迎注释。\n\n```yaml annotate\n# The name of the workflow as it will appear in the \"Actions\" tab of the GitHub repository.\nname: Post welcome comment\n# The `on` keyword lets you define the events that trigger when the workflow is run.\non:\n  # Add the `pull_request` event, so that the workflow runs automatically\n  # every time a pull request is created.\n  pull_request:\n    types: [opened]\n# Modifies the default permissions granted to `GITHUB_TOKEN`.\npermissions:\n  pull-requests: write\n# Defines a job with the ID `build` that is stored within the `jobs` key.\njobs:\n  build:\n    name: Post welcome comment\n    # Configures the operating system the job runs on.\n    runs-on: ubuntu-latest\n    # The `run` keyword tells the job to execute a command on the runner.\n    steps:\n      - run: gh pr comment $PR_URL --body \"Welcome to the repository!\"\n        env:\n          GH_TOKEN: $\n          PR_URL: $\n```\n\n### 原始代码示例\n\n    The following code example shows a workflow that posts a welcome comment on a pull request when it is opened.\n\n    ```yaml annotate\n    # The name of the workflow as it will appear in the \"Actions\" tab of the GitHub repository.\n    name: Post welcome comment\n    # The `on` keyword lets you define the events that trigger when the workflow is run.\n    on:\n      # Add the `pull_request` event, so that the workflow runs automatically\n      # every time a pull request is created.\n      pull_request:\n        types: [opened]\n    # Modifies the default permissions granted to `GITHUB_TOKEN`.\n    permissions:\n      pull-requests: write\n    # Defines a job with the ID `build` that is stored within the `jobs` key.\n    jobs:\n      build:\n        name: Post welcome comment\n        # Configures the operating system the job runs on.\n        runs-on: ubuntu-latest\n        # The `run` keyword tells the job to execute a command on the runner.\n        steps:\n          - run: gh pr comment $PR_URL --body \"Welcome to the repository!\"\n            env:\n              GH_TOKEN: $\n              PR_URL: $\n    ```"}