feat(marko): udpate examples for v6 (#299)

This commit is contained in:
Luke LaValva
2025-09-13 04:50:14 -07:00
committed by GitHub
parent a45e56b340
commit 886fdcd1d3
25 changed files with 101 additions and 70 deletions

View File

@@ -92,16 +92,17 @@ export default {
return generateURLFromData(data);
},
marko: (contentByFilename) => {
const BASE_URL = "https://markojs.com/playground/#";
marko: async (contentByFilename) => {
let firstFile = true;
const data = Object.entries(contentByFilename).map(([path, content]) => ({
name: nodePath.parse(path).base,
path: `/components/${path}`,
path: firstFile ? (firstFile = false) || "index.marko" : path,
content,
}));
return BASE_URL + compressToURL(JSON.stringify(data));
return (
"https://markojs.com/playground#" +
(await markoCompress(JSON.stringify(data)))
);
},
};
@@ -158,3 +159,30 @@ async function compress_and_encode_text(input) {
}
}
}
// method `compress` from https://github.com/marko-js/website/blob/main/src/util/hasher.ts#L8-L25
export async function markoCompress(value) {
const stream = new CompressionStream("gzip");
const writer = stream.writable.getWriter();
writer.write(new TextEncoder().encode(value));
writer.close();
let result = "v2";
const reader = stream.readable.getReader();
try {
while (true) {
const { value, done } = await reader.read();
if (done) break;
for (const byte of value) {
result += String.fromCharCode(byte);
}
}
return btoa(result)
.replace(/=+$/, "")
.replace(/\+/g, "-")
.replace(/\//g, "_");
} finally {
reader.releaseLock();
}
}

View File

@@ -1,2 +1,2 @@
<let/name = "John"/>
<let/name="John">
<h1>Hello ${name}</h1>

View File

@@ -1,3 +1,3 @@
<let/name = "John"/>
<effect() { name = "Jane" }/>
<let/name="John">
<script>name = "Jane"</script>
<h1>Hello ${name}</h1>

View File

@@ -1,3 +1,3 @@
<let/count = 10/>
<const/doubleCount = count * 2/>
<let/count=10>
<const/doubleCount=count * 2>
<div>${doubleCount}</div>

View File

@@ -1,5 +1,5 @@
<h1.title>I am red</h1>
<button style={ fontSize: "10rem" }>I am a button</button>
<button style={ "font-size": "10rem" }>I am a button</button>
<button class=scopedButton>I am a style-scoped button</button>
<style>

View File

@@ -1,3 +1,3 @@
<let/count = 0/>
<let/count=0>
<p>Counter: ${count}</p>
<button onClick() { count++ }>+1</button>

View File

@@ -1,2 +1,4 @@
<input/inputElement>
<effect() { inputElement().focus() }/>
<script>
inputElement().focus();
</script>

View File

@@ -1,6 +1,6 @@
static const TRAFFIC_LIGHTS = ["red", "orange", "green"];
<let/lightIndex = 0/>
<const/light = TRAFFIC_LIGHTS[lightIndex]/>
<let/lightIndex=0>
<const/light=TRAFFIC_LIGHTS[lightIndex]>
<button onClick() { lightIndex = (lightIndex + 1) % TRAFFIC_LIGHTS.length }>
Next light
@@ -9,6 +9,6 @@ static const TRAFFIC_LIGHTS = ["red", "orange", "green"];
<p>
You must
<if=light === "red">STOP</if>
<else-if=light === "orange">SLOW DOWN</else-if>
<else if=light === "orange">SLOW DOWN</else>
<else>GO</else>
</p>

View File

@@ -1,3 +1,3 @@
<let/pageTitle = ""/>
<effect() { pageTitle = document.title }/>
<let/pageTitle="">
<script>pageTitle = document.title</script>
<p>Page title: ${pageTitle}</p>

View File

@@ -1,6 +1,6 @@
<let/time = new Date()/>
<lifecycle
onMount() { this.timer = setInterval(_ => time = new Date(), 1000) }
onDestroy() { clearInterval(this.timer) }
/>
<let/time=new Date()>
<script>
const id = setInterval(() => time = new Date(), 1000);
$signal.onabort = () => clearInterval(id)
</script>
<p>Current time: ${time.toLocaleTimeString()}</p>

View File

@@ -3,7 +3,7 @@
age = null,
favouriteColors = [],
isAvailable = false,
} = input/>
}=input>
<p>My name is ${name}!</p>
<p>My age is ${age}!</p>

View File

@@ -1,7 +1,7 @@
<let/isHappy = true/>
<let/isHappy=true>
<p>Are you happy?</p>
<AnswerButton
onYes() { isHappy = true }
onNo() { isHappy = false }
/>
<p style={ fontSize: 50 }>${isHappy ? "😀" : "😥"}</p>
<p style={ "font-size": 50 }>${isHappy ? "😀" : "😥"}</p>

View File

@@ -1,8 +1,8 @@
<button.${funnyButton}>
<${input.renderBody}/>
<button class=funnyButton>
<${input.content}/>
</button>
<style.module.css/{ funnyButton }>
<style/{ funnyButton }>
.funnyButton {
background: rgba(0, 0, 0, 0.4);
color: #fff;

View File

@@ -1,10 +1,10 @@
<button.${funnyButton}>
<${input.renderBody}>
<button class=funnyButton>
<${input.content}>
<span>No content found</span>
</>
</button>
<style.module.css/{ funnyButton }>
<style/{ funnyButton }>
.funnyButton {
background: rgba(0, 0, 0, 0.4);
color: #fff;

View File

@@ -1,13 +1,13 @@
<let/user = {
<let/user={
id: 1,
username: "unicorn42",
email: "unicorn42@example.com",
}/>
}>
<const/updateUsername(newUsername) {
user = { ...user, username: newUsername };
}/>
}>
<h1>Welcome back, ${user.username}</h1>
<set={ ...user, updateUsername }>
<context={ ...user, updateUsername }>
<UserProfile />
</set>
</context>

View File

@@ -1,4 +1,4 @@
<get/{ username, email, updateUsername } = "App"/>
<context/{ username, email, updateUsername } from="App"/>
<div>
<h2>My Profile</h2>
<p>Username: ${username}</p>

View File

@@ -1,3 +1,3 @@
<let/text = "Hello world"/>
<let/text="Hello world">
<p>${text}</p>
<input value:=text/>
<input value:=text>

View File

@@ -1,5 +1,5 @@
<input#is-available
type="checkbox"
checked:=input.isAvailable
/>
>
<label for="is-available">Is available</label>

View File

@@ -1,21 +1,16 @@
<let/picked = "red"/>
<const/handleChange(event) {
picked = event.target.value;
}/>
<let/picked="red">
<div>Picked: ${picked}</div>
<input#blue-pill
type="radio"
checked=picked === "blue"
value="blue"
onChange=handleChange
/>
checkedValue:=picked
>
<label for="blue-pill">Blue pill</label>
<input#red-pill
type="radio"
checked=picked === "red"
value="red"
onChange=handleChange
/>
checkedValue:=picked
>
<label for="red-pill">Red pill</label>

View File

@@ -4,11 +4,11 @@ static const colors = [
{ id: 3, text: "green" },
{ id: 4, text: "gray", isDisabled: true },
];
<let/selectedColorId = 2/>
<let/selectedColorId=2>
<select onChange(event) { selectedColorId = event.target.value }>
<select value:=selectedColorId>
<for|{ id, isDisabled, text }| of=colors>
<option value=id disabled=isDisabled selected=id === selectedColorId>
<option value=id disabled=isDisabled>
${text}
</option>
</for>

View File

@@ -0,0 +1,3 @@
import App from "./App.marko";
App.mount({}, document.getElementById("app"));

View File

@@ -0,0 +1,7 @@
<!doctype html>
<html>
<body>
<div id="app"></div>
<script type="module" src="./app.js"></script>
</body>
</html>

View File

@@ -1,4 +0,0 @@
<!DOCTYPE html>
<html>
<App/>
</html>

View File

@@ -1,11 +1,5 @@
<await(fetch("https://randomuser.me/api/?results=3").then(res => res.json()))>
<@placeholder>
<p>Fetching users...</p>
</@placeholder>
<@catch|error|>
<p>An error occurred while fetching users</p>
</@catch>
<@then|{ results: users }|>
<try>
<await|{ results: users }|=fetch("https://randomuser.me/api/?results=3").then(res => res.json())>
<ul>
<for|{ picture, name }| of=users>
<li>
@@ -14,5 +8,11 @@
</li>
</for>
</ul>
</@then>
</await>
</await>
<@placeholder>
<p>Fetching users...</p>
</@placeholder>
<@catch|error|>
<p>An error occurred while fetching users</p>
</@catch>
</try>

View File

@@ -436,7 +436,7 @@ const frameworks = [
playgroundURL: "https://markojs.com/playground/",
documentationURL: "https://markojs.com/docs/getting-started/",
filesSorter(files) {
return sortAllFilenames(files, ["index.marko", "App.marko"]);
return sortAllFilenames(files, ["index.html", "App.marko"]);
},
repositoryLink: "https://github.com/marko-js/marko",
mainPackageName: "marko",