@zentered/issue-forms-body-parser 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintignore +3 -0
- package/.eslintrc.json +15 -0
- package/.github/ISSUE_TEMPLATE/bug_report.md +24 -0
- package/.github/ISSUE_TEMPLATE/feature_request.md +19 -0
- package/.github/dependabot.yml +6 -0
- package/.github/workflows/publish.yml +24 -0
- package/.github/workflows/test.yml +24 -0
- package/.husky/commit-msg +1 -0
- package/.husky/pre-commit +1 -0
- package/.prettierrc +6 -0
- package/CONTRIBUTING.md +11 -0
- package/LICENSE +21 -0
- package/README.md +114 -0
- package/action.yml +15 -0
- package/package.json +86 -0
- package/src/index.js +21 -0
- package/src/parse.js +123 -0
- package/test/parse-issue.test.js +92 -0
- package/test/test-issue-1.md +58 -0
package/.eslintignore
ADDED
package/.eslintrc.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Bug report
|
|
3
|
+
about: Create a report to help us improve
|
|
4
|
+
title: ''
|
|
5
|
+
labels: ''
|
|
6
|
+
assignees: ''
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
**Describe the bug**
|
|
10
|
+
A clear and concise description of what the bug is.
|
|
11
|
+
|
|
12
|
+
**To Reproduce**
|
|
13
|
+
Steps to reproduce the behavior:
|
|
14
|
+
|
|
15
|
+
1. Go to '...'
|
|
16
|
+
2. Click on '....'
|
|
17
|
+
3. Scroll down to '....'
|
|
18
|
+
4. See error
|
|
19
|
+
|
|
20
|
+
**Expected behavior**
|
|
21
|
+
A clear and concise description of what you expected to happen.
|
|
22
|
+
|
|
23
|
+
**Additional context**
|
|
24
|
+
Add any other context about the problem here.
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Feature request
|
|
3
|
+
about: Suggest an idea for this project
|
|
4
|
+
title: ''
|
|
5
|
+
labels: ''
|
|
6
|
+
assignees: ''
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
**Is your feature request related to a problem? Please describe.**
|
|
10
|
+
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
|
|
11
|
+
|
|
12
|
+
**Describe the solution you'd like**
|
|
13
|
+
A clear and concise description of what you want to happen.
|
|
14
|
+
|
|
15
|
+
**Describe alternatives you've considered**
|
|
16
|
+
A clear and concise description of any alternative solutions or features you've considered.
|
|
17
|
+
|
|
18
|
+
**Additional context**
|
|
19
|
+
Add any other context or screenshots about the feature request here.
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
name: Publish
|
|
2
|
+
|
|
3
|
+
permissions:
|
|
4
|
+
contents: write
|
|
5
|
+
deployments: write
|
|
6
|
+
issues: read
|
|
7
|
+
pull-requests: write
|
|
8
|
+
|
|
9
|
+
on:
|
|
10
|
+
push:
|
|
11
|
+
branches:
|
|
12
|
+
- 'main'
|
|
13
|
+
|
|
14
|
+
jobs:
|
|
15
|
+
test:
|
|
16
|
+
env:
|
|
17
|
+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
18
|
+
runs-on: ubuntu-latest
|
|
19
|
+
steps:
|
|
20
|
+
- uses: actions/checkout@v3
|
|
21
|
+
- run: npm ci
|
|
22
|
+
- run: npm test
|
|
23
|
+
- run: npm run release
|
|
24
|
+
- run: npx semantic-release
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
name: Test
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
pull_request:
|
|
5
|
+
branches:
|
|
6
|
+
- '**'
|
|
7
|
+
- '!main'
|
|
8
|
+
push:
|
|
9
|
+
branches:
|
|
10
|
+
- '**'
|
|
11
|
+
- '!main'
|
|
12
|
+
|
|
13
|
+
jobs:
|
|
14
|
+
test:
|
|
15
|
+
env:
|
|
16
|
+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
17
|
+
runs-on: ubuntu-latest
|
|
18
|
+
steps:
|
|
19
|
+
- uses: actions/checkout@v3
|
|
20
|
+
with:
|
|
21
|
+
fetch-depth: 0
|
|
22
|
+
- run: npm ci
|
|
23
|
+
- run: npm run lint
|
|
24
|
+
- run: npm test
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
npx commitlint --edit $1
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
npx lint-staged
|
package/.prettierrc
ADDED
package/CONTRIBUTING.md
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# How to contribute
|
|
2
|
+
|
|
3
|
+
We'd love to accept your patches and contributions to this project. There are just a few small guidelines you need to follow.
|
|
4
|
+
|
|
5
|
+
## Code reviews
|
|
6
|
+
|
|
7
|
+
All submissions require review. We use GitHub pull requests for this purpose. Consult [GitHub Help](https://help.github.com/articles/about-pull-requests/) for more information on using pull requests.
|
|
8
|
+
|
|
9
|
+
## Style Guide
|
|
10
|
+
|
|
11
|
+
This project provides a prettier configuration. All submitted PRs will be required to conform to this style guide before being merged.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Zentered OÜ
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
# GitHub Issue Forms Body Parser
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+

|
|
5
|
+

|
|
6
|
+
[](https://conventionalcommits.org)
|
|
7
|
+
[](https://zentered.co)
|
|
8
|
+
|
|
9
|
+
[Issue Forms](https://docs.github.com/en/communities/using-templates-to-encourage-useful-issues-and-pull-requests/configuring-issue-templates-for-your-repository#creating-issue-forms)
|
|
10
|
+
is a great way to structure GitHub Issues to an expected format, and to make it
|
|
11
|
+
easier to capture information from the user. Unfortunately, the schema only
|
|
12
|
+
defined the input of the data, not the output. So the markdown body needs to be
|
|
13
|
+
parsed to extract the information in a structured way and to make further
|
|
14
|
+
processing easier.
|
|
15
|
+
|
|
16
|
+
We use this Action at the
|
|
17
|
+
[Cyprus Developer Community](https://github.com/cyprus-developer-community) to
|
|
18
|
+
[create issues with event data](https://github.com/cyprus-developer-community/events/issues/new?assignees=&labels=Event+%3Asparkles%3A&template=event.yml&title=Event+Title)
|
|
19
|
+
for upcoming meetups etc. The parser extracts the information from the issues
|
|
20
|
+
and provides structured data to create calendar entries (ie `.ics` files for
|
|
21
|
+
[calendar subscriptions with GitEvents](https://github.com/gitevents/ics)),
|
|
22
|
+
calling 3rd party APIs, etc.
|
|
23
|
+
|
|
24
|
+
\_Inspired by:
|
|
25
|
+
[Peter Murray's Issue Forms Body Parser](https://github.com/peter-murray/issue-forms-body-parser)
|
|
26
|
+
with valuable feedback from [Steffen](https://gist.github.com/steffen)\_
|
|
27
|
+
|
|
28
|
+
## Features
|
|
29
|
+
|
|
30
|
+
- :white_check_mark: parse question/answer format into title/text as JSON
|
|
31
|
+
- :white_check_mark: parse line items and "tasks" with separate `checked`
|
|
32
|
+
attributes
|
|
33
|
+
- :white_check_mark: slugify title to id to find data
|
|
34
|
+
- :white_check_mark: parse dates and times into separate `date` and `time`
|
|
35
|
+
fields
|
|
36
|
+
- :negative_squared_cross_mark: no tokens/input required
|
|
37
|
+
- :negative_squared_cross_mark: zero configuration
|
|
38
|
+
|
|
39
|
+
Transforms markdown from GitHub Issue Forms:
|
|
40
|
+
|
|
41
|
+
```markdown
|
|
42
|
+
### Event Description
|
|
43
|
+
|
|
44
|
+
Let's meet for coffee and chat about tech, coding, Cyprus and the newly formed
|
|
45
|
+
CDC (Cyprus Developer Community).
|
|
46
|
+
|
|
47
|
+
### Location
|
|
48
|
+
|
|
49
|
+
Cafe Nero Finikoudes, Larnaka
|
|
50
|
+
|
|
51
|
+
### Date
|
|
52
|
+
|
|
53
|
+
11.03.2022
|
|
54
|
+
|
|
55
|
+
### Time
|
|
56
|
+
|
|
57
|
+
16:00
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
to structured, usable data:
|
|
61
|
+
|
|
62
|
+
```json
|
|
63
|
+
[
|
|
64
|
+
{
|
|
65
|
+
"id": "event-description",
|
|
66
|
+
"title": "Event Description",
|
|
67
|
+
"text": "Let's meet for coffee and chat about tech, coding, Cyprus and the newly formed\nCDC (Cyprus Developer Community).\n"
|
|
68
|
+
},
|
|
69
|
+
{
|
|
70
|
+
"id": "location",
|
|
71
|
+
"title": "Location",
|
|
72
|
+
"text": "Cafe Nero Finikoudes, Larnaka\n"
|
|
73
|
+
},
|
|
74
|
+
{
|
|
75
|
+
"id": "date",
|
|
76
|
+
"title": "Date",
|
|
77
|
+
"text": "11.03.2022\n",
|
|
78
|
+
"date": "2022-03-11T00:00:00.000Z"
|
|
79
|
+
},
|
|
80
|
+
{ "id": "time", "title": "Time", "text": "16:00\n", "time": "16:00" }
|
|
81
|
+
]
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
See more examples in [md test cases](./test/test-issue-1.md) and
|
|
85
|
+
[test results](./test/parse-issue-test.md]).
|
|
86
|
+
|
|
87
|
+
## Installation & Usage
|
|
88
|
+
|
|
89
|
+
```yml
|
|
90
|
+
name: Issue Forms Body Parser
|
|
91
|
+
|
|
92
|
+
on: issues
|
|
93
|
+
|
|
94
|
+
jobs:
|
|
95
|
+
process:
|
|
96
|
+
runs-on: ubuntu-latest
|
|
97
|
+
steps:
|
|
98
|
+
- name: Issue Forms Body Parser
|
|
99
|
+
id: parse
|
|
100
|
+
uses: zentered/issue-forms-body-parser@1.0.0
|
|
101
|
+
- run: echo "${{ JSON.stringify(steps.parse.outputs.data) }}"
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
## Links
|
|
105
|
+
|
|
106
|
+
- [Creating issue forms](https://docs.github.com/en/communities/using-templates-to-encourage-useful-issues-and-pull-requests/configuring-issue-templates-for-your-repository#creating-issue-forms)
|
|
107
|
+
- [Syntax for GitHub's form schema](https://docs.github.com/en/communities/using-templates-to-encourage-useful-issues-and-pull-requests/syntax-for-githubs-form-schema)
|
|
108
|
+
|
|
109
|
+
## License
|
|
110
|
+
|
|
111
|
+
Licensed under [MIT](./LICENSE).
|
|
112
|
+
|
|
113
|
+
Here is a list of all the licenses of our
|
|
114
|
+
[production dependencies](./dist/licenses.txt)
|
package/action.yml
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
name: GitHub Issue Forms Body Parser
|
|
2
|
+
author: PatrickHeneise
|
|
3
|
+
description:
|
|
4
|
+
'Issue Forms is a great way to structure GitHub Issues to an expected format,
|
|
5
|
+
and to make it easier to capture information.'
|
|
6
|
+
branding:
|
|
7
|
+
color: blue
|
|
8
|
+
icon: chevron-right
|
|
9
|
+
outputs:
|
|
10
|
+
data:
|
|
11
|
+
description:
|
|
12
|
+
The extracted payload data from the issue body labels in JSON encoded form
|
|
13
|
+
runs:
|
|
14
|
+
main: dist/index.js
|
|
15
|
+
using: node16
|
package/package.json
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@zentered/issue-forms-body-parser",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"private": false,
|
|
5
|
+
"description": "Tech Events meet Issue Ops",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"issues",
|
|
8
|
+
"forms",
|
|
9
|
+
"issue ops",
|
|
10
|
+
"github",
|
|
11
|
+
"action"
|
|
12
|
+
],
|
|
13
|
+
"homepage": "https://zentered.co/",
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/zentered/issue-forms-body-parser/issues"
|
|
16
|
+
},
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/zentered/issue-forms-body-parser.git"
|
|
20
|
+
},
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"author": "Patrick Heneise <patrick@zentered.co> (https://zentered.co)",
|
|
23
|
+
"contributors": [
|
|
24
|
+
{
|
|
25
|
+
"name": "Patrick Heneise",
|
|
26
|
+
"url": "https://zentered.co",
|
|
27
|
+
"author": true
|
|
28
|
+
}
|
|
29
|
+
],
|
|
30
|
+
"type": "module",
|
|
31
|
+
"main": "src/parse.js",
|
|
32
|
+
"scripts": {
|
|
33
|
+
"build": "ncc build src/index.js -o dist --license licenses.txt",
|
|
34
|
+
"release": "run-s lint test build",
|
|
35
|
+
"lint": "eslint .",
|
|
36
|
+
"license-checker": "license-checker --production --onlyAllow=\"MIT;ISC;BSD-3-Clause;BSD-2-Clause;Apache-2.0\"",
|
|
37
|
+
"test": "tap --node-arg=--experimental-json-modules --test-env=GITHUB_REPOSITORY=zentered/issue-forms-body-parser-test -J test/*.test.js",
|
|
38
|
+
"test:report": "tap --test-env=GITHUB_REPOSITORY=zentered/issue-forms-body-parser-test -J test/*.test.js --cov",
|
|
39
|
+
"_postinstall": "husky install",
|
|
40
|
+
"prepublishOnly": "pinst --disable",
|
|
41
|
+
"postpublish": "pinst --enable"
|
|
42
|
+
},
|
|
43
|
+
"commitlint": {
|
|
44
|
+
"extends": [
|
|
45
|
+
"@commitlint/config-conventional"
|
|
46
|
+
]
|
|
47
|
+
},
|
|
48
|
+
"lint-staged": {
|
|
49
|
+
"*.{js,json,md}": [
|
|
50
|
+
"prettier --write"
|
|
51
|
+
],
|
|
52
|
+
"*.{js}": [
|
|
53
|
+
"eslint --cache --fix"
|
|
54
|
+
]
|
|
55
|
+
},
|
|
56
|
+
"dependencies": {
|
|
57
|
+
"@actions/core": "^1.6.0",
|
|
58
|
+
"@actions/github": "^5.0.0",
|
|
59
|
+
"@sindresorhus/slugify": "^2.1.0",
|
|
60
|
+
"date-fns": "^2.28.0",
|
|
61
|
+
"date-fns-tz": "^1.3.0",
|
|
62
|
+
"remark-gfm": "^3.0.1",
|
|
63
|
+
"remark-parse": "^10.0.1",
|
|
64
|
+
"remark-stringify": "^10.0.2",
|
|
65
|
+
"unified": "^10.1.2"
|
|
66
|
+
},
|
|
67
|
+
"devDependencies": {
|
|
68
|
+
"@commitlint/config-conventional": "^16.2.1",
|
|
69
|
+
"@vercel/ncc": "^0.33.3",
|
|
70
|
+
"commitlint": "^16.2.3",
|
|
71
|
+
"eslint": "^8.11.0",
|
|
72
|
+
"eslint-plugin-json": "^3.1.0",
|
|
73
|
+
"eslint-plugin-node": "^11.1.0",
|
|
74
|
+
"husky": "^7.0.4",
|
|
75
|
+
"license-checker": "^25.0.1",
|
|
76
|
+
"npm-run-all": "^4.1.5",
|
|
77
|
+
"pinst": "^3.0.0",
|
|
78
|
+
"prettier": "^2.6.0",
|
|
79
|
+
"tap": "^16.0.0"
|
|
80
|
+
},
|
|
81
|
+
"release": {
|
|
82
|
+
"branches": [
|
|
83
|
+
"main"
|
|
84
|
+
]
|
|
85
|
+
}
|
|
86
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import github from '@actions/github'
|
|
2
|
+
import core from '@actions/core'
|
|
3
|
+
import parse from './parse.js'
|
|
4
|
+
|
|
5
|
+
async function run() {
|
|
6
|
+
core.info('Parsing issue body ...')
|
|
7
|
+
|
|
8
|
+
try {
|
|
9
|
+
const parsedContent = await parse(github.context.payload.issue.body)
|
|
10
|
+
|
|
11
|
+
if (parsedContent !== undefined) {
|
|
12
|
+
core.setOutput('payload', parsedContent)
|
|
13
|
+
} else {
|
|
14
|
+
core.setFailed(`There was no valid payload found in the issue.`)
|
|
15
|
+
}
|
|
16
|
+
} catch (err) {
|
|
17
|
+
core.setFailed(err)
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
run()
|
package/src/parse.js
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
import { unified } from 'unified'
|
|
4
|
+
import remarkParse from 'remark-parse'
|
|
5
|
+
import remarkGfm from 'remark-gfm'
|
|
6
|
+
import slugify from '@sindresorhus/slugify'
|
|
7
|
+
import remarkStringify from 'remark-stringify'
|
|
8
|
+
import { parse, isMatch } from 'date-fns'
|
|
9
|
+
// if the system time is not UTC, we need to convert it to UTC
|
|
10
|
+
import { zonedTimeToUtc, formatInTimeZone } from 'date-fns-tz/esm'
|
|
11
|
+
const loc = 'UTC'
|
|
12
|
+
|
|
13
|
+
const commonDateFormats = [
|
|
14
|
+
'yyyy-MM-dd',
|
|
15
|
+
'dd/MM/yyyy',
|
|
16
|
+
'dd/MM/yy',
|
|
17
|
+
'dd-MM-yyyy',
|
|
18
|
+
'dd-MM-yy',
|
|
19
|
+
'dd.MM.yyyy',
|
|
20
|
+
'dd.MM.yy'
|
|
21
|
+
]
|
|
22
|
+
|
|
23
|
+
const commonTimeFormats = ['HH:mm', 'hh:mm a', 'hh:mm A']
|
|
24
|
+
|
|
25
|
+
function parseDate(text) {
|
|
26
|
+
const match = commonDateFormats.map((format) => {
|
|
27
|
+
return isMatch(text, format)
|
|
28
|
+
})
|
|
29
|
+
if (match.indexOf(true) > -1) {
|
|
30
|
+
return zonedTimeToUtc(
|
|
31
|
+
parse(text, commonDateFormats[match.indexOf(true)], new Date()),
|
|
32
|
+
loc
|
|
33
|
+
).toJSON()
|
|
34
|
+
} else {
|
|
35
|
+
return null
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function parseTime(text) {
|
|
40
|
+
const match = commonTimeFormats.map((format) => {
|
|
41
|
+
return isMatch(text, format)
|
|
42
|
+
})
|
|
43
|
+
if (match.indexOf(true) > -1) {
|
|
44
|
+
const time = zonedTimeToUtc(
|
|
45
|
+
parse(text, commonTimeFormats[match.indexOf(true)], new Date()),
|
|
46
|
+
loc
|
|
47
|
+
)
|
|
48
|
+
console.log(time)
|
|
49
|
+
return formatInTimeZone(time, loc, 'HH:mm')
|
|
50
|
+
} else {
|
|
51
|
+
return null
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function parseList(list) {
|
|
56
|
+
return list.children
|
|
57
|
+
.map((item) => {
|
|
58
|
+
const listItem = {}
|
|
59
|
+
if (item.type === 'list') {
|
|
60
|
+
return parseList(list)
|
|
61
|
+
} else if (item.type === 'listItem') {
|
|
62
|
+
listItem.checked = item.checked
|
|
63
|
+
return item.children
|
|
64
|
+
.map((child) => {
|
|
65
|
+
if (child.type === 'paragraph') {
|
|
66
|
+
listItem.text = child.children
|
|
67
|
+
.map((c) => {
|
|
68
|
+
if (c.type === 'link') {
|
|
69
|
+
return c.children[0].value
|
|
70
|
+
} else {
|
|
71
|
+
return c.value
|
|
72
|
+
}
|
|
73
|
+
})
|
|
74
|
+
.filter((x) => !!x)
|
|
75
|
+
.join('')
|
|
76
|
+
return listItem
|
|
77
|
+
}
|
|
78
|
+
})
|
|
79
|
+
.filter((x) => !!x)
|
|
80
|
+
}
|
|
81
|
+
})
|
|
82
|
+
.filter((x) => !!x)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export default async function parseMD(body) {
|
|
86
|
+
const tokens = await unified().use(remarkParse).use(remarkGfm).parse(body)
|
|
87
|
+
if (!tokens) {
|
|
88
|
+
return []
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const r = []
|
|
92
|
+
for (let idx = 0; idx < tokens.children.length; idx = idx + 2) {
|
|
93
|
+
const current = tokens.children[idx]
|
|
94
|
+
const hasNext = idx + 1 < tokens.children.length
|
|
95
|
+
|
|
96
|
+
const obj = {}
|
|
97
|
+
if (current.type === 'heading') {
|
|
98
|
+
obj.id = slugify(current.children[0].value)
|
|
99
|
+
obj.title = current.children[0].value
|
|
100
|
+
if (hasNext) {
|
|
101
|
+
const next = tokens.children[idx + 1]
|
|
102
|
+
if (next.type === 'list') {
|
|
103
|
+
obj.list = parseList(next).flat()
|
|
104
|
+
}
|
|
105
|
+
obj.text = await unified()
|
|
106
|
+
.use(remarkGfm)
|
|
107
|
+
.use(remarkStringify)
|
|
108
|
+
.stringify(next)
|
|
109
|
+
const date = parseDate(obj.text)
|
|
110
|
+
const time = parseTime(obj.text)
|
|
111
|
+
if (date) {
|
|
112
|
+
obj.date = date
|
|
113
|
+
}
|
|
114
|
+
if (time) {
|
|
115
|
+
obj.time = time
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
r.push(obj)
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
return r
|
|
123
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
import t from 'tap'
|
|
4
|
+
import fn from '../src/parse.js'
|
|
5
|
+
import { readFile } from 'fs/promises'
|
|
6
|
+
import { join } from 'path'
|
|
7
|
+
|
|
8
|
+
const test = t.test
|
|
9
|
+
|
|
10
|
+
test('parse(md) should parse GitHub Issue Form data into useful, structured data', async (t) => {
|
|
11
|
+
const expected = [
|
|
12
|
+
{
|
|
13
|
+
id: 'event-description',
|
|
14
|
+
title: 'Event Description',
|
|
15
|
+
text: "Let's meet for coffee and chat about tech, coding, Cyprus and the newly formed\nCDC (Cyprus Developer Community).\n"
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
id: 'location',
|
|
19
|
+
title: 'Location',
|
|
20
|
+
text: '[Cafe Nero Finikoudes, Larnaka](https://goo.gl/maps/Bzjxdeat3BSdsUSVA)\n'
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
id: 'date',
|
|
24
|
+
title: 'Date',
|
|
25
|
+
text: '11.03.2022\n',
|
|
26
|
+
date: '2022-03-11T00:00:00.000Z'
|
|
27
|
+
},
|
|
28
|
+
{ id: 'time', title: 'Time', text: '16:00\n', time: '16:00' },
|
|
29
|
+
{ id: 'duration', title: 'Duration', text: '2h\n' },
|
|
30
|
+
{
|
|
31
|
+
id: 'list-item-checked',
|
|
32
|
+
title: 'List Item Checked',
|
|
33
|
+
list: [
|
|
34
|
+
{
|
|
35
|
+
checked: true,
|
|
36
|
+
text: "I agree to follow this project's\nCode of Conduct"
|
|
37
|
+
}
|
|
38
|
+
],
|
|
39
|
+
text: "* [x] I agree to follow this project's\n [Code of Conduct](https://berlincodeofconduct.org)\n"
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
id: 'list-item-unchecked',
|
|
43
|
+
title: 'List Item Unchecked',
|
|
44
|
+
list: [
|
|
45
|
+
{
|
|
46
|
+
checked: false,
|
|
47
|
+
text: "I agree to follow this project's\nCode of Conduct"
|
|
48
|
+
}
|
|
49
|
+
],
|
|
50
|
+
text: "* [ ] I agree to follow this project's\n [Code of Conduct](https://berlincodeofconduct.org)\n"
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
id: 'mixed-task-list',
|
|
54
|
+
title: 'Mixed Task List',
|
|
55
|
+
list: [
|
|
56
|
+
{ checked: true, text: 'checked' },
|
|
57
|
+
{ checked: false, text: 'unchecked' },
|
|
58
|
+
{ checked: true, text: 'checked 2' },
|
|
59
|
+
{ checked: true, text: 'checked 3' },
|
|
60
|
+
{ checked: false, text: 'unchecked 2' }
|
|
61
|
+
],
|
|
62
|
+
text: '* [x] checked\n* [ ] unchecked\n* [x] checked 2\n* [x] checked 3\n* [ ] unchecked 2\n'
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
id: 'complex-list',
|
|
66
|
+
title: 'Complex List',
|
|
67
|
+
list: [
|
|
68
|
+
{ checked: null, text: 'one' },
|
|
69
|
+
{ checked: null, text: 'two' }
|
|
70
|
+
],
|
|
71
|
+
text: '* one\n* two\n * three\n * four\n 1. five\n 2. six\n'
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
id: 'repositories',
|
|
75
|
+
title: 'Repositories',
|
|
76
|
+
text: '```csv\nhttps://example.com/repository-1\nhttps://example.com/repository-2\n```\n'
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
id: 'visibility',
|
|
80
|
+
title: 'Visibility',
|
|
81
|
+
text: 'Internal\n'
|
|
82
|
+
}
|
|
83
|
+
]
|
|
84
|
+
|
|
85
|
+
const md = await readFile(
|
|
86
|
+
join(process.cwd(), 'test', 'test-issue-1.md'),
|
|
87
|
+
'utf8'
|
|
88
|
+
)
|
|
89
|
+
const actual = await fn(md)
|
|
90
|
+
// console.log(JSON.stringify(actual, null, 0))
|
|
91
|
+
t.deepEqual(actual, expected)
|
|
92
|
+
})
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
### Event Description
|
|
2
|
+
|
|
3
|
+
Let's meet for coffee and chat about tech, coding, Cyprus and the newly formed
|
|
4
|
+
CDC (Cyprus Developer Community).
|
|
5
|
+
|
|
6
|
+
### Location
|
|
7
|
+
|
|
8
|
+
[Cafe Nero Finikoudes, Larnaka](https://goo.gl/maps/Bzjxdeat3BSdsUSVA)
|
|
9
|
+
|
|
10
|
+
### Date
|
|
11
|
+
|
|
12
|
+
11.03.2022
|
|
13
|
+
|
|
14
|
+
### Time
|
|
15
|
+
|
|
16
|
+
16:00
|
|
17
|
+
|
|
18
|
+
### Duration
|
|
19
|
+
|
|
20
|
+
2h
|
|
21
|
+
|
|
22
|
+
### List Item Checked
|
|
23
|
+
|
|
24
|
+
- [x] I agree to follow this project's
|
|
25
|
+
[Code of Conduct](https://berlincodeofconduct.org)
|
|
26
|
+
|
|
27
|
+
### List Item Unchecked
|
|
28
|
+
|
|
29
|
+
- [ ] I agree to follow this project's
|
|
30
|
+
[Code of Conduct](https://berlincodeofconduct.org)
|
|
31
|
+
|
|
32
|
+
### Mixed Task List
|
|
33
|
+
|
|
34
|
+
- [x] checked
|
|
35
|
+
- [ ] unchecked
|
|
36
|
+
- [x] checked 2
|
|
37
|
+
- [x] checked 3
|
|
38
|
+
- [ ] unchecked 2
|
|
39
|
+
|
|
40
|
+
### Complex List
|
|
41
|
+
|
|
42
|
+
- one
|
|
43
|
+
- two
|
|
44
|
+
- three
|
|
45
|
+
- four
|
|
46
|
+
1. five
|
|
47
|
+
2. six
|
|
48
|
+
|
|
49
|
+
### Repositories
|
|
50
|
+
|
|
51
|
+
```csv
|
|
52
|
+
https://example.com/repository-1
|
|
53
|
+
https://example.com/repository-2
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### Visibility
|
|
57
|
+
|
|
58
|
+
Internal
|