Repository navigation
feat(docs): share playground links that carry the Markdown in the URL #526
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
Open
onmax
wants to merge
7
commits into
comarkdown:main
Choose a base branch
from
onmax:feat/playground-share-url
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
dc3a5eb
feat(docs): share playground links that carry the Markdown in the URL
onmax 8ee5f1e
feat(docs): confirm a copied link on the Share button instead of a toast
onmax 821d675
fix(docs): keep share hashes in router history and report unreadable …
onmax 3e14e81
fix(docs): let only the latest navigation restore a shared link
onmax 6f17073
fix(docs): keep text typed while a shared link decodes
onmax 4b85b4d
fix(docs): size the Share icon to the label's cap height
onmax 90dabb3
fix(docs): copy the share link in Safari
onmax File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,23 @@ | ||
| // The playground keeps its Markdown in the URL hash, like the TypeScript playground, so a link carries the input and | ||
| // nothing is stored on a server. The text is deflated and base64url-encoded to keep links short. | ||
| const PREFIX = '#code/' | ||
|
|
||
| async function bytes(stream: ReadableStream<Uint8Array>): Promise<Uint8Array> { | ||
| return new Uint8Array(await new Response(stream).arrayBuffer()) | ||
| } | ||
|
|
||
| export async function encodeShareHash(markdown: string): Promise<string> { | ||
| const deflated = await bytes(new Blob([markdown]).stream().pipeThrough(new CompressionStream('deflate-raw'))) | ||
| let binary = '' | ||
| for (const byte of deflated) binary += String.fromCharCode(byte) | ||
| return PREFIX + btoa(binary).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '') | ||
| } | ||
|
|
||
| /** The Markdown in a share hash, or `undefined` for a hash that isn't one. Throws when the hash is cut off or mangled. */ | ||
| export async function decodeShareHash(hash: string): Promise<string | undefined> { | ||
| if (!hash.startsWith(PREFIX)) return undefined | ||
| const binary = atob(hash.slice(PREFIX.length).replace(/-/g, '+').replace(/_/g, '/')) | ||
| const deflated = Uint8Array.from(binary, (char) => char.charCodeAt(0)) | ||
| const inflated = await bytes(new Blob([deflated]).stream().pipeThrough(new DecompressionStream('deflate-raw'))) | ||
| return new TextDecoder().decode(inflated) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.