code block syntax highlighting

This commit is contained in:
end-4
2025-05-07 12:16:20 +02:00
parent f3e0f14c44
commit 8464f0107c
6 changed files with 262 additions and 52 deletions
@@ -11,6 +11,7 @@ Singleton {
property QtObject rounding
property QtObject font
property QtObject sizes
property string syntaxHighlightingTheme
function mix(color1, color2, percentage) {
var c1 = Qt.color(color1);
@@ -238,4 +239,5 @@ Singleton {
property int fabHoveredShadowRadius: 7
}
syntaxHighlightingTheme: Appearance.m3colors.darkmode ? "Monokai" : "ayu Light"
}
@@ -1,17 +1,35 @@
function format(str, ...args) {
return str.replace(/{(\d+)}/g, (match, index) =>
typeof args[index] !== 'undefined' ? args[index] : match
);
return str.replace(/{(\d+)}/g, (match, index) =>
typeof args[index] !== 'undefined' ? args[index] : match
);
}
function getDomain(url) {
const match = url.match(/^(?:https?:\/\/)?(?:www\.)?([^\/]+)/);
return match ? match[1] : null;
const match = url.match(/^(?:https?:\/\/)?(?:www\.)?([^\/]+)/);
return match ? match[1] : null;
}
function shellSingleQuoteEscape(str) {
// First escape backslashes, then escape single quotes
return String(str)
.replace(/\\/g, '\\\\')
.replace(/'/g, "'\\''");
// First escape backslashes, then escape single quotes
return String(str)
.replace(/\\/g, '\\\\')
.replace(/'/g, "'\\''");
}
function splitMarkdownBlocks(markdown) {
const regex = /```(\w+)?\n([\s\S]*?)```/g;
let result = [];
let lastIndex = 0;
let match;
while ((match = regex.exec(markdown)) !== null) {
if (match.index > lastIndex) {
result.push({ type: "text", content: markdown.slice(lastIndex, match.index) });
}
result.push({ type: "code", lang: match[1] || "", content: match[2] });
lastIndex = regex.lastIndex;
}
if (lastIndex < markdown.length) {
result.push({ type: "text", content: markdown.slice(lastIndex) });
}
return result;
}