Note

Contenuto del File

Ecco un esempio di codice PHP che crea due form di testo per
output in italiano e in formato
write a php with 2 textforms: 1 for markdown, 2 for css (save both of them in indexdb js in browser, just the latest). sanitize avoiding html tags.
and then, show the preview in a different windows, using the custom css
 
codestral-latest 0.7  
Array
(
    [id] => 860de50698bb43048e7d2a137888d7ba
    [created] => 1759754684
    [model] => codestral-latest
    [usage] => Array
        (
            [prompt_tokens] => 79
            [total_tokens] => 1539
            [completion_tokens] => 1460
        )

    [object] => chat.completion
    [choices] => Array
        (
            [0] => Array
                (
                    [index] => 0
                    [finish_reason] => stop
                    [message] => Array
                        (
                            [role] => assistant
                            [tool_calls] => 
                            [content] => Ecco un esempio di codice PHP che crea due form di testo per Markdown e CSS, salva l'ultimo input in IndexedDB del browser, e mostra una preview con il CSS personalizzato:

```php
<!DOCTYPE html>
<html lang="it">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Editor Markdown e CSS</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
        }
        .container {
            display: flex;
            gap: 20px;
        }
        .form-group {
            flex: 1;
        }
        textarea {
            width: 100%;
            height: 200px;
            margin-bottom: 10px;
        }
        button {
            padding: 10px 15px;
            background-color: #4CAF50;
            color: white;
            border: none;
            cursor: pointer;
        }
        button:hover {
            background-color: #45a049;
        }
        #preview {
            margin-top: 20px;
            padding: 20px;
            border: 1px solid #ddd;
            min-height: 200px;
        }
    </style>
</head>
<body>
    <h1>Editor Markdown e CSS</h1>
    <div class="container">
        <div class="form-group">
            <h2>Markdown</h2>
            <textarea id="markdownInput" placeholder="Inserisci il tuo Markdown qui..."></textarea>
            <button onclick="saveToIndexedDB('markdown', document.getElementById('markdownInput').value)">Salva Markdown</button>
        </div>
        <div class="form-group">
            <h2>CSS</h2>
            <textarea id="cssInput" placeholder="Inserisci il tuo CSS qui..."></textarea>
            <button onclick="saveToIndexedDB('css', document.getElementById('cssInput').value)">Salva CSS</button>
        </div>
    </div>
    <button onclick="showPreview()">Mostra Preview</button>
    <div id="preview"></div>

    <script>
        // Funzione per salvare i dati in IndexedDB
        function saveToIndexedDB(key, value) {
            // Sanitizza l'input rimuovendo i tag HTML
            const sanitizedValue = value.replace(/<[^>]*>/g, '');

            const request = indexedDB.open('EditorDB', 1);

            request.onupgradeneeded = function(event) {
                const db = event.target.result;
                if (!db.objectStoreNames.contains('editorData')) {
                    db.createObjectStore('editorData');
                }
            };

            request.onsuccess = function(event) {
                const db = event.target.result;
                const transaction = db.transaction('editorData', 'readwrite');
                const store = transaction.objectStore('editorData');

                store.put(sanitizedValue, key);

                transaction.oncomplete = function() {
                    console.log(`Dati ${key} salvati con successo`);
                };
            };

            request.onerror = function(event) {
                console.error('Errore durante l\'apertura del database:', event.target.error);
            };
        }

        // Funzione per caricare i dati da IndexedDB
        function loadFromIndexedDB(key) {
            return new Promise((resolve, reject) => {
                const request = indexedDB.open('EditorDB', 1);

                request.onsuccess = function(event) {
                    const db = event.target.result;
                    const transaction = db.transaction('editorData', 'readonly');
                    const store = transaction.objectStore('editorData');

                    const getRequest = store.get(key);

                    getRequest.onsuccess = function(event) {
                        resolve(event.target.result || '');
                    };

                    getRequest.onerror = function(event) {
                        reject(event.target.error);
                    };
                };

                request.onerror = function(event) {
                    reject(event.target.error);
                };
            });
        }

        // Funzione per mostrare la preview
        async function showPreview() {
            const markdown = await loadFromIndexedDB('markdown');
            const css = await loadFromIndexedDB('css');

            // Converti Markdown in HTML (semplice conversione)
            const html = markdown
                .replace(/^# (.*$)/gm, '<h1>$1</h1>')
                .replace(/^## (.*$)/gm, '<h2>$1</h2>')
                .replace(/^### (.*$)/gm, '<h3>$1</h3>')
                .replace(/^\> (.*$)/gm, '<blockquote>$1</blockquote>')
                .replace(/\*\*(.*?)\*\*/g, '<strong>$1</strong>')
                .replace(/\*(.*?)\*/g, '<em>$1</em>')
                .replace(/!\[(.*?)\]\((.*?)\)/g, '<img alt="$1" src="$2" />')
                .replace(/\[(.*?)\]\((.*?)\)/g, '<a href="$2">$1</a>')
                .replace(/\n$/gm, '<br />');

            // Crea una nuova finestra per la preview
            const previewWindow = window.open('', 'Preview', 'width=800,height=600');
            previewWindow.document.write(`
                <!DOCTYPE html>
                <html lang="it">
                <head>
                    <meta charset="UTF-8">
                    <meta name="viewport" content="width=device-width, initial-scale=1.0">
                    <title>Preview</title>
                    <style>${css}</style>
                </head>
                <body>
                    ${html}
                </body>
                </html>
            `);
            previewWindow.document.close();
        }

        // Carica i dati salvati al caricamento della pagina
        window.onload = async function() {
            const markdown = await loadFromIndexedDB('markdown');
            const css = await loadFromIndexedDB('css');

            document.getElementById('markdownInput').value = markdown;
            document.getElementById('cssInput').value = css;
        };
    </script>
</body>
</html>
```

Questo codice PHP crea una pagina web con due form di testo per Markdown e CSS. Utilizza JavaScript per salvare l'ultimo input in IndexedDB del browser e mostrare una preview con il CSS personalizzato in una nuova finestra. La funzione di sanitizzazione rimuove i tag HTML dall'input per prevenire attacchi XSS.
                        )

                )

        )

)