demike / TsUML2

Generates UML diagrams from TypeScript source code
MIT License
266 stars 34 forks source link

Add support to output markdown file that has mermaid in it #81

Open ckrauterlovescoffee opened 5 months ago

ckrauterlovescoffee commented 5 months ago

It would be neat to be able to specify an output for a markdown file that gets filled with the mermaid diagram.

ckrauterlovescoffee commented 5 months ago

Here is what I've settled on for now,

# What I run
tsuml2 --outFile ./docs/uml.svg --outMermaidDsl ./docs/mermaid.dsl --glob './src/**/*.ts' --glob './examples/**/*.ts' --memberAssociations && ./scripts/add_mermaid_to_readme.sh
#!/bin/bash
# This script injects the content of 'mermaid.dsl' into the README.md under the 'Diagram' section.

MERMAID_FILE="./docs/mermaid.dsl"
README_FILE="./README.md"
TEMP_FILE="./docs/temp_readme.md"

GREEN="\033[1;32m"
RESET="\033[0m"

cp $README_FILE $TEMP_FILE

if ! grep -q '## Architecture' $TEMP_FILE; then
  echo "\n\n## Architecture" >> $TEMP_FILE
fi

sed -i '' '/## Architecture/,$d' $TEMP_FILE

echo -e '\n\n## Architecture' >> $TEMP_FILE
echo -e '\n\nThis is an auto-generated diagram per the [add_mermaid_to_readme.sh](./scripts/add_mermaid_to_readme.sh) script. The section must be the last section in this file. Any subsequent sections will be destroyed!' >> $TEMP_FILE
echo -e '\n\n```mermaid' >> $TEMP_FILE
cat $MERMAID_FILE >> $TEMP_FILE
echo -e '\n```\n' >> $TEMP_FILE

cp $TEMP_FILE $README_FILE
echo -e "${GREEN}Update complete: README.md has been updated with the contents of $MERMAID_FILE.${RESET}"

rm $TEMP_FILE
rm $MERMAID_FILE
demike commented 4 months ago

@ckrauterlovescoffee I think it would be easier with the public parseProject getMermaidDSL functions You can read the markdown file and replace a specified placeholder with the generated mermaid dsl