forked from Shinonome/dots-hyprland
ai: turn inline latex into latex code blocks for proper rendering
it looks poopy sometimes but still better than copilot failing all the time
This commit is contained in:
@@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
const monospaceFonts = 'JetBrains Mono NF, JetBrains Mono Nerd Font, JetBrains Mono NL, SpaceMono NF, SpaceMono Nerd Font, monospace';
|
const monospaceFonts = 'JetBrains Mono NF, JetBrains Mono Nerd Font, JetBrains Mono NL, SpaceMono NF, SpaceMono Nerd Font, monospace';
|
||||||
|
|
||||||
|
const codeBlockRegex = /^\s*```([a-zA-Z0-9]+)?\n?/;
|
||||||
const replacements = {
|
const replacements = {
|
||||||
'indents': [
|
'indents': [
|
||||||
{ name: 'BULLET', re: /^(\s*)([\*\-]\s)(.*)(\s*)$/, sub: ' $1- $3' },
|
{ name: 'BULLET', re: /^(\s*)([\*\-]\s)(.*)(\s*)$/, sub: ' $1- $3' },
|
||||||
@@ -31,6 +32,11 @@ const replacements = {
|
|||||||
{ name: 'INLCODE', re: /(`)([^`]*)(`)/g, sub: '<span font_weight="bold" font_family="' + monospaceFonts + '">$2</span>' },
|
{ name: 'INLCODE', re: /(`)([^`]*)(`)/g, sub: '<span font_weight="bold" font_family="' + monospaceFonts + '">$2</span>' },
|
||||||
// { name: 'UND', re: /(__|\*\*)(\S[\s\S]*?\S)(__|\*\*)/g, sub: "<u>$2</u>" },
|
// { name: 'UND', re: /(__|\*\*)(\S[\s\S]*?\S)(__|\*\*)/g, sub: "<u>$2</u>" },
|
||||||
],
|
],
|
||||||
|
'forceLatex': [
|
||||||
|
{ name: 'LATEX_INLINE_SQUARE', re: /\\\[(.*?)\\\]/g, sub: '\n```latex\n$1\n```' },
|
||||||
|
{ name: 'LATEX_INLINE_ROUND', re: /\\\((.*?)\\\)/g, sub: '\n```latex\n$1\n```' },
|
||||||
|
{ name: 'LATEX_INLINE_DOLLAR', re: /\$(.*?)\$/g, sub: '\n```latex\n$1\n```' }
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
const replaceCategory = (text, replaces) => {
|
const replaceCategory = (text, replaces) => {
|
||||||
@@ -42,12 +48,22 @@ const replaceCategory = (text, replaces) => {
|
|||||||
|
|
||||||
// Main function
|
// Main function
|
||||||
|
|
||||||
|
export function replaceInlineLatexWithCodeBlocks(text) {
|
||||||
|
return text.replace(/\\\[(.*?)\\\]|\\\((.*?)\\\)|\$(.*?)\$/gs, (match, square, round, dollar) => {
|
||||||
|
const latex = square || round || dollar;
|
||||||
|
return `\n\`\`\`latex\n${latex}\n\`\`\`\n`;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
export default (text) => {
|
export default (text) => {
|
||||||
let lines = text.split('\n')
|
let lines = text.split('\n')
|
||||||
let output = [];
|
let output = [];
|
||||||
|
let inCode = false;
|
||||||
// Replace
|
// Replace
|
||||||
for (const line of lines) {
|
for (const line of lines) {
|
||||||
let result = line;
|
let result = line;
|
||||||
|
if (codeBlockRegex.test(line)) inCode = !inCode;
|
||||||
|
if (inCode) continue;
|
||||||
result = replaceCategory(result, replacements.indents);
|
result = replaceCategory(result, replacements.indents);
|
||||||
result = replaceCategory(result, replacements.escapes);
|
result = replaceCategory(result, replacements.escapes);
|
||||||
result = replaceCategory(result, replacements.sections);
|
result = replaceCategory(result, replacements.sections);
|
||||||
@@ -74,6 +90,8 @@ int main(int argc, char* argv[]) {
|
|||||||
}
|
}
|
||||||
\`\`\`
|
\`\`\`
|
||||||
## LaTeX
|
## LaTeX
|
||||||
|
- Inline LaTeX: \\[ \\frac{d}{dx} \\left( \\frac{x-438}{x^2+23x-7} \\right) = \\frac{-x^2 + 869}{(x^2+23x-7)^2} \\]
|
||||||
|
- Block LaTeX:
|
||||||
\`\`\`latex
|
\`\`\`latex
|
||||||
\\frac{d}{dx} \\left( \\frac{x-438}{x^2+23x-7} \\right) = \\frac{-x^2 + 869}{(x^2+23x-7)^2} \\\\ → \\\\ cos(2x) = 2cos^2(x) - 1 = 1 - 2sin^2(x) = cos^2(x) - sin^2(x)
|
\\frac{d}{dx} \\left( \\frac{x-438}{x^2+23x-7} \\right) = \\frac{-x^2 + 869}{(x^2+23x-7)^2} \\\\ → \\\\ cos(2x) = 2cos^2(x) - 1 = 1 - 2sin^2(x) = cos^2(x) - sin^2(x)
|
||||||
\`\`\`
|
\`\`\`
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import * as Utils from 'resource:///com/github/Aylur/ags/utils.js';
|
|||||||
const { Box, Button, Label, Icon, Scrollable, Stack } = Widget;
|
const { Box, Button, Label, Icon, Scrollable, Stack } = Widget;
|
||||||
const { execAsync, exec } = Utils;
|
const { execAsync, exec } = Utils;
|
||||||
import { MaterialIcon } from '../../.commonwidgets/materialicon.js';
|
import { MaterialIcon } from '../../.commonwidgets/materialicon.js';
|
||||||
import md2pango from '../../.miscutils/md2pango.js';
|
import md2pango, { replaceInlineLatexWithCodeBlocks } from '../../.miscutils/md2pango.js';
|
||||||
import { darkMode } from "../../.miscutils/system.js";
|
import { darkMode } from "../../.miscutils/system.js";
|
||||||
|
|
||||||
const LATEX_DIR = `${GLib.get_user_cache_dir()}/ags/media/latex`;
|
const LATEX_DIR = `${GLib.get_user_cache_dir()}/ags/media/latex`;
|
||||||
@@ -216,12 +216,11 @@ const MessageContent = (content) => {
|
|||||||
child.destroy();
|
child.destroy();
|
||||||
}
|
}
|
||||||
contentBox.add(TextBlock())
|
contentBox.add(TextBlock())
|
||||||
// Loop lines. Put normal text in markdown parser
|
|
||||||
// and put code into code highlighter (TODO)
|
let lines = replaceInlineLatexWithCodeBlocks(content).split('\n');
|
||||||
let lines = content.split('\n');
|
|
||||||
let lastProcessed = 0;
|
let lastProcessed = 0;
|
||||||
let inCode = false;
|
let inCode = false;
|
||||||
for (const [index, line] of lines.entries()) {
|
for (let [index, line] of lines.entries()) {
|
||||||
// Code blocks
|
// Code blocks
|
||||||
const codeBlockRegex = /^\s*```([a-zA-Z0-9]+)?\n?/;
|
const codeBlockRegex = /^\s*```([a-zA-Z0-9]+)?\n?/;
|
||||||
if (codeBlockRegex.test(line)) {
|
if (codeBlockRegex.test(line)) {
|
||||||
|
|||||||
Reference in New Issue
Block a user