Skip to content

Commit 3a8bb3f

Browse files
authored
Fix markdown export (#389)
1 parent 5c1b292 commit 3a8bb3f

File tree

1 file changed

+63
-39
lines changed

1 file changed

+63
-39
lines changed

src/utils/exportAs/documentation.js

+63-39
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,23 @@ import { jsonToMermaid } from "./mermaid";
33
import { databases } from "../../data/databases";
44

55
export function jsonToDocumentation(obj) {
6-
76
const documentationSummary = obj.tables
87
.map((table) => {
98
return `\t- [${table.name}](#${table.name})`;
10-
}).join("\n");
9+
})
10+
.join("\n");
1111

1212
const documentationEntities = obj.tables
1313
.map((table) => {
1414
let enums = "";
15-
let indexes = table.indices.length > 0 ? table.indices.map((index) => {
16-
return `| ${index.name} | ${index.unique ? "✅" : ""} | ${index.fields.join(", ")} |`;
17-
}).join("\n") : "";
15+
let indexes =
16+
table.indices.length > 0
17+
? table.indices
18+
.map((index) => {
19+
return `| ${index.name} | ${index.unique ? "✅" : ""} | ${index.fields.join(", ")} |`;
20+
})
21+
.join("\n")
22+
: "";
1823
const fields = table.fields
1924
.map((field) => {
2025
const fieldType =
@@ -25,46 +30,65 @@ export function jsonToDocumentation(obj) {
2530
field.size !== ""
2631
? "(" + field.size + ")"
2732
: "");
28-
enums += (field.type === "ENUM" && field.values && field.values.length > 0) ?
29-
`##### ${field.name}\n\n${field.values.map((index) => `- ${index}`).join("\n")}\n` : "";
30-
return `| **${field.name}** | ${fieldType} | ${field.primary ? "🔑 PK, " : ""}` +
31-
`${field.nullable ? "null " : "not null "}${field.unique ? ", unique" : ""}${field.increment?", autoincrement":""}` +
33+
enums +=
34+
field.type === "ENUM" && field.values && field.values.length > 0
35+
? `##### ${field.name}\n\n${field.values.map((index) => `- ${index}`).join("\n")}\n`
36+
: "";
37+
return (
38+
`| **${field.name}** | ${fieldType} | ${field.primary ? "🔑 PK, " : ""}` +
39+
`${field.notNull ? "not null" : "null"}${field.unique ? ", unique" : ""}${field.increment ? ", autoincrement" : ""}` +
3240
`${field.default ? `, default: ${field.default}` : ""} | ` +
3341
`${relationshipByField(table.id, obj.relationships, field.id)}` +
34-
` |${field.comment ? field.comment : ""} |`;
35-
}).join("\n");
36-
return `### ${table.name}\n${table.comment ? table.comment : ""}\n` +
42+
` |${field.comment ? field.comment : ""} |`
43+
);
44+
})
45+
.join("\n");
46+
return (
47+
`### ${table.name}\n${table.comment ? table.comment : ""}\n` +
3748
`| Name | Type | Settings | References | Note |\n` +
3849
`|-------------|---------------|-------------------------------|-------------------------------|--------------------------------|\n` +
3950
`${fields} \n${enums.length > 0 ? "\n#### Enums\n" + enums : ""}\n` +
40-
`${indexes.length > 0 ? "\n#### Indexes\n| Name | Unique | Fields |\n|------|--------|--------|\n" + indexes : ""}`;
41-
}).join("\n");
42-
43-
function relationshipByField(table, relationships, fieldId) {
44-
return relationships.filter(r => r.startTableId === table && r.startFieldId === fieldId)
45-
.map((rel) => rel.name);
51+
`${indexes.length > 0 ? "\n#### Indexes\n| Name | Unique | Fields |\n|------|--------|--------|\n" + indexes : ""}`
52+
);
53+
})
54+
.join("\n");
4655

56+
function relationshipByField(table, relationships, fieldId) {
57+
return relationships
58+
.filter((r) => r.startTableId === table && r.startFieldId === fieldId)
59+
.map((rel) => rel.name);
4760
}
48-
61+
4962
const documentationRelationships = obj.relationships?.length
5063
? obj.relationships
51-
.map((r) => {
52-
const startTable = obj.tables[r.startTableId].name;
53-
const endTable = obj.tables[r.endTableId].name;
54-
return `- **${startTable} to ${endTable}**: ${r.cardinality}\n`;
55-
}).join("") : "";
56-
57-
const documentationTypes = databases[obj.database].hasTypes && obj.types.length > 0 ? obj.types.map((type) => {
58-
return `| Name | fields | Note |\n` +
59-
`|-------------|---------------|--------------------------------|\n` +
60-
`| ${type.name} | ${type.fields.map((field) => field.name).join(", ")} | ${type.comment ? type.comment : ""} |`;
61-
}).join("\n") : "";
62-
63-
return `# ${obj.title} documentation\n## Summary\n\n- [Introduction](#introduction)\n- [Database Type](#database-type)\n`+
64-
`- [Table Structure](#table-structure)\n${documentationSummary}\n- [Relationships](#relationships)\n- [Database Diagram](#database-Diagram)\n\n`+
65-
`## Introduction\n\n## Database type\n\n- **Database system:** `+
66-
`${databases[obj.database].name}\n## Table structure\n\n${documentationEntities}`+
67-
`\n## Relationships\n\n${documentationRelationships}\n` +
68-
`${databases[obj.database].hasTypes && obj.types.length > 0 ? `## Types\n\n` + documentationTypes + `\n\n` : "" }` +
69-
`## Database Diagram\n\n\`\`\`mermaid\n${jsonToMermaid(obj)}\n\`\`\``;
70-
}
64+
.map((r) => {
65+
const startTable = obj.tables[r.startTableId].name;
66+
const endTable = obj.tables[r.endTableId].name;
67+
return `- **${startTable} to ${endTable}**: ${r.cardinality}\n`;
68+
})
69+
.join("")
70+
: "";
71+
72+
const documentationTypes =
73+
databases[obj.database].hasTypes && obj.types.length > 0
74+
? obj.types
75+
.map((type) => {
76+
return (
77+
`| Name | fields | Note |\n` +
78+
`|-------------|---------------|--------------------------------|\n` +
79+
`| ${type.name} | ${type.fields.map((field) => field.name).join(", ")} | ${type.comment ? type.comment : ""} |`
80+
);
81+
})
82+
.join("\n")
83+
: "";
84+
85+
return (
86+
`# ${obj.title} documentation\n## Summary\n\n- [Introduction](#introduction)\n- [Database Type](#database-type)\n` +
87+
`- [Table Structure](#table-structure)\n${documentationSummary}\n- [Relationships](#relationships)\n- [Database Diagram](#database-Diagram)\n\n` +
88+
`## Introduction\n\n## Database type\n\n- **Database system:** ` +
89+
`${databases[obj.database].name}\n## Table structure\n\n${documentationEntities}` +
90+
`\n## Relationships\n\n${documentationRelationships}\n` +
91+
`${databases[obj.database].hasTypes && obj.types.length > 0 ? `## Types\n\n` + documentationTypes + `\n\n` : ""}` +
92+
`## Database Diagram\n\n\`\`\`mermaid\n${jsonToMermaid(obj)}\n\`\`\``
93+
);
94+
}

0 commit comments

Comments
 (0)