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

Encode and decode copy-to-clipboard functionality using base64 #372

Merged
merged 1 commit into from
May 15, 2023
Merged
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
7 changes: 4 additions & 3 deletions kit/preprocess.js
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ const _mdsvexPreprocess = mdsvex({
lang && hljs.getLanguage(lang)
? hljs.highlight(lang, code, true).value
: hljs.highlightAuto(code).value;
const base64 = (val) => btoa(encodeURIComponent(val));
const escape = (code) =>
code.replace(/\\/g, "\\\\").replace(/`/g, "\\`").replace(/}/g, "\\}").replace(/\$/g, "\\$");
const REGEX_FRAMEWORKS_SPLIT = /\s*===(PT-TF|STRINGAPI-READINSTRUCTION)-SPLIT===\s*/gm;
Expand Down Expand Up @@ -462,12 +463,12 @@ const _mdsvexPreprocess = mdsvex({
<CodeBlockFw
group1={{
id: '${isPtTf ? "pt" : "stringapi"}',
code: \`${escape(codeGroup1)}\`,
code: \`${base64(codeGroup1)}\`,
highlighted: \`${escape(highlightedPt)}\`
}}
group2={{
id: '${isPtTf ? "tf" : "readinstruction"}',
code: \`${escape(codeGroup2)}\`,
code: \`${base64(codeGroup2)}\`,
highlighted: \`${escape(highlightedTf)}\`
}}
/>`;
Expand All @@ -484,7 +485,7 @@ const _mdsvexPreprocess = mdsvex({
}
return `
<CodeBlock
code={\`${escape(code)}\`}
code={\`${base64(code)}\`}
highlighted={\`${escape(highlighted)}\`}
/>`;
}
Expand Down
2 changes: 1 addition & 1 deletion kit/src/lib/CopyButton.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
});

function handleClick() {
copyToClipboard(value);
copyToClipboard(decodeURIComponent(atob(value)));
isSuccess = true;
if (timeout) {
clearTimeout(timeout);
Expand Down