Skip to content

Commit ebb1502

Browse files
committed
Improve github-links.ts to properly handle issue references from other repos and orgs
1 parent c89ff06 commit ebb1502

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

.vitepress/github-links.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import MarkdownIt from "markdown-it";
22

3-
const REGEX = /#(\d+)/g;
3+
const DEFAULT_ORG = "Creators-of-Create";
4+
const DEFAULT_REPO = "Create";
5+
const REGEX =
6+
/([A-Za-z0-9_.-]+)\/([A-Za-z0-9_.-]+)#(\d+)|([A-Za-z0-9_.-]+)?#(\d+)/g;
47

58
export default function (md: MarkdownIt) {
69
md.core.ruler.after("linkify", "github-links", (state) => {
@@ -14,7 +17,15 @@ export default function (md: MarkdownIt) {
1417
const text = child.content;
1518

1619
for (const match of text.matchAll(REGEX)) {
17-
const [full, number] = match;
20+
const [
21+
fullMatch,
22+
matchedOrg1,
23+
matchedRepo1,
24+
matchedNum1,
25+
matchedOrg2,
26+
matchedNum2,
27+
] = match;
28+
console.log(match);
1829
const index = match.index;
1930

2031
if (index > last) {
@@ -25,13 +36,17 @@ export default function (md: MarkdownIt) {
2536
});
2637
}
2738

39+
const org = matchedOrg1 ?? matchedOrg2 ?? DEFAULT_ORG;
40+
const repo = matchedRepo1 ?? DEFAULT_REPO;
41+
const number = matchedNum1 ?? matchedNum2;
42+
2843
newChildren.push({
2944
type: "html_inline",
30-
content: `<a href="https://github.com/Creators-of-Create/Create/issues/${number}">#${number}</a>`,
45+
content: `<a href="https://github.com/${org}/${repo}/issues/${number}">${fullMatch}</a>`,
3146
level: child.level,
3247
});
3348

34-
last = index + full.length;
49+
last = index + fullMatch.length;
3550
}
3651

3752
if (last < text.length) {

0 commit comments

Comments
 (0)