-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
152 lines (139 loc) · 4.2 KB
/
index.ts
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import { type Plugin } from "unified";
import { type Root, type Code } from "mdast";
import { visit } from "unist-util-visit";
import * as Path from "path";
import fetch from "node-fetch";
// eslint-disable-next-line
export type Options = {
// If this string is detected in a link text, the link will be replaced with a code snippet
// Default: "inline"
inlineMarker?: string;
// The comment placed on top of the linked code snippet, <url> will be replaced with the URL
// of the link. If undefined, no comment will be added.
// Default: undefined
originComment?: string;
};
const DEFAULT_SETTINGS: Options = {
inlineMarker: "inline",
};
type InlineSnippet = {
inline: (codeblock: Code) => void;
url: string;
};
const RemarkInlineGithubCodeSnippet: Plugin<[Options?], Root> = (
options: Options | undefined,
) => {
const settings = Object.assign({}, DEFAULT_SETTINGS, options);
return async (tree) => {
const inlineSnippets: InlineSnippet[] = [];
// find all snippets
visit(tree, (node, index, parent) => {
// Search for links starting with github.com and containing the inlineMarker
if (
node.type === "link" &&
node.children[0].type === "text" &&
node.children[0].value === settings.inlineMarker &&
node.url.startsWith("https://github.com")
) {
inlineSnippets.push({
url: node.url,
inline: (codeblock) => {
parent!.children[index!] = codeblock;
},
});
}
});
// replace all snippets
await Promise.all(
inlineSnippets.map(async (snippet) => {
// e.g. https://github.com/hashicorp/terraform/blob/main/internal/promising/promise.go#L30-L41
const hashParts = new URL(snippet.url).hash.replace("#", "").split("-");
if (hashParts.length != 2) {
throw new Error(
`Inlining snippet points to ${snippet.url} with an invalid hash. Expected #L<number>-L<number>`,
);
}
const [start, end] = hashParts.map((p) => parseInt(p.replace("L", ""), 10));
const content = await fetch(
snippet.url
.replace("https://github.com", "https://raw.githubusercontent.com")
.replace("blob", ""),
).then((res) => res.text());
const ext = Path.parse(new URL(snippet.url).pathname.split("/").slice(5).join("/")).ext;
const snippetContent = content
.split("\n")
.slice(start - 1, end)
.join("\n");
snippet.inline({
type: "code",
lang: pathExtensionToMarkdownLanguageTag(ext),
value: settings.originComment
? `${commentOutBasedOnLanguage(ext, settings.originComment.replaceAll("<url>", snippet.url))}` +
snippetContent
: snippetContent,
});
}),
);
};
};
type LanguageHandler = {
markdown: string;
comment: (comment: string) => string;
};
export const supportedLanguageExtensions: Record<string, LanguageHandler> = {
".js": {
markdown: "javascript",
comment: (c) => `// ${c}\n`,
},
".ts": {
markdown: "typescript",
comment: (c) => `// ${c}\n`,
},
".py": {
markdown: "python",
comment: (c) => `# ${c}\n`,
},
".sh": {
markdown: "bash",
comment: (c) => `# ${c}\n`,
},
".json": {
markdown: "json",
comment: () => "",
},
".yaml": {
markdown: "yaml",
comment: (c) => `# ${c}\n`,
},
".yml": {
markdown: "yaml",
comment: (c) => `# ${c}\n`,
},
".tf": {
markdown: "terraform",
comment: (c) => `# ${c}\n`,
},
".hcl": {
markdown: "hcl",
comment: (c) => `# ${c}\n`,
},
".tfstacks.hcl": {
markdown: "hcl",
comment: (c) => `# ${c}\n`,
},
".go": {
markdown: "go",
comment: (c) => `// ${c}\n`,
},
} as const;
const defaultLanguageExtension: LanguageHandler = {
markdown: "",
comment: (c) => `// ${c}\n`,
};
export function commentOutBasedOnLanguage(ext: string, code: string) {
return (supportedLanguageExtensions[ext] || defaultLanguageExtension).comment(code);
}
export function pathExtensionToMarkdownLanguageTag(ext: string) {
return (supportedLanguageExtensions[ext] || defaultLanguageExtension).markdown;
}
export default RemarkInlineGithubCodeSnippet;