forked from mermaid-js/mermaid-live-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gist-utils.js
29 lines (25 loc) · 735 Bytes
/
gist-utils.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
import { Octokit } from '@octokit/rest';
const octokit = new Octokit({
userAgent: 'mermaid-live-editor',
baseUrl: 'https://api.github.com',
});
const isValidGist = (files) => {
return 'config.json' in files && 'mermaid.diagram' in files;
};
const getFileContent = async (file) => {
if (file.truncated) {
return await (await fetch(file.raw_url)).text();
}
return file.content;
};
export const getMermaidData = async (gist_id) => {
const gist = await octokit.gists.get({
gist_id,
});
const files = gist.data.files;
if (isValidGist(files)) {
const config = await getFileContent(files['config.json']);
const code = await getFileContent(files['mermaid.diagram']);
return { config, code };
}
};