From a213ec447060be4426484b0516df169b958ab7da Mon Sep 17 00:00:00 2001 From: vjspranav Date: Sat, 25 Nov 2023 03:07:56 +0530 Subject: [PATCH] Add a very basic Static code checker * Improvise it as we go --- src/components/MonacoEditor.js | 7 +++++-- src/utils/language.js | 25 +++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 src/utils/language.js diff --git a/src/components/MonacoEditor.js b/src/components/MonacoEditor.js index 0afcb94..31680e3 100644 --- a/src/components/MonacoEditor.js +++ b/src/components/MonacoEditor.js @@ -12,6 +12,7 @@ import { makeStyles } from "@material-ui/core"; import { API_URL } from "../Constants"; import { StagBinContext } from "../App"; +import detectLanguage from "../utils/language"; let reqData = {}; @@ -21,6 +22,7 @@ const getData = async ( setOldEncrypted, setEncryptedReadOnly, setOpenPasswordDialog, + setLanguage, id, redirect, base_url, @@ -49,6 +51,7 @@ const getData = async ( setOpenPasswordDialog(reqData.isEncrypted || false); setData(reqData.data); setLoading(false); + if (id.indexOf(".") === -1) setLanguage(detectLanguage(reqData.data)); } if (reqData.url && !redirect) { window.location = reqData.data; @@ -135,8 +138,6 @@ export default function MEditor() { default: break; } - } else { - setLanguage("javascript"); } if (!(!readOnly && edited)) setReadOnly(true); setUrl(id); @@ -147,6 +148,7 @@ export default function MEditor() { setOldEncrypted, setEncryptedReadOnly, setOpenPasswordDialog, + setLanguage, id, redirect, base_url, @@ -154,6 +156,7 @@ export default function MEditor() { setLoading ); } + } else { } } diff --git a/src/utils/language.js b/src/utils/language.js new file mode 100644 index 0000000..9e3ce8e --- /dev/null +++ b/src/utils/language.js @@ -0,0 +1,25 @@ +const detectLanguage = (code) => { + if (code.includes("import java")) { + return "java"; + } else if (code.includes("import ") && code.includes("print(")) { + return "python"; + } else if (code.includes("#include") && code.includes("cout")) { + return "cpp"; + } else if (code.includes("#include")) { + return "c"; + } else if (code.includes("") || code.includes("")) { + return "html"; + } else if (code.includes("func main()")) { + return "go"; + } else if (code.includes("function") && code.includes(";")) { + return "javascript"; + } else if (code.includes("#") && code.includes("-")) { + return "markdown"; + } else if (code.includes("{") && code.includes("}") && code.includes(":")) { + return "css"; + } else { + return "javascript"; + } +}; + +export default detectLanguage;