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

[Base64] Losing the line breaks #12183

Open
Decryptu opened this issue May 5, 2024 · 1 comment
Open

[Base64] Losing the line breaks #12183

Decryptu opened this issue May 5, 2024 · 1 comment
Labels
bug Something isn't working extension: base64 Issues related to the base64 extension extension Issues related to one of the extensions in the Store

Comments

@Decryptu
Copy link

Decryptu commented May 5, 2024

Extension

https://www.raycast.com/DanielSinclair/base64

Description

Hello, I'm not sure if the issue is 100% from this extension, but when I encode multiple lines of code with this extension, and decode it like that (react / typescript):

const base64EncodedCode ="string from extension here";
const decodedCode = atob(base64EncodedCode);

I will lose all the line breaks. But if I do the exact same thing, but instead of using the extension, I use base64decode.org tool to encode, it will work fine with their base64 string, and the line breaks will be here.


It's a bit weird because, the "decode" part of the extension will display the line breaks, but the way its encoded must be different from the other app, otherwise I would get the same result from the extension, and from the other website using atob.

I also had the issue of the special characters not being properly decoded, but I solved that using an utils :

// src/utils/base64.ts

/**
 * Decodes a Base64 encoded string to a regular string with proper UTF-8 support.
 * This method ensures that special characters and line breaks are handled correctly.
 * @param {string} base64String - The Base64 encoded string.
 * @returns {string} - The decoded string.
 */
export const decodeBase64 = (base64String: string): string => {
  return decodeURIComponent(
    atob(base64String)
      .split("")
      .map((char) => {
        return "%" + ("00" + char.charCodeAt(0).toString(16)).slice(-2);
      })
      .join("")
  );
};

/**
 * Encodes a string to a Base64 encoded string with proper UTF-8 support.
 * This method handles special characters and ensures the string is properly encoded.
 * @param {string} normalString - The regular string.
 * @returns {string} - The Base64 encoded string.
 */
export const encodeBase64 = (normalString: string): string => {
  return btoa(
    encodeURIComponent(normalString).replace(
      /%([0-9A-F]{2})/g,
      function (match, p1) {
        return String.fromCharCode(parseInt(p1, 16));
      }
    )
  );
};

Thank you :)

Steps To Reproduce

  1. Encode multiple lines of code
  2. Decode the string using JavaScript atob
  3. All line breaks are gone
  4. Use another tool to encode
  5. Decode the new string with JavaScript atob
  6. Line breaks are back

Current Behaviour

Removes all line breaks on decode

Expected Behaviour

Keeps the line breaks

@Decryptu Decryptu added bug Something isn't working extension Issues related to one of the extensions in the Store labels May 5, 2024
@raycastbot raycastbot added the extension: base64 Issues related to the base64 extension label May 5, 2024
@raycastbot
Copy link
Collaborator

raycastbot commented May 5, 2024

Thank you for opening this issue!

🔔 @DanielSinclair @sammarks you might want to have a look.

💡 Author and Contributors commands

The author and contributors of DanielSinclair/base64 can trigger bot actions by commenting:

  • @raycastbot close this issue Closes the issue.
  • @raycastbot rename this issue to "Awesome new title" Renames the issue.
  • @raycastbot reopen this issue Reopens the issue.
  • @raycastbot assign me Assigns yourself to the issue.
  • @raycastbot good first issue Adds the "Good first issue" label to the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working extension: base64 Issues related to the base64 extension extension Issues related to one of the extensions in the Store
Projects
None yet
Development

No branches or pull requests

2 participants