问题窗体解析器
转换 JSON 的版本响应
关于
此软件包可用于将 GitHub 问题解析为机器可读 JSON 处理。 特别是,它旨在与 发行表格 使问题机构实现基本转变,从而实现一致性 JSON输出.
使用问题表格提交的问题采用结构化的Markdown格式. 再见 由于发行机构没有被用户大量修改, 我们可以可靠地分析 问题体变成 JSON 对象.
安装
npm i @github/issue-parser
示例
以下是一个简单的例子,说明如何在您的工程中使用此软件包.
import { parseIssue } from '@github/issue-parser'
const issue = parseIssue('<issue body>', '<template>')
假设问题和模板看起来像这些例子:
结果 issue 对象的外观如下:
{
name: 'this-thing',
nickname: 'thing',
color: ['blue'],
shape: ['square'],
sounds: ['re', 'mi'],
topics: [],
description: "This is a description.\n\nIt has multiple lines.\n\nIt's pretty cool!",
notes: '- Note\n- Another note\n- Lots of notes',
code: 'const thing = new Thing()\nthing.doThing()',
'code-string': 'thing.toString()',
'is-thing': {
selected: ['Yes'],
unselected: ['No']
},
'is-thing-useful': {
selected: ['Sometimes'],
unselected: ['Yes', 'No']
},
'read-team': 'IssueOps-Demo-Readers',
'write-team': 'IssueOps-Demo-Writers'
}
使用量
parseIssue
这是软件包的主要功能. 这需要两个论点:
issue: string- 要解析的问题的正文template: string- (可选) 用于创建 问题options?: { slugify?: boolean }- (备选)附加解析选项 处理问题主体时使用
如果 template 将尝试改变
以 type 财产的财产
模板中的特定字段。 如果 template 数值被省略,全部解析
值将作为字符串返回。 关于转型的信息
应用,参见 转换 节.
分析选项
parseTemplate(template?: string)
分析一个问题窗体模板并返回对象。 这个可以用来匹配 与字段一起在问题机构形成响应,以便您能够执行 额外验证.
当解析一个问题和相关的形式模板时,此软件包将 尝试将字段ID与响应值相匹配。 如果找到匹配的字段 ID将在解析对象密钥中使用(而不是来自 缩放反应)。 如果没有匹配,则以页眉文字作为对象 键.
例如,如果您有下列问题表模板:
name: Example Request
description: Submit an example request
title: '[Request] Example'
body:
- type: input
id: name
attributes:
label: The Name of the Thing
description: The name of the thing you want to create.
placeholder: this-is-the-thing
validations:
required: true
以及以下议题机构:
### The Name of the Thing
this-thing
### The Nickname of the Thing
thing
由此产生的问题将是:
{
// Uses the ID value from the issue form template as the key
"name": "this-thing",
// Uses the original header value from the issue body
"The Nickname of the Thing": "thing"
}
转换
以下变化将视 输入类型。 该类型取自发行表格模板. 供参考 每个特定类型,见 GitHub 形式方案语法。 。 。 .
单行
在此之前:
This is a response
之后( 无更改 ) :
This is a response
多线
[注]
在多行响应中保留了空行.
在此之前:
First line :D
Third line!
之后:
First line :D\n\nThird line!
下拉选择
在此之前:
red, blue, green
之后:
["red", "blue", "green"]
复选框
在此之前:
- [x] Pick me!
- [ ] Don't pick me D:
之后:
{
"selected": ["Pick me!"],
"unselected": ["Don't pick me D:"]
}
输入
在以下情况下,产出JSON中将省去一个输入:
| 假设 | 示例 |
|---|---|
| 无效的标题 | ## This is invalid |
| 空标题 | ### |
This is a value | |
| 无值 | ### This is a heading |
### This is another | |
This is a value |
通常,如果提交表单时带有空字段,则将包含在 根据表格中的输入类型,将问题机构作为以下之一: 模板.
| 类型 | 无答复 |
|---|---|
dropdown | None |
input | _No response_ |
textarea | _No response_ |
### Dropdown Field
None
### Input or Textarea Field
_No response_
### Checkboxes Field
- [ ] Item A
- [ ] Item B
这些将按输入类型转换为以下之一 问题表格模板中指定的:
| 类型 | 产出 |
|---|---|
checkboxes | { "selected": [], "unselected": []} |
dropdown | [] |
input | undefined |
textarea | undefined |