Skip to content

Commit 9c89f14

Browse files
committed
add "run on Lean4 Playground" button to code blocks
1 parent 4ffaf96 commit 9c89f14

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

assets/blockPlay.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Array.from(document.querySelectorAll(".language-lean")).forEach(function (code_block) {
2+
let pre_block = code_block.parentElement;
3+
4+
// create a link to lean4 web editor
5+
let escaped_code = encodeURIComponent(code_block.textContent);
6+
let url = `https://live.lean-lang.org/#code=${escaped_code}`;
7+
8+
// create a button
9+
let buttons = pre_block.querySelector(".buttons");
10+
let leanWebButton = document.createElement('button');
11+
leanWebButton.className = 'fa fa-external-link lean-web-button';
12+
leanWebButton.hidden = true;
13+
leanWebButton.title = 'Run on lean4 playground';
14+
leanWebButton.setAttribute('aria-label', leanWebButton.title);
15+
16+
// insert the button
17+
buttons.insertBefore(leanWebButton, buttons.firstChild);
18+
19+
// open the link when the button is clicked
20+
leanWebButton.addEventListener('click', function (e) {
21+
open(url);
22+
});
23+
});

book.toml

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,7 @@ src = "md"
66
title = "Metaprogramming in Lean 4"
77

88
[output.html]
9-
git-repository-url = "https://github.com/leanprover-community/lean4-metaprogramming-book"
9+
git-repository-url = "https://github.com/leanprover-community/lean4-metaprogramming-book"
10+
additional-js = [
11+
"assets/blockPlay.js", # Add "Run on Lean4 Playground" button to each code block
12+
]

0 commit comments

Comments
 (0)