-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
122 lines (98 loc) · 2.98 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
import * as Commonmark from 'commonmark';
import fs from 'fs';
import { LinkType } from './types';
import { FileLink } from './types';
// const goalFileLinks: FileLink[] = getFileLinks(LinkType.GOAL, '../goals');
// console.log(goalFileLinks);
function getFileLinks(linkType:LinkType, filePath:string) {
const parser = new Commonmark.Parser();
const goalFiles = fs.readdirSync(filePath);
let fileLinks: FileLink[] = [];
goalFiles.forEach(fileName => {
if (fileName.endsWith('template.md')) return;
const data = fs.readFileSync(`${filePath}/${fileName}`, 'utf-8');
const parsed = parser.parse(data);
const links = getLinks(parsed);
const fileLink: FileLink = {
type: linkType,
file: fileName,
links
};
fileLinks.push(fileLink);
});
return fileLinks;
}
// const questions = fs.readdirSync('../questions');
// const metrics = fs.readdirSync('../metrics');
// const data = fs.readFileSync('../goals/find-projects.md', 'utf-8');
// const parsed = parser.parse(data);
// const links = getLinks();
// console.log(links);
export function getLinks(parsed: Commonmark.Node) {
const walker = parsed.walker();
let event, node;
let links = [];
while ((event = walker.next())) {
node = event.node;
if (event.entering && node.type === 'link') {
const link = {
url: node.destination,
text: node.firstChild?.literal
}
links.push(link);
}
}
return links;
}
function getNodeShapeSyntax(node: { id?: string; shape: any; label: any; }) {
switch (node.shape) {
case 'rect':
return `[${node.label}]`;
case 'circ':
return `((${node.label}))`;
case 'roundrect':
return `((${node.label}))`;
case 'diamond':
return `{${node.label}}`;
default:
return `[${node.label}]`;
}
}
function getArrowTypeSyntax(edge: { from?: string; to?: string; type: any; }) {
switch (edge.type) {
case 'arrow':
return '-->';
case 'dashed':
return '-.->';
case 'thick':
return '==>';
default:
return '-->';
}
}
function generateMermaidDiagram() {
const nodes = [
{ id: 'Goal1', shape: 'roundrect', label: 'Goal1' },
{ id: 'Question1', shape: 'diamond', label: 'Question2' },
{ id: 'Metric1', shape: 'circ', label: 'Metric1' },
{ id: 'Goal4', shape: 'roundrect', label: 'Goal4' },
];
const edges = [
{ from: 'Goal1', to: 'Question1', type: 'arrow' },
{ from: 'Goal1', to: 'Metric1', type: 'arrow' },
{ from: 'Question1', to: 'Metric1', type: 'dashed' },
{ from: 'Metric1', to: 'Goal1', type: 'thick' },
];
let mermaidSyntax = "```mermaid\ngraph LR\n";
nodes.forEach(node => {
const nodeSyntax = getNodeShapeSyntax(node);
mermaidSyntax += `${node.id}${nodeSyntax}\n`;
});
edges.forEach(edge => {
const arrowSyntax = getArrowTypeSyntax(edge);
mermaidSyntax += `${edge.from}${arrowSyntax}${edge.to}\n`;
});
mermaidSyntax += '\n```';
return mermaidSyntax;
}
console.log(generateMermaidDiagram())