Skip to content

Commit f71f41d

Browse files
committed
Add documentation menu and generate documentation markdown
1 parent 1139ad4 commit f71f41d

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

src/components/EditorHeader/ControlPanel.jsx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ import { exportSQL } from "../../utils/exportSQL";
7070
import { databases } from "../../data/databases";
7171
import { jsonToMermaid } from "../../utils/exportAs/mermaid";
7272
import { isRtl } from "../../i18n/utils/rtl";
73+
import { jsonToDocumentation } from "../../utils/exportAs/documentation";
7374

7475
export default function ControlPanel({
7576
diagramId,
@@ -1064,6 +1065,29 @@ export default function ControlPanel({
10641065
],
10651066
function: () => {},
10661067
},
1068+
Documentation: {
1069+
children: [
1070+
{
1071+
Markdown: () => {
1072+
setModal(MODAL.CODE);
1073+
const result = jsonToDocumentation({
1074+
tables: tables,
1075+
relationships: relationships,
1076+
notes: notes,
1077+
subjectAreas: areas,
1078+
database: database,
1079+
title: title,
1080+
});
1081+
setExportData((prev) => ({
1082+
...prev,
1083+
data: result,
1084+
extension: "md",
1085+
}));
1086+
}
1087+
}
1088+
],
1089+
function: () => {},
1090+
},
10671091
exit: {
10681092
function: () => {
10691093
save();

src/utils/exportAs/documentation.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { dbToTypes } from "../../data/datatypes";
2+
import { jsonToMermaid } from "./mermaid";
3+
4+
export function jsonToDocumentation(obj) {
5+
6+
const documentationSummary = obj.tables
7+
.map((table) => {
8+
return `\t- [${table.name}](#${table.name})\n`;
9+
}).join("");
10+
11+
const documentationEntities = obj.tables
12+
.map((table) => {
13+
const fields = table.fields
14+
.map((field) => {
15+
const fieldType =
16+
field.type +
17+
((dbToTypes[obj.database][field.type].isSized ||
18+
dbToTypes[obj.database][field.type].hasPrecision) &&
19+
field.size &&
20+
field.size !== ""
21+
? "(" + field.size + ")"
22+
: "");
23+
return `| **${field.name}** | ${fieldType} | ${field.primary ? "🔑 PK, " : ""}` +
24+
`${field.nullable ? "null " : "not null "}${field.unique ? ", unique" : ""}${field.increment?", autoincrement":""}` +
25+
`${field.default ? `, default: ${field.default}` : ""} | |${field.comment ? field.comment : ""} |`;
26+
27+
}).join("\n");
28+
return `### ${table.name}\n${table.comment ? table.comment : ""}\n` +
29+
`| Name | Type | Settings | References | Note |\n` +
30+
`|-------------|---------------|-------------------------------|-------------------------------|--------------------------------|\n` +
31+
`${fields}\n\n`;
32+
}).join("");
33+
34+
const documentationRelationships = obj.relationships?.length
35+
? obj.relationships
36+
.map((r) => {
37+
console.log(r);
38+
const startTable = obj.tables[r.startTableId].name;
39+
const endTable = obj.tables[r.endTableId].name;
40+
return `- **${startTable} to ${endTable}**: ${r.cardinality} (${r.comment ? r.comment : ""})\n`;
41+
}).join("") : "";
42+
43+
return `# ${obj.title} Database Documentation\n## Summary\n- [Introduction](#introduction)\n- [Database Type](#database-type)\n`+
44+
`- [Table Structure](#table-structure)\n${documentationSummary}\n- [Relationships](#relationships)\n- [Database Diagram](#database-Diagram)\n`+
45+
`## Introduction\n${obj.notes}\n## Database type\n- **Database system:** `+
46+
`${obj.database.type}\n## Table structure\n\n${documentationEntities}`+
47+
`\n\n## Relationships\n${documentationRelationships}\n\n`+
48+
`## Database Diagram\n\`\`\`${jsonToMermaid(obj)}\`\`\``;
49+
}

0 commit comments

Comments
 (0)