Skip to content

Commit 2ea2833

Browse files
committed
fix: Qt.btoa warning
Signed-off-by: Patrizio Bekerle <patrizio@bekerle.com>
1 parent a28b21f commit 2ea2833

4 files changed

Lines changed: 103 additions & 5 deletions

File tree

‎latex-math/info.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "Latex Math",
33
"identifier": "latex-math",
44
"script": "latex-math.qml",
5-
"version": "0.0.6",
5+
"version": "0.0.7",
66
"platforms": ["linux", "macos", "windows"],
77
"minAppVersion": "20.8.0",
88
"authors": ["@r00tr4v3n", "@Aganel"],

‎latex-math/latex-math.qml‎

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ QtObject {
121121
}
122122
function getBashCmd(path, latexBase64) {
123123
const exec = executable;
124-
const preamble = Qt.btoa(getPreamble());
124+
const preamble = toBase64(getPreamble());
125125
const quiet = " --quiet 1"; // --quiet OFF does not work (klatexformula bug?)
126126
const cmd = `"${exec}" -f "${formulaColor}" -b "${formulaBgColor}" --base64arg --preamble="${preamble}" --base64arg --latexinput="${latexBase64}" --dpi ${settingDPI} ${quiet} --output ${path}`;
127127
//log("cmd: "+cmd)
@@ -155,6 +155,55 @@ QtObject {
155155
}
156156
}
157157

158+
/**
159+
* Encodes a string as base64 of its UTF-8 representation
160+
*
161+
* This replaces the deprecated Qt.btoa(string) call, which emits a
162+
* deprecation warning in newer Qt versions
163+
*
164+
* @param {string} text - the text to encode
165+
* @return {string} the base64 encoded text
166+
*/
167+
function toBase64(text) {
168+
const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
169+
var bytes = [];
170+
171+
// encode the string as UTF-8
172+
for (var i = 0; i < text.length; i++) {
173+
var cp = text.codePointAt(i);
174+
175+
if (cp > 0xFFFF) {
176+
// skip the low surrogate of the surrogate pair
177+
i++;
178+
}
179+
180+
if (cp < 0x80) {
181+
bytes.push(cp);
182+
} else if (cp < 0x800) {
183+
bytes.push(0xC0 | (cp >> 6), 0x80 | (cp & 0x3F));
184+
} else if (cp < 0x10000) {
185+
bytes.push(0xE0 | (cp >> 12), 0x80 | ((cp >> 6) & 0x3F), 0x80 | (cp & 0x3F));
186+
} else {
187+
bytes.push(0xF0 | (cp >> 18), 0x80 | ((cp >> 12) & 0x3F), 0x80 | ((cp >> 6) & 0x3F), 0x80 | (cp & 0x3F));
188+
}
189+
}
190+
191+
var result = "";
192+
193+
for (var j = 0; j < bytes.length; j += 3) {
194+
const b0 = bytes[j];
195+
const b1 = j + 1 < bytes.length ? bytes[j + 1] : -1;
196+
const b2 = j + 2 < bytes.length ? bytes[j + 2] : -1;
197+
198+
result += alphabet[b0 >> 2];
199+
result += alphabet[((b0 & 0x03) << 4) | (b1 < 0 ? 0 : b1 >> 4)];
200+
result += b1 < 0 ? "=" : alphabet[((b1 & 0x0F) << 2) | (b2 < 0 ? 0 : b2 >> 6)];
201+
result += b2 < 0 ? "=" : alphabet[b2 & 0x3F];
202+
}
203+
204+
return result;
205+
}
206+
158207
/**
159208
* This function is called when the markdown html of a note is generated
160209
*
@@ -183,7 +232,7 @@ QtObject {
183232

184233
latex = formulaPrefix + latex; // add prefix from settings
185234
latex = latex.trim();
186-
const latexBase64 = Qt.btoa(latex);
235+
const latexBase64 = toBase64(latex);
187236
const filename = Qt.md5(latex);
188237
const path = workDir + "/" + filename + ".png";
189238

‎mermaid/info.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"script": "mermaid.qml",
55
"authors": ["@dohliam", "@cyclops1982"],
66
"platforms": ["linux", "macos", "windows"],
7-
"version": "0.0.4",
7+
"version": "0.0.5",
88
"minAppVersion": "22.1.1",
99
"description": "Support for rendering mermaid diagrams in mermaid code blocks as image. Uses the external API from <a href='https://mermaid.ink/'>mermaid.ink</a>."
1010
}

‎mermaid/mermaid.qml‎

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,55 @@ import QtQml 2.0
22
import QOwnNotesTypes 1.0
33

44
QtObject {
5+
/**
6+
* Encodes a string as base64 of its UTF-8 representation
7+
*
8+
* This replaces the deprecated Qt.btoa(string) call, which emits a
9+
* deprecation warning in newer Qt versions
10+
*
11+
* @param {string} text - the text to encode
12+
* @return {string} the base64 encoded text
13+
*/
14+
function toBase64(text) {
15+
const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
16+
var bytes = [];
17+
18+
// encode the string as UTF-8
19+
for (var i = 0; i < text.length; i++) {
20+
var cp = text.codePointAt(i);
21+
22+
if (cp > 0xFFFF) {
23+
// skip the low surrogate of the surrogate pair
24+
i++;
25+
}
26+
27+
if (cp < 0x80) {
28+
bytes.push(cp);
29+
} else if (cp < 0x800) {
30+
bytes.push(0xC0 | (cp >> 6), 0x80 | (cp & 0x3F));
31+
} else if (cp < 0x10000) {
32+
bytes.push(0xE0 | (cp >> 12), 0x80 | ((cp >> 6) & 0x3F), 0x80 | (cp & 0x3F));
33+
} else {
34+
bytes.push(0xF0 | (cp >> 18), 0x80 | ((cp >> 12) & 0x3F), 0x80 | ((cp >> 6) & 0x3F), 0x80 | (cp & 0x3F));
35+
}
36+
}
37+
38+
var result = "";
39+
40+
for (var j = 0; j < bytes.length; j += 3) {
41+
const b0 = bytes[j];
42+
const b1 = j + 1 < bytes.length ? bytes[j + 1] : -1;
43+
const b2 = j + 2 < bytes.length ? bytes[j + 2] : -1;
44+
45+
result += alphabet[b0 >> 2];
46+
result += alphabet[((b0 & 0x03) << 4) | (b1 < 0 ? 0 : b1 >> 4)];
47+
result += b1 < 0 ? "=" : alphabet[((b1 & 0x0F) << 2) | (b2 < 0 ? 0 : b2 >> 6)];
48+
result += b2 < 0 ? "=" : alphabet[b2 & 0x3F];
49+
}
50+
51+
return result;
52+
}
53+
554
/**
655
* This function is called before the markdown html of a note is generated
756
*
@@ -20,7 +69,7 @@ QtObject {
2069
function preNoteToMarkdownHtmlHook(note, markdown, forExport) {
2170
var re = /```mermaid\n([\s\S]*?)\n```/gim;
2271
markdown = markdown.replace(re, function (_, diag) {
23-
var encodedData = Qt.btoa(diag);
72+
var encodedData = toBase64(diag);
2473
var ink = '![](https://mermaid.ink/img/' + encodedData + ')';
2574
return ink;
2675
});

0 commit comments

Comments
 (0)