Custom Code Imports for LiaScript: Load External Files Into Executable Blocks

Custom Code Imports for LiaScript: Load External Files Into Executable Blocks

LiaScript’s code blocks are normally written directly in the Markdown source. The Custom-code-imports playground by André Dietrich demonstrates a powerful pattern: fetching the content of an external source file by URL and injecting it as a live, executable LiaScript code block at runtime. This is particularly useful for courses that should always show the latest version of a codebase, or for exercises that load student submissions for review.


Quick Start

<!--
import: https://raw.githubusercontent.com/LiaPlayground/Custom-code-imports/main/README.md
-->

Concepts

The LIASCRIPT: prefix

Any <script> block in LiaScript can return a string that starts with LIASCRIPT: to inject raw LiaScript Markdown into the rendered output:

<script>
  "LIASCRIPT:\n```java\npublic class Hello { }\n```"
</script>

This allows scripts to dynamically produce code blocks, quizzes, or any other LiaScript content.

The HTML: prefix

Similarly, returning HTML: + HTML string renders the HTML directly:

<script>
  "HTML:<b>Hello from a script!</b>"
</script>

See both prefixes in action — content generated at render time by a script:


@load.java — Display an external Java file

@load.java(HelloWorld.java)

Fetches HelloWorld.java from the same GitHub directory as the README and renders it as a read-only java code block.

Or load from an explicit URL:

@[load.java](https://raw.githubusercontent.com/LiaPlayground/Custom-code-imports/main/HelloWorld.java)

Try it live — the Java file is fetched from GitHub and displayed as a formatted code block:


@loadAndRun(java) — Load AND execute

Fetches the file and renders it as an executable code block connected to CodeRunner:

@[loadAndRun(java)](https://raw.githubusercontent.com/LiaPlayground/Custom-code-imports/main/HelloWorld.java)

The student sees the code, can edit it, and can click Run — all from a file fetched at render time.


How it works

The macro uses a <script> block with run-once="true" and modify="false" to fetch the URL and inject the result as LiaScript markup:

<script run-once="true" modify="false">
  fetch("@1")
    .then(r => r.text())
    .then(text => {
      send.lia("LIASCRIPT:\n``` java\n" + text + "\n```")
    })
    .catch(e => {
      send.lia("HTML:<b>Could not load file: @1</b>")
    })
  "loading..."
</script>

Example: Multi-language code loader

<!--
import: https://raw.githubusercontent.com/LiaPlayground/Custom-code-imports/main/README.md
-->

# Dynamic Code Exercises

## Load from GitHub

The following Java file is fetched from GitHub at render time:

@[load.java](https://raw.githubusercontent.com/LiaPlayground/Custom-code-imports/main/HelloWorld.java)

---

## Load and run from GitHub

This version is editable and executable:

@[loadAndRun(java)](https://raw.githubusercontent.com/LiaPlayground/Custom-code-imports/main/HelloWorld.java)

---

## Build your own loader

Here is the pattern for any file type:

``` markdown
<script run-once="true" modify="false">
  fetch("https://example.com/MyCode.py")
    .then(r => r.text())
    .then(text => {
      send.lia("LIASCRIPT:\n\`\`\` python\n" + text + "\n\`\`\`")
    })
  "loading..."
</script>

---

## Advanced: Inject a quiz from a URL

The same pattern works for any LiaScript content, not just code blocks:

```html
<script run-once="true" modify="false">
  fetch("https://example.com/quiz.md")
    .then(r => r.text())
    .then(text => send.lia("LIASCRIPT:\n" + text))
  "loading..."
</script>

This allows modular course assembly: a teacher updates quiz.md on GitHub, and all courses that reference it automatically get the updated quiz next time students open the page.


Full Template Demo


Use Cases

Versioned course content — Reference source files on GitHub; the course always shows the latest version without re-publishing.

Reusable exercise banks — A shared repository of exercise files: each course imports what it needs by URL.

Code review exercises — Load a student’s submission URL and annotate or run it directly in the course.

Polyglot courses — A single course can load Java, Python, and C++ examples from different repositories without embedding all code inline.


Technical Facts

Runs in browserYes — fetch API
Server requiredNo
Key macros@load.java, @[load.java](URL), @[loadAndRun(java)](URL)
Core mechanismsend.lia("LIASCRIPT:\n...") from script block
Script attributesrun-once="true", modify="false"
SupportsAny language / any LiaScript content type
AuthorLiaPlayground / André Dietrich
LicenseMIT

Try It

Try in LiaScript Open in LiveEditor View on GitHub
  • AVR8js-mem — load and run AVR/Arduino code with memory visualization
  • SCORM-Progress — inject dynamic score tracking into a course
  • H5P-Test — embed external H5P content in a LiaScript course
  • Fonts — load external fonts into a LiaScript course

Related Posts

Tau-Prolog for LiaScript: Interactive Logic Programming in the Browser

Use the Tau-Prolog template to run Prolog programs and queries interactively in your LiaScript courses — full logic programming in the browser, with quiz integration.

Read More

A-Frame for LiaScript: 3D and VR Scenes in Your Course

Embed interactive 3D scenes and VR environments in LiaScript using the A-Frame template — write HTML A-Frame markup in a code block and render a fully interactive WebGL scene on any page.

Read More

Fonts for LiaScript: Custom Typography in Online Courses

Embed custom web fonts in LiaScript courses using CSS @font-face, Google Fonts link directives, or inline style blocks — the Fonts template from LiaPlayground demonstrates all three approaches.

Read More