Skip to content

Commit 7233ec9

Browse files
committed
✨ update contribution parser
1 parent 54dbbbf commit 7233ec9

1 file changed

Lines changed: 27 additions & 31 deletions

File tree

  • packages/github-user-contribution

packages/github-user-contribution/index.ts

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -39,40 +39,36 @@ export const getGithubUserContribution = async (
3939
};
4040

4141
const parseUserPage = (content: string) => {
42-
// take roughly the svg block
42+
// take roughly the table block
4343
const block = content
44-
.split(`class="js-calendar-graph-svg"`)[1]
45-
.split("</svg>")[0];
46-
47-
let x = 0;
48-
let lastYAttribute = 0;
49-
50-
const rects = Array.from(block.matchAll(/<rect[^>]*>[^<]*<\/rect>/g)).map(
51-
([m]) => {
52-
const date = m.match(/data-date="([^"]+)"/)![1];
53-
const level = +m.match(/data-level="([^"]+)"/)![1];
54-
const yAttribute = +m.match(/y="([^"]+)"/)![1];
55-
56-
const literalCount = m.match(/(No|\d+) contributions? on/)![1];
57-
const count = literalCount === "No" ? 0 : +literalCount;
58-
59-
if (lastYAttribute > yAttribute) x++;
60-
61-
lastYAttribute = yAttribute;
62-
63-
return { date, count, level, x, yAttribute };
64-
}
44+
.split(`aria-describedby="contribution-graph-description"`)[1]
45+
.split("<tbody>")[1]
46+
.split("</tbody>")[0];
47+
48+
const cells = block.split("</tr>").flatMap((inside, y) =>
49+
inside.split("</td>").flatMap((m) => {
50+
const date = m.match(/data-date="([^"]+)"/)?.[1];
51+
52+
const literalLevel = m.match(/data-level="([^"]+)"/)?.[1];
53+
const literalX = m.match(/data-ix="([^"]+)"/)?.[1];
54+
const literalCount = m.match(/(No|\d+) contributions? on/)?.[1];
55+
56+
if (date && literalLevel && literalX && literalCount)
57+
return [
58+
{
59+
x: +literalX,
60+
y,
61+
62+
date,
63+
count: +literalCount,
64+
level: +literalLevel,
65+
},
66+
];
67+
68+
return [];
69+
})
6570
);
6671

67-
const yAttributes = Array.from(
68-
new Set(rects.map((c) => c.yAttribute)).keys()
69-
).sort();
70-
71-
const cells = rects.map(({ yAttribute, ...c }) => ({
72-
y: yAttributes.indexOf(yAttribute),
73-
...c,
74-
}));
75-
7672
return cells;
7773
};
7874

0 commit comments

Comments
 (0)