Electron 文档风格指南
以下是编写 Electron 文档的指南。
标题
- 每个页面顶部必须有一个
#
级别的标题。 - 同一页面中的章节必须使用
##
级别的标题。 - 子章节需要根据其嵌套深度增加标题中
#
的数量。 - 页面的标题必须遵循 APA 标题大小写。
- 所有章节都必须遵循 APA 句子大小写。
以 Quick Start
为例
# Quick Start
...
## Main process
...
## Renderer process
...
## Run your app
...
### Run as a distribution
...
### Manually downloaded Electron binary
...
对于 API 参考,此规则有例外。
Markdown 规则
此存储库使用 markdownlint
包来强制执行一致的 Markdown 样式。 有关确切规则,请参见根文件夹中的 .markdownlint.json
文件。
有一些样式指南未包含在 linter 规则中
- 在代码块中使用
sh
而不是cmd
(由于语法高亮显示器)。 - 为了便于阅读,请尽可能将行长度保持在 80 到 100 个字符之间。
- 不要嵌套超过 2 层的列表(由于 markdown 渲染器)。
- 所有
js
和javascript
代码块都使用 standard-markdown 进行 linting。 - 对于无序列表,请使用星号而不是破折号。
选择词语
- 在描述结果时,使用 "will" 而不是 "would"。
- 优选 "in the ___ process" 而不是 "on"。
API 参考
以下规则仅适用于 API 的文档。
标题和描述
每个模块的 API 文档必须使用 require('electron')
返回的实际对象名称作为其标题(例如 BrowserWindow
、autoUpdater
和 session
)。
在页面标题正下方,添加模块的单行描述作为 markdown 引用(以 >
开头)。
以 session
模块为例
# session
> Manage browser sessions, cookies, cache, proxy settings, etc.
模块方法和事件
对于非类的模块,其方法和事件必须在 ## Methods
和 ## Events
章节下列出。
以 autoUpdater
为例
# autoUpdater
## Events
### Event: 'error'
## Methods
### `autoUpdater.setFeedURL(url[, requestHeaders])`
类
- API 类或属于模块的类必须在
## Class: TheClassName
章节下列出。 - 一个页面可以有多个类。
- 构造函数必须使用
###
级别的标题列出。 - 静态方法 必须在
### Static Methods
章节下列出。 - 实例方法 必须在
### Instance Methods
章节下列出。 - 所有具有返回值的方法都必须以 "Returns
[TYPE]
- [Return description]" 开头。- 如果该方法返回一个
Object
,则可以使用冒号后跟一个换行符,然后是一个与函数参数样式相同的属性无序列表来指定其结构。
- 如果该方法返回一个
- 实例事件必须在
### Instance Events
章节下列出。 - 实例属性必须在
### Instance Properties
章节下列出。- 实例属性必须以 "A [Property Type] ..." 开头。
以 Session
和 Cookies
类为例
# session
## Methods
### session.fromPartition(partition)
## Static Properties
### session.defaultSession
## Class: Session
### Instance Events
#### Event: 'will-download'
### Instance Methods
#### `ses.getCacheSize()`
### Instance Properties
#### `ses.cookies`
## Class: Cookies
### Instance Methods
#### `cookies.get(filter, callback)`
方法及其参数
方法章节必须采用以下形式
### `objectName.methodName(required[, optional]))`
* `required` string - A parameter description.
* `optional` Integer (optional) - Another parameter description.
...
标题级别
标题可以是 ###
或 ####
级别,具体取决于该方法是属于模块还是类。
函数签名
对于模块,objectName
是模块的名称。 对于类,它必须是该类实例的名称,并且不得与模块的名称相同。
例如,session
模块下的 Session
类的方法必须使用 ses
作为 objectName
。
可选参数用方括号 []
表示,方括号包含可选参数以及如果此可选参数跟随另一个参数所需的逗号。
required[, optional]
参数描述
有关每个参数的更多详细信息在方法下面的无序列表中注明。 参数的类型由 JavaScript 原始类型(例如 string
、Promise
或 Object
)、自定义 API 结构(如 Electron 的 Cookie)或通配符 any
表示。
如果参数的类型为 Array
,请使用 []
简写,其中包含数组内的值的类型(例如,any[]
或 string[]
)。
如果参数的类型为 Promise
,请使用 promise 解析到的内容来参数化该类型(例如,Promise<void>
或 Promise<string>
)。
如果参数可以是多种类型,请用 |
分隔这些类型。
Function
类型参数的描述应明确说明如何调用它,并列出将传递给它的参数的类型。
平台特定功能
如果参数或方法是特定平台独有的,则这些平台使用空格分隔的斜体列表在数据类型之后表示。 值可以是 macOS
、Windows
或 Linux
。
* `animate` boolean (optional) _macOS_ _Windows_ - Animate the thing.
事件
事件章节必须采用以下形式
### Event: 'wake-up'
Returns:
* `time` string
...
标题可以是 ###
或 ####
级别,具体取决于该事件是属于模块还是类。
事件的参数遵循与方法相同的规则。
属性
属性章节必须采用以下形式
### session.defaultSession
...
标题可以是 ###
或 ####
级别,具体取决于该属性是属于模块还是类。
API 历史
"API 历史"块是由 HTML 注释封装的 YAML 代码块,应直接放置在类或方法的 Markdown 标题之后,如下所示
#### `win.setTrafficLightPosition(position)` _macOS_
<!--
```YAML history
added:
- pr-url: https://github.com/electron/electron/pull/22533
changes:
- pr-url: https://github.com/electron/electron/pull/26789
description: "Made `trafficLightPosition` option work for `customButtonOnHover` window."
deprecated:
- pr-url: https://github.com/electron/electron/pull/37094
breaking-changes-header: deprecated-browserwindowsettrafficlightpositionposition
```
-->
* `position` [Point](structures/point.md)
Set a custom position for the traffic light buttons. Can only be used with `titleBarStyle` set to `hidden`.
它应符合 API 历史 JSON 模式 (api-history.schema.json
),您可以在 docs
文件夹中找到该模式。 API 历史模式 RFC 包含示例用法和模式每个方面的详细说明。
API 历史块的目的是描述何时/何地/如何/为什么 API
- 添加
- 更改(通常是重大更改)
- 弃用
该块中列出的每个 API 更改都应包含指向进行该更改的 PR 的链接,以及对更改的可选简短描述。 如果适用,请包括来自 重大更改文档 的更改的 标题 ID。
API 历史 linting 脚本 (lint:api-history
) 根据模式验证 Electron 文档中的 API 历史块,并执行其他一些检查。 您可以查看其 测试 以获取更多详细信息。
有一些样式指南未包含在 linting 脚本中
格式
始终遵守此格式
API HEADER | #### `win.flashFrame(flag)`
BLANK LINE |
HTML COMMENT OPENING TAG | <!--
API HISTORY OPENING TAG | ```YAML history
API HISTORY | added:
| - pr-url: https://github.com/electron/electron/pull/22533
API HISTORY CLOSING TAG | ```
HTML COMMENT CLOSING TAG | -->
BLANK LINE |
YAML
- 使用两个空格进行缩进。
- 不要使用注释。
描述
- 始终用双引号将描述括起来(即 "example")。
- 以与应用程序开发人员相关的方式描述更改,并将其大写、标点符号化和过去式。
- 有关示例,请参阅 Clerk。
- 保持描述简洁。
- 理想情况下,描述应与其在重大更改文档中的相应标题匹配。
- 尽可能优先使用来自相关 PR 的发行说明。
- 开发人员始终可以查看重大更改文档或链接的拉取请求以获取更多详细信息。
放置
通常,您应该将 API 历史块直接放置在已更改的类或方法的 Markdown 标题之后。 但是,在某些情况下,这并不明确
Chromium 版本提升
有时,重大更改与任何现有 API 都没有关系。 在这种情况下,可以不在任何地方添加 API 历史记录。
影响多个 API 的更改
有时,重大更改涉及多个 API。 在这种情况下,将 API 历史块放置在每个涉及的 API 的顶级 Markdown 标题下。
# contextBridge
<!--
```YAML history
changes:
- pr-url: https://github.com/electron/electron/pull/40330
description: "`ipcRenderer` can no longer be sent over the `contextBridge`"
breaking-changes-header: behavior-changed-ipcrenderer-can-no-longer-be-sent-over-the-contextbridge
```
-->
> Create a safe, bi-directional, synchronous bridge across isolated contexts
# ipcRenderer
<!--
```YAML history
changes:
- pr-url: https://github.com/electron/electron/pull/40330
description: "`ipcRenderer` can no longer be sent over the `contextBridge`"
breaking-changes-header: behavior-changed-ipcrenderer-can-no-longer-be-sent-over-the-contextbridge
```
-->
Process: [Renderer](../glossary.md#renderer-process)
请注意,API 历史块未添加到
contextBridge.exposeInMainWorld(apiKey, api)
因为该函数没有更改,只有它的使用方式发生了更改
contextBridge.exposeInMainWorld('app', {
- ipcRenderer,
+ onEvent: (cb) => ipcRenderer.on('foo', (e, ...args) => cb(args))
})
文档翻译
请参阅 electron/i18n