Skip to content

Commit

Permalink
[UPD] refactor and clean up function + add todo
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon-Busch committed Oct 24, 2023
1 parent 92a2923 commit 3f46ffd
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 31 deletions.
6 changes: 5 additions & 1 deletion bin/threatdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ async function main () {
const inputFile = positionals.shift();
// non-null assertion safe because the outputType has a default
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
if (!inputFile || !["json", "mermaid", "svg"].includes(values.type!)) {
if (!inputFile || !["json", "mermaid", "svg"].includes(values.type!)) { // TODO add md as option here
usage();
} else {
const fileContent = readFileSync(resolve(process.cwd(), inputFile), { encoding: "utf8" });
Expand Down Expand Up @@ -71,6 +71,10 @@ async function main () {
console.log(svgContent);
}
}

if (values.type === "md") {
// TODO
}
}
}

Expand Down
39 changes: 9 additions & 30 deletions lib/md-converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@ import { parse } from "./parser/index";
import { compileToMermaid } from "./compiler";
import { renderMermaid } from "./renderer";
import fs from "fs";
// TODO
// remove this, local dev
const file = fs.readFileSync("./test.md", "utf8");
import { threatdownRegex } from "./parser/helpers";

const getMermaid = async (mermaidFormatedData: string): Promise<string> => {
const testSvg = await renderMermaid(mermaidFormatedData);
return testSvg;
};

// This function will actually crete the svg and json files
// This function will actually crete the svg and json files and write to system
const getThreatdownContent = (trimmedContent: string) => {
const jsonFormatedData = parse(trimmedContent);
writeFileType(JSON.stringify(jsonFormatedData), "json");
Expand Down Expand Up @@ -45,31 +43,21 @@ const processMatchAsync = async (match: string) => {
const jsonFormatedData = parse(cleanMatch);
const mermaidData = compileToMermaid(jsonFormatedData);

// Define an asynchronous function to get the Mermaid SVG
const getMermaidAsync = async (mermaidData: string) => {
try {
const res = await getMermaid(mermaidData);
return res;
} catch (err) {
console.error(err);
return "";
}
};

const mermaidSvg = await getMermaidAsync(mermaidData);
const mermaidSvg = await getMermaid(mermaidData);

return (
`<!-- ${match} --> \n` +
"## JSON \n" +
// Render JSON
"## JSON \n" +
"```json\n" +
JSON.stringify(jsonFormatedData) +
"\n```\n" +
"## MERMAID \n" +
// Render Mermaid
"## MERMAID \n" +
"```mermaid\n" +
mermaidData +
"\n```\n" +
// Render SVG
"## SVG \n" +
"\n" +
mermaidSvg +
Expand All @@ -78,23 +66,14 @@ const processMatchAsync = async (match: string) => {
};

// This function takes a file as input and update it adding the svg and json
const generateUpdateMd = (file: string) => {
const threatdownRegex = /```threatdown([\s\S]*?)```/g;
const generateUpdatedMd = (filePath: string) => {
const file = fs.readFileSync(filePath, "utf8");
const threatdownMatches = file.match(threatdownRegex);
console.log(threatdownMatches);

if (!threatdownMatches) {
throw new Error("No threatdown content found");
}

threatdownMatches.forEach((threatdownContent: string) => {
const trimmedContent = threatdownContent
.trim()
.replace(/^```threatdown/, "")
.replace(/```$/, "");
console.log("trimmedContent", trimmedContent);
});

const newFilePromises = threatdownMatches.map(processMatchAsync);
Promise.all(newFilePromises)
.then((newFileContentArray) => {
Expand All @@ -115,4 +94,4 @@ const generateUpdateMd = (file: string) => {

// TODO
// remove this, local dev
generateUpdateMd(file);
generateUpdatedMd("./test.md");
2 changes: 2 additions & 0 deletions lib/parser/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,5 @@ export function parseLine (line: string | string[]): string {

return line;
}

export const threatdownRegex = /```threatdown([\s\S]*?)```/g;

0 comments on commit 3f46ffd

Please sign in to comment.