Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Add Documentation Menu with Markdown Export for Database Documentation #229

21 changes: 21 additions & 0 deletions src/components/EditorHeader/ControlPanel.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ import { exportSQL } from "../../utils/exportSQL";
import { databases } from "../../data/databases";
import { jsonToMermaid } from "../../utils/exportAs/mermaid";
import { isRtl } from "../../i18n/utils/rtl";
import { jsonToDocumentation } from "../../utils/exportAs/documentation";

export default function ControlPanel({
diagramId,
Expand Down Expand Up @@ -1061,6 +1062,26 @@ export default function ControlPanel({
}));
},
},
{
readme: () => {
setModal(MODAL.CODE);
const result = jsonToDocumentation({
tables: tables,
relationships: relationships,
notes: notes,
subjectAreas: areas,
database: database,
title: title,
...(databases[database].hasTypes && { types: types }),
...(databases[database].hasEnums && { enums: enums }),
});
setExportData((prev) => ({
...prev,
data: result,
extension: "md",
}));
}
},
],
function: () => {},
},
Expand Down
1 change: 1 addition & 0 deletions src/i18n/locales/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ const en = {
empty_index_name: "Declared an index with no name in table '{{tableName}}'",
didnt_find_diagram: "Oops! Didn't find the diagram.",
unsigned: "Unsigned",
readme: "README",
},
};

Expand Down
1 change: 1 addition & 0 deletions src/i18n/locales/es.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ const es = {
edit_relationship: "{{extra}} Editar relación {{refName}}",
delete_relationship: "Eliminar relación {{refName}}",
not_found: "No encontrado",
readme: "README",
},
};

Expand Down
1 change: 1 addition & 0 deletions src/i18n/locales/fr.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ const fr = {
edit_relationship: "{{extra}} Modifier la relation {{refName}}",
delete_relationship: "Supprimer la relation {{refName}}",
not_found: "Non trouvé",
readme: "README",
},
};

Expand Down
70 changes: 70 additions & 0 deletions src/utils/exportAs/documentation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { dbToTypes } from "../../data/datatypes";
import { jsonToMermaid } from "./mermaid";
import { databases } from "../../data/databases";

export function jsonToDocumentation(obj) {

const documentationSummary = obj.tables
.map((table) => {
return `\t- [${table.name}](#${table.name})`;
}).join("\n");

const documentationEntities = obj.tables
.map((table) => {
let enums = "";
let indexes = table.indices.length > 0 ? table.indices.map((index) => {
return `| ${index.name} | ${index.unique ? "✅" : ""} | ${index.fields.join(", ")} |`;
}).join("\n") : "";
const fields = table.fields
.map((field) => {
const fieldType =
field.type +
((dbToTypes[obj.database][field.type].isSized ||
dbToTypes[obj.database][field.type].hasPrecision) &&
field.size &&
field.size !== ""
? "(" + field.size + ")"
: "");
enums += (field.type === "ENUM" && field.values && field.values.length > 0) ?
`##### ${field.name}\n\n${field.values.map((index) => `- ${index}`).join("\n")}\n` : "";
return `| **${field.name}** | ${fieldType} | ${field.primary ? "🔑 PK, " : ""}` +
`${field.nullable ? "null " : "not null "}${field.unique ? ", unique" : ""}${field.increment?", autoincrement":""}` +
`${field.default ? `, default: ${field.default}` : ""} | ` +
`${relationshipByField(table.id, obj.relationships, field.id)}` +
` |${field.comment ? field.comment : ""} |`;
}).join("\n");
return `### ${table.name}\n${table.comment ? table.comment : ""}\n` +
`| Name | Type | Settings | References | Note |\n` +
`|-------------|---------------|-------------------------------|-------------------------------|--------------------------------|\n` +
`${fields} \n${enums.length > 0 ? "\n#### Enums\n" + enums : ""}\n` +
`${indexes.length > 0 ? "\n#### Indexes\n| Name | Unique | Fields |\n|------|--------|--------|\n" + indexes : ""}`;
}).join("\n");

function relationshipByField(table, relationships, fieldId) {
return relationships.filter(r => r.startTableId === table && r.startFieldId === fieldId)
.map((rel) => rel.name);

}

const documentationRelationships = obj.relationships?.length
? obj.relationships
.map((r) => {
const startTable = obj.tables[r.startTableId].name;
const endTable = obj.tables[r.endTableId].name;
return `- **${startTable} to ${endTable}**: ${r.cardinality}\n`;
}).join("") : "";

const documentationTypes = databases[obj.database].hasTypes && obj.types.length > 0 ? obj.types.map((type) => {
return `| Name | fields | Note |\n` +
`|-------------|---------------|--------------------------------|\n` +
`| ${type.name} | ${type.fields.map((field) => field.name).join(", ")} | ${type.comment ? type.comment : ""} |`;
}).join("\n") : "";

return `# ${obj.title} documentation\n## Summary\n\n- [Introduction](#introduction)\n- [Database Type](#database-type)\n`+
`- [Table Structure](#table-structure)\n${documentationSummary}\n- [Relationships](#relationships)\n- [Database Diagram](#database-Diagram)\n\n`+
`## Introduction\n\n## Database type\n\n- **Database system:** `+
`${databases[obj.database].name}\n## Table structure\n\n${documentationEntities}`+
`\n## Relationships\n\n${documentationRelationships}\n` +
`${databases[obj.database].hasTypes && obj.types.length > 0 ? `## Types\n\n` + documentationTypes + `\n\n` : "" }` +
`## Database Diagram\n\n\`\`\`mermaid\n${jsonToMermaid(obj)}\n\`\`\``;
}
Loading