Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5/?] Add ability to run code examples in the playground: Run code samples inline #554

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/linters/.markdown-lint.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"MD007": { "indent": 4 },
"MD013": false,
"MD026": false
"MD026": false,
"MD042": false
}
1 change: 1 addition & 0 deletions .spelling-wordlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ FFI
filesystem
finaliser
finalisers
fontawesome
forgeable
GC
GDB
Expand Down
32 changes: 32 additions & 0 deletions docs/assets/snippets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* @param {MouseEvent} evt
*/
async function runSnippetInline(evt) {
evt.preventDefault();
const snippetRes = await fetch(`https://raw.githubusercontent.com/ponylang/pony-tutorial/main/code-samples/${evt.target.dataset.snippet}`)
const snippetCode = await snippetRes.text()
const evaluateRes = await fetch('https://playground.ponylang.io/evaluate.json', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
"code": snippetCode,
"separate_output": true,
"color": true,
"branch": "release"
})
})
const json = await evaluateRes.json()
evt.target.previousElementSibling.querySelector('pre').append(Object.assign(document.create('pre'), {
innerHTML: json.success ? json.stdout : json.compiler,
}))
}

document$.subscribe(function() {
const inlineRunButtons = document.querySelectorAll('.md-button[data-snippet]')

for (const button of inlineRunButtons) {
button.addEventListener('click', runSnippetInline)
}
})
2 changes: 2 additions & 0 deletions docs/getting-started/hello-world.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ In your file, put the following code:
--8<-- "hello-world-main.pony"
```

[:fontawesome-solid-play: Run inline](#){ .md-button .md-button--primary data-snippet="hello-world-main.pony" }

## Compiling the program

Now compile it:
Expand Down
7 changes: 7 additions & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ repo_url: https://github.com/ponylang/pony-tutorial/
site_url: https://tutorial.ponylang.io/
use_directory_urls: false

extra_javascript:
- assets/snippets.js

extra:
generator: false
social:
Expand All @@ -25,6 +28,10 @@ markdown_extensions:
- pymdownx.snippets:
base_path: ['code-samples']
check_paths: true
- pymdownx.emoji:
emoji_index: !!python/name:material.extensions.emoji.twemoji
emoji_generator: !!python/name:material.extensions.emoji.to_svg
- attr_list
- smarty
- toc:
permalink: true
Expand Down
Loading