Merge pull request #2165 from zsviczian/2.7.1-beta-1

fixed unescape, decodeURIComponent issue with non-latin characters.
This commit is contained in:
zsviczian
2024-12-18 17:58:45 +01:00
committed by GitHub
5 changed files with 26 additions and 11 deletions

View File

@@ -15,9 +15,15 @@ let html: any;
let preamble: string;
function svgToBase64(svg: string): string {
return `data:image/svg+xml;base64,${btoa(
decodeURIComponent(encodeURIComponent(svg.replaceAll(" ", " "))),
)}`;
const cleanSvg = svg.replaceAll(" ", " ");
// Convert the string to UTF-8 and handle non-Latin1 characters
const encodedData = encodeURIComponent(cleanSvg)
.replace(/%([0-9A-F]{2})/g,
(match, p1) => String.fromCharCode(parseInt(p1, 16))
);
return `data:image/svg+xml;base64,${btoa(encodedData)}`;
}
async function getImageSize(src: string): Promise<{ height: number; width: number }> {

View File

@@ -1925,7 +1925,7 @@ export default class ExcalidrawView extends TextFileView implements HoverParent{
//onClose happens after onunload
protected async onClose(): Promise<void> {
(process.env.NODE_ENV === 'development') && DEBUGGING && debug(this.onClose,`ExcalidrawView.onClose, file:${this.file?.name}`);
this.exitFullscreen();
await this.forceSaveIfRequired();
if (this.excalidrawRoot) {
this.excalidrawRoot.unmount();

View File

@@ -7,6 +7,7 @@ import { getEA } from "src";
import { ExcalidrawAutomate, cloneElement } from "src/ExcalidrawAutomate";
import { ExportSettings } from "src/ExcalidrawView";
import { nanoid } from "src/constants/constants";
import { svgToBase64 } from "./Utils";
export class CropImage {
private imageEA: ExcalidrawAutomate;
@@ -170,7 +171,7 @@ export class CropImage {
1 // image quality (0 - 1)
);
};
image.src = `data:image/svg+xml;base64,${btoa(unescape(encodeURIComponent(svgData)))}`;
image.src = svgToBase64(svgData);
});
}

View File

@@ -350,7 +350,9 @@ export const getInternalLinkOrFileURLLink = (
const vault = plugin.app.vault;
const fileURLString = vault.adapter.url.pathToFileURL(path).toString();
if (fileURLString.startsWith(VAULT_BASE_URL())) {
const internalPath = normalizePath(unescape(fileURLString.substring(VAULT_BASE_URL().length)));
const internalPath = normalizePath(
decodeURIComponent(fileURLString.substring(VAULT_BASE_URL().length))
);
const file = vault.getAbstractFileByPath(internalPath);
if(file && file instanceof TFile) {
const link = plugin.app.metadataCache.fileToLinktext(

View File

@@ -230,11 +230,17 @@ export function base64StringToBlob (base64String: string, mimeType: string): Blo
return new Blob([buffer], { type: mimeType });
};
export function svgToBase64 (svg: string): string {
return `data:image/svg+xml;base64,${btoa(
unescape(encodeURIComponent(svg.replaceAll("&nbsp;", " "))),
)}`;
};
export function svgToBase64(svg: string): string {
const cleanSvg = svg.replaceAll("&nbsp;", " ");
// Convert the string to UTF-8 and handle non-Latin1 characters
const encodedData = encodeURIComponent(cleanSvg)
.replace(/%([0-9A-F]{2})/g,
(match, p1) => String.fromCharCode(parseInt(p1, 16))
);
return `data:image/svg+xml;base64,${btoa(encodedData)}`;
}
export async function getBinaryFileFromDataURL (dataURL: string): Promise<ArrayBuffer> {
if (!dataURL) {