generated from custom-cards/boilerplate-card
-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
discussion-index.mjs
131 lines (120 loc) · 3.13 KB
/
discussion-index.mjs
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { Octokit } from "@octokit/core";
const owner = "dbuezas";
const repo = "lovelace-plotly-graph-card";
const auth = "github_pat_xxx"; // create from https://github.com/settings/tokens
import fetch from "node-fetch";
const octokit = new Octokit({
request: {
fetch: fetch,
},
auth,
});
async function fetchDiscussions(owner, repo, cursor) {
const query = `
query ($owner: String!, $repo: String!, $cursor: String) {
repository(owner: $owner, name: $repo) {
discussions(first: 100, after: $cursor) {
totalCount
pageInfo {
endCursor
hasNextPage
}
nodes {
id
title
url
body
comments(first: 70) {
totalCount
nodes {
id
body
createdAt
replies(first: 70) {
totalCount
nodes {
body
}
}
}
pageInfo {
endCursor
hasNextPage
}
}
}
}
}
}
`;
const response = await octokit.graphql(query, {
owner,
repo,
cursor,
});
// console.log(JSON.stringify(response, 0, 2));
function extractImageUrls(markdown) {
const imageUrls = [];
// This regex specifically matches URLs starting with the specified GitHub path
const regexes = [/!\[.*?\]\((.*?)\)/g, /src="([^"]+)"/g];
for (const regex of regexes) {
let match;
while ((match = regex.exec(markdown)) !== null) {
imageUrls.push(match[1]);
}
}
return imageUrls;
}
const data = response.repository.discussions.nodes
.map(({ title, url, body, comments }) => {
const images = extractImageUrls(body);
for (const comment of comments.nodes) {
images.push(...extractImageUrls(comment.body));
for (const reply of comment.replies.nodes) {
images.push(...extractImageUrls(reply.body));
}
}
return { title, url, images };
})
.filter(({ images }) => images.length);
const md = data.map(({ title, url, images }) => {
let groups = [];
let group = [];
groups.push(group);
for (let i = 0; i < images.length; i++) {
if (i % 4 === 0) {
group = [];
groups.push(group);
}
group.push(images[i]);
}
let txt = "";
for (const group of groups) {
if (group.length === 0) continue;
txt += "<tr>\n";
for (const img of group) {
txt += `<td><img src="${img}" width="200" /></td>\n`;
}
txt += "</tr>\n";
}
return `
## [${title}](${url})
<table>
${txt}
</table>
---`;
});
return {
md: md.join("\n"),
next: response.repository.discussions.pageInfo.endCursor,
};
}
let { md, next } = await fetchDiscussions(owner, repo, null);
let result = md;
while (next) {
console.log(next);
let xx = await fetchDiscussions(owner, repo, next);
result += "\n" + xx.md;
next = xx.next;
}
console.log(result);