fix string escaping

This commit is contained in:
end-4
2025-05-07 23:48:24 +02:00
parent 47a8149968
commit 0a331061c3
3 changed files with 21 additions and 14 deletions
@@ -10,9 +10,9 @@ function getDomain(url) {
}
function shellSingleQuoteEscape(str) {
// First escape backslashes, then escape single quotes
// escape single quotes
return String(str)
.replace(/\\/g, '\\\\')
// .replace(/\\/g, '\\\\')
.replace(/'/g, "'\\''");
}
@@ -33,7 +33,3 @@ function splitMarkdownBlocks(markdown) {
}
return result;
}
function unEscapeBackslashes(str) {
return str.replace(/\\\\/g, '\\');
}
@@ -27,6 +27,7 @@ Rectangle {
property real codeBlockHeaderPadding: 3
property real codeBlockComponentSpacing: 2
property bool enableMouseSelection: false
property bool renderMarkdown: true
property bool editing: false
@@ -250,6 +251,7 @@ Rectangle {
TextArea {
Layout.fillWidth: true
readOnly: !root.editing
selectByMouse: root.enableMouseSelection || root.editing
renderType: Text.NativeRendering
font.family: Appearance.font.family.reading
font.hintingPreference: Font.PreferNoHinting // Prevent weird bold text
@@ -281,7 +283,8 @@ Rectangle {
anchors.fill: parent
acceptedButtons: Qt.NoButton // Only for hover
hoverEnabled: true
cursorShape: parent.hoveredLink !== "" ? Qt.PointingHandCursor : Qt.IBeamCursor
cursorShape: parent.hoveredLink !== "" ? Qt.PointingHandCursor :
(root.enableMouseSelection || root.editing) ? Qt.IBeamCursor : Qt.ArrowCursor
}
}
}
@@ -330,9 +333,7 @@ Rectangle {
id: copyCodeButton
buttonIcon: "content_copy"
onClicked: {
Hyprland.dispatch(`exec wl-copy '${StringUtils.unEscapeBackslashes(
StringUtils.shellSingleQuoteEscape(segment.content)
)}'`)
Hyprland.dispatch(`exec wl-copy '${StringUtils.shellSingleQuoteEscape(segment.content)}'`)
}
StyledToolTip {
content: qsTr("Copy code")
@@ -422,10 +423,10 @@ Rectangle {
}
TextArea { // Code
id: codeTextArea
Layout.fillWidth: true
readOnly: !root.editing
selectByMouse: root.enableMouseSelection || root.editing
renderType: Text.NativeRendering
font.family: Appearance.font.family.monospace
font.hintingPreference: Font.PreferNoHinting // Prevent weird bold text
@@ -463,6 +464,16 @@ Rectangle {
}
}
}
// MouseArea to block scrolling
MouseArea {
id: codeBlockMouseArea
anchors.fill: parent
acceptedButtons: root.editing ? Qt.NoButton : Qt.LeftButton
onWheel: (event) => {
event.accepted = false
}
}
}
}
}
+3 -3
View File
@@ -249,7 +249,7 @@ Singleton {
+ ` ${headerString}`
+ ' -H "Authorization: Bearer ${API_KEY}"'
+ ` -d '${StringUtils.shellSingleQuoteEscape(JSON.stringify(data))}'`
// console.log("Request command: ", requestCommandString);
console.log("Request command: ", requestCommandString);
requester.command = baseCommand.concat([requestCommandString]);
/* Reset vars and make the request */
@@ -302,7 +302,7 @@ Singleton {
if (dataJson.done) requester.message.done = true;
} catch (e) {
console.log("Could not parse response: ", e);
console.log("[AI] Could not parse response from stream: ", e);
requester.message.content += cleanData;
}
}
@@ -314,7 +314,7 @@ Singleton {
const parsedResponse = JSON.parse(requester.message.content + "]");
requester.message.content = `\`\`\`json\n${JSON.stringify(parsedResponse, null, 2)}\n\`\`\``;
} catch (e) {
console.log("Could not parse response: ", e);
console.log("[AI] Could not parse response on exit: ", e);
}
}
}