-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
73 lines (63 loc) · 1.76 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// @ts-check
import core from '@actions/core'
import { gfmTableToMarkdown } from 'mdast-util-gfm-table'
import { toMarkdown } from 'mdast-util-to-markdown'
// We export the `run` function for testing
// Call `run()` directly if this file is the entry point
// If this file is being run directly
if (import.meta.url.endsWith(process.argv[1])) {
const json = JSON.parse(core.getInput('json'))
const alignPipes = core.getInput('align-pipes') !== 'false'
core.setOutput(
'table',
await run(core, json, alignPipes).catch((error) => {
core.setFailed(error)
})
)
}
/**
* @param {import("@actions/core")} core
* @param {Object} json
* @param {boolean} [alignPipes]
*/
export async function run(core, json, alignPipes) {
function createTableCell(value) {
return {
type: 'tableCell',
children: [{ type: 'text', value: String(value) }]
}
}
function createTableRow(cells) {
return {
type: 'tableRow',
children: cells.map(createTableCell)
}
}
function createTable(headers, rows) {
return {
type: 'table',
align: headers.map(() => 'left'), // Align all columns left
children: [createTableRow(headers), ...rows.map(createTableRow)]
}
}
// Convert JSON to mdast (Markdown Abstract Syntax Tree)
const headers = Object.keys(json[0])
const rows = json.map(Object.values)
const mdast = {
type: 'root',
children: [createTable(headers, rows)]
}
const markdownTable = toMarkdown(mdast, {
extensions: [
gfmTableToMarkdown({
tablePipeAlign: alignPipes
})
]
})
// Add the Markdown table to the summary
core.summary
.addDetails('Generated Markdown table', `\n\n${markdownTable}\n`)
.write()
// Return the Markdown table
return markdownTable
}