-
Notifications
You must be signed in to change notification settings - Fork 239
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(playground): add svelte 5 playground (#270)
* feat(playground): add svelte 5 playground * fix(playground): exclude svelte 5 examples that includes html files since it does not work * fix: use "/untitled" on Svelte 5 playground base url Co-authored-by: brunnerh <[email protected]> * feat(playground): include snippet title key in json hashed --------- Co-authored-by: brunnerh <[email protected]>
- Loading branch information
Showing
4 changed files
with
70 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,4 +28,6 @@ dist-ssr | |
|
||
src/generatedContent | ||
|
||
archive | ||
archive | ||
|
||
vite.config.js.timestamp* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import path from "node:path"; | ||
|
||
export default function createSvelte5Playground() { | ||
const BASE_URL = "https://svelte.dev/playground/untitled?version=5#"; | ||
|
||
async function fromContentByFilename(contentByFilename, title) { | ||
const filenames = Object.keys(contentByFilename); | ||
if (filenames.some((f) => f.includes(".html"))) { | ||
return; | ||
} | ||
|
||
const files = filenames.map((filename, index) => { | ||
const contents = contentByFilename[filename]; | ||
const name = index === 0 ? "App.svelte" : path.parse(filename).base; | ||
return { | ||
type: "file", | ||
name, | ||
basename: name, | ||
contents, | ||
text: true, | ||
}; | ||
}); | ||
|
||
const payload = { title, files }; | ||
|
||
const hash = await compress_and_encode_text(JSON.stringify(payload)); | ||
|
||
const url = `${BASE_URL}${hash}`; | ||
|
||
return url; | ||
} | ||
|
||
return { | ||
fromContentByFilename, | ||
}; | ||
} | ||
|
||
// method `compress_and_encode_text` from https://github.com/sveltejs/svelte.dev/blob/main/apps/svelte.dev/src/routes/(authed)/playground/%5Bid%5D/gzip.js | ||
export async function compress_and_encode_text(input) { | ||
const reader = new Blob([input]) | ||
.stream() | ||
.pipeThrough(new CompressionStream("gzip")) | ||
.getReader(); | ||
let buffer = ""; | ||
for (;;) { | ||
const { done, value } = await reader.read(); | ||
if (done) { | ||
reader.releaseLock(); | ||
return btoa(buffer).replaceAll("+", "-").replaceAll("/", "_"); | ||
} else { | ||
for (let i = 0; i < value.length; i++) { | ||
// decoding as utf-8 will make btoa reject the string | ||
buffer += String.fromCharCode(value[i]); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters