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。 - 对于无序列表,请使用星号代替破折号。
用词选择
- 在描述结果时,使用“将”而不是“会”。
- 更喜欢“在 ___ 过程中”而不是“在”。
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章节下列出。 - 所有具有返回值的方法都必须以“返回
[TYPE]- [返回值描述]”开头- 如果方法返回一个
Object,可以使用冒号后跟换行符以及与函数参数样式相同的无序列表来指定其结构。
- 如果方法返回一个
- 实例事件必须在
### Instance Events章节下列出。 - 实例属性必须在
### Instance Properties章节下列出。- 实例属性必须以“一个 [属性类型] …” 开头
以 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)、Electron 的自定义 API 结构(如 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 历史记录”块是一个 YAML 代码块,封装在 HTML 注释中,应放置在类或方法的 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 Schema(api-history.schema.json),您可以在 docs 文件夹中找到它。 API 历史记录 Schema RFC 包含示例用法和对模式的每个方面的详细说明。
API 历史记录块的目的是描述 API 何时/在哪里/如何/为什么
- 添加
- 更改(通常是破坏性更改)
- 已弃用
API 历史记录块中列出的每个 API 更改都应包含指向进行该更改的 PR 的链接,以及可选的更改的简短描述。如果适用,请包含来自 heading 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
- 使用两个空格进行缩进。
- 不要使用注释。
描述
- 始终用双引号将描述括起来(即“示例”)。
- 以对应用程序开发人员有意义的方式描述更改,并使其大写、标点符号正确且为过去时。
- 请参阅 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)
请注意,未在
contextBridge.exposeInMainWorld(apiKey, api)
下添加 API 历史记录块
contextBridge.exposeInMainWorld('app', {
- ipcRenderer,
+ onEvent: (cb) => ipcRenderer.on('foo', (e, ...args) => cb(args))
})
因为该函数没有更改,只是它的使用方式发生了变化
文档翻译