{"id":23949734,"url":"https://github.com/liaplayground/custom-code-imports","last_synced_at":"2026-05-17T19:06:20.059Z","repository":{"id":133197446,"uuid":"572939496","full_name":"LiaPlayground/Custom-code-imports","owner":"LiaPlayground","description":"Demo of how to load and display code from another resource","archived":false,"fork":false,"pushed_at":"2022-12-05T13:45:57.000Z","size":7,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-02-24T08:47:25.020Z","etag":null,"topics":["liascript","liascript-template","markdown"],"latest_commit_sha":null,"homepage":"https://liascript.github.io/course/?https://raw.githubusercontent.com/LiaPlayground/Custom-code-imports/main/README.md","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"cc0-1.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/LiaPlayground.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-12-01T11:03:11.000Z","updated_at":"2024-07-04T18:00:47.000Z","dependencies_parsed_at":"2023-03-15T10:32:43.137Z","dependency_job_id":null,"html_url":"https://github.com/LiaPlayground/Custom-code-imports","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/LiaPlayground/Custom-code-imports","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LiaPlayground%2FCustom-code-imports","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LiaPlayground%2FCustom-code-imports/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LiaPlayground%2FCustom-code-imports/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LiaPlayground%2FCustom-code-imports/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/LiaPlayground","download_url":"https://codeload.github.com/LiaPlayground/Custom-code-imports/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LiaPlayground%2FCustom-code-imports/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33151625,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-17T09:28:26.183Z","status":"ssl_error","status_checked_at":"2026-05-17T09:27:52.702Z","response_time":107,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["liascript","liascript-template","markdown"],"created_at":"2025-01-06T11:50:24.357Z","updated_at":"2026-05-17T19:06:20.053Z","avatar_url":"https://github.com/LiaPlayground.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003c!--\nauthor:   André Dietrich\n\nemail:    LiaScript@web.de\n\nversion:  0.0.2\n\nlanguage: en\n\nnarrator: US English Female\n\ncomment:  Demo of creating a custom loader for code-highlighting of code that has to be fetched...\n\n@load.java: @load(java,@0)\n\n@load\n\u003cscript style=\"display: block\" modify=\"false\" run-once=\"true\"\u003e\n    fetch(\"@1\")\n    .then((response) =\u003e {\n        if (response.ok) {\n            response.text()\n            .then((text) =\u003e {\n                send.lia(\"LIASCRIPT:\\n``` @0\\n\" + text + \"\\n```\")\n            })\n        } else {\n            send.lia(\"HTML: \u003cspan style='color: red'\u003eSomething went wrong, could not load \u003ca href='@1'\u003e@1\u003c/a\u003e\u003c/span\u003e\")\n        }\n    })\n    \"loading: @1\"\n\u003c/script\u003e\n@end\n\n\n@import: https://raw.githubusercontent.com/LiaScript/CodeRunner/master/README.md\n\n@loadAndRun\n\u003cscript style=\"display: block\" modify=\"false\" run-once=\"true\"\u003e\n    const url = \"@1\"\n\n    let execute = \"\"\n    \n    try {\n      const [file, ending] = url.match(/[^\\/]+$/)[0].split(\".\")\n\n      switch(ending) {\n        case \"java\": {\n          execute = \"@LIA.java(\" + file + \")\"\n          break\n        }\n        case \"c\": {\n          execute = \"@LIA.gcc\"\n          break\n        }\n        case \"cpp\": {\n          execute = \"@LIA.g++\"\n          break\n        }\n      }\n    } catch (e) {\n      console.warn(\"could not identify filename in\", url)\n    }\n\n    fetch(url)\n    .then((response) =\u003e {\n        if (response.ok) {\n            response.text()\n            .then((text) =\u003e {\n\n                send.lia(`LIASCRIPT:\n\\`\\`\\` @0\n${text}\n\\`\\`\\`\n${execute}\n`)\n            })\n        } else {\n            send.lia(\"HTML: \u003cspan style='color: red'\u003eSomething went wrong, could not load \u003ca href='@1'\u003e@1\u003c/a\u003e\u003c/span\u003e\")\n        }\n    })\n    \"loading: @1\"\n\u003c/script\u003e\n@end\n\n--\u003e\n\n# Embedding external code-snippets\n\nScripts are native building blocks of LiaScript and they can be used nearly everywhere.\nScripts do not necessarily have to be attached to another element, such as a quiz, survey, or code-snippet.\nThus, if there is a free script, the result of the execution defines its body.\nThe result send back to LiaScript is always interpreted as a string, but in this way it also possible to construct new Markdown/LiaScript elements or HTML and to tell the interpreter to display it properly.\n\nThus, if a script returns null or undefined it will not be shown to the user at all.\n\n## Creating a custom Macro for loading code\n\nThe following example shows how such a custom loader might look like.\nThere are two macros defined, whereby the first is only used as a shortcut for the second one.\n\nWe have to tell the loading script:\n\n* that it is ok to run only once, when the slide is loaded, so that it will not be updated on every visit\n* that it should not be modifyable, otherwise the user might double-click on the result and inspect and modify the script\n* the result has to be displayed as a block...\n\nThe script that gets injected uses the fetch-API to GET the code-file, which is displayed as a code-block.\n\nThe first result of the script is the string at the bottom, since the fetch-api works asynchronous.\nBy using the `send.lia` function, we have a direct connection from our javascript source code to the counterpart-body in LiaScript.\nWhat is send back is also only a string, but in the first case we construct a LiaScript/Markdown - codeblock and by starting the string with `LIASCRIPT:` we say that this tiny string should be interpreted and parsed as LiaScript, which could also be a quiz, a table, or an entire chapter.\n`HTML:` is just a shortcut for embedding HTML-code, which will be displayed otherwise with all tags.\nThese later sending to lia will overwrite the former body, which was only used as a placeholder for the user.\n\nThe first macro-call might not work in this case, since LiaScript will directly pass it to the fetch-api, that will fail, since it will try to fetch a relative URL on another server, where your project is not in the root.\nBy using the Markdown-link like version of the macro call, the macro is called within brackets and the URL in parenthesis is passed in as the last parameter.\nThis way, LiaScript will know, that this is a URL and it will check whether it is relative or absolute and do some corrections in the first case.\nAnother benefit is, that the meaning will be preserved, it is a link and another Markdown-viewer will present the content accordingly and also link to your resource.\n\n```` markdown\n\u003c!--\n@load.java: @load(java,@0)\n\n@load\n\u003cscript style=\"display: block\" modify=\"false\" run-once=\"true\"\u003e\n    fetch(\"@1\")\n    .then((response) =\u003e {\n        if (response.ok) {\n            response.text()\n            .then((text) =\u003e {\n                send.lia(\"LIASCRIPT:\\n``` @0\\n\" + text + \"\\n```\")\n            })\n        } else {\n            send.lia(\"HTML: \u003cspan style='color: red'\u003eSomething went wrong, could not load \u003ca href='@1'\u003e@1\u003c/a\u003e\u003c/span\u003e\")\n        }\n    })\n    \"loading: @1\"\n\u003c/script\u003e\n@end\n--\u003e\n\n# Loading\n\n@load.java(App.java) --\u003e might not work\n\n@[load.java](App.java)\n\n@[load(java)](App.java)\n\nwill return an error message\n\n@[load.java](DoesNotExist.java)\n````\n\n### Demo\n\n@load.java(App.java) --\u003e might not work\n\n---\n\n@[load.java](App.java)\n\n---\n\n@[load(java)](App.java)\n\n---\n\nwill return an error message\n\n@[load.java](DoesNotExist.java)\n\n\n## Load and Run\n\nThis is a more sophisticated example, which additionally load the CodeRunner, for more information check out the following link:\n\nhttps://github.com/liaScript/coderunner\n\n... and it creates a script, which generates an executable code-block ...\n\n```` markdown\n\u003c!--\n@import: https://raw.githubusercontent.com/LiaScript/CodeRunner/master/README.md\n\n@loadAndRun\n\u003cscript style=\"display: block\" modify=\"false\" run-once=\"true\"\u003e\n    const url = \"@1\"\n\n    let execute = \"\"\n    \n    try {\n      const [file, ending] = url.match(/[^\\/]+$/)[0].split(\".\")\n\n      switch(ending) {\n        case \"java\": {\n          execute = \"@LIA.java(\" + file + \")\"\n          break\n        }\n        case \"c\": {\n          execute = \"@LIA.gcc\"\n          break\n        }\n        case \"cpp\": {\n          execute = \"@LIA.g++\"\n          break\n        }\n      }\n    } catch (e) {\n      console.warn(\"could not identify filename in\", url)\n    }\n\n    fetch(url)\n    .then((response) =\u003e {\n        if (response.ok) {\n            response.text()\n            .then((text) =\u003e {\n\n                send.lia(`LIASCRIPT:\n\\`\\`\\` @0\n${text}\n\\`\\`\\`\n${execute}\n`)\n            })\n        } else {\n            send.lia(\"HTML: \u003cspan style='color: red'\u003eSomething went wrong, could not load \u003ca href='@1'\u003e@1\u003c/a\u003e\u003c/span\u003e\")\n        }\n    })\n    \"loading: @1\"\n\u003c/script\u003e\n@end\n\n--\u003e\n\n# Demo 2\n\n@[loadAndRun(java)](App.java)\n````\n\n### Demo 2\n\n@[loadAndRun(java)](App.java)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fliaplayground%2Fcustom-code-imports","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fliaplayground%2Fcustom-code-imports","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fliaplayground%2Fcustom-code-imports/lists"}