T3.chat - Markdown code un-indenter

Automatically un-indents code blocks in the message input.

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

You will need to install an extension such as Tampermonkey to install this script.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

You will need to install an extension such as Tampermonkey to install this script.

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==UserScript==
// @name        T3.chat - Markdown code un-indenter
// @namespace   Violentmonkey Scripts
// @match       https://beta.t3.chat/*
// @match       https://t3.chat/*
// @grant       none
// @version     1.0
// @author      koza.dev
// @description Automatically un-indents code blocks in the message input.
// @license MIT
// ==/UserScript==

(function() {
    let debounceTimer

    const unindent = (text) => {
        return text.replace(/```([\s\S]*?)```/g, (match, code) => {
            const lines = code.split('\n')

            // Filter out empty lines to find the actual minimum indentation
            const contentLines = lines.filter((line) => line.trim().length > 0)
            if (contentLines.length === 0) return match

            const minIndent = Math.min(
                ...contentLines.map((line) => line.match(/^\s*/)[0].length)
            )

            const processedCode = lines
                .map((line) => (line.length >= minIndent ? line.slice(minIndent) : line.trimStart()))
                .join('\n')

            return '```' + processedCode + '```'
        })
    }

    const processInput = (target) => {
        const originalValue = target.value
        const newValue = unindent(originalValue)

        if (originalValue !== newValue) {
            const start = target.selectionStart
            const end = target.selectionEnd

            target.value = newValue

            // Restore cursor position
            target.selectionStart = start
            target.selectionEnd = end

            // Trigger Svelte 5 reactivity
            target.dispatchEvent(
                new InputEvent('input', {
                    bubbles: true,
                    cancelable: true,
                    inputType: 'insertText'
                })
            )
            target.dispatchEvent(new Event('change', { bubbles: true }))
        }
    }

    window.addEventListener('input', (e) => {
        if (e.target.id !== 'chat-input') return

        clearTimeout(debounceTimer)
        debounceTimer = setTimeout(() => {
            processInput(e.target)
        }, 500)
    })
})()