iglais

joined 2 weeks ago
[–] iglais@lemmy.world 2 points 1 week ago (1 children)

i noticed that this morning, it's add that glyph or a zero length character. breaks a lot of things. i had to update the JSON parser i use to try and accommodate those additions. works, some of the time.

parseJSON: (input) => { if (!input) return null;

        // 1. OBJECT HANDLING (Already parsed)
        if (typeof input === 'object') {
            if (typeof input.text === 'string') return Utils.parseJSON(input.text);
            if (typeof input.generatedText === 'string') return Utils.parseJSON(input.generatedText);
            return input;
        }

        // 2. STRING HANDLING (The Toughened Shield)
        if (typeof input === 'string') {
            let clean = input.trim();
            
            // A. Strip Markdown Code Blocks
            clean = clean.replace(/```json/gi, "").replace(/```/g, "");
            
            // B. REINFORCED EXTRACTION
            // We find the FIRST occurrence of an opening bracket/brace
            // and the LAST occurrence of the matching closing bracket/brace.
            const firstObj = clean.indexOf('{');
            const firstArr = clean.indexOf('[');
            
            let firstOpen = -1;
            let lastClose = -1;

            if (firstObj !== -1 && (firstArr === -1 || firstObj < firstArr)) {
                firstOpen = firstObj;
                lastClose = clean.lastIndexOf('}');
            } else if (firstArr !== -1) {
                firstOpen = firstArr;
                lastClose = clean.lastIndexOf(']');
            }
            
            if (firstOpen !== -1 && lastClose !== -1 && lastClose > firstOpen) {
                // C. HARD TRUNCATION
                // We extract ONLY what is between the brackets.
                // This kills the trailing "〖" and any other AI rambling.
                clean = clean.substring(firstOpen, lastClose + 1);

                // D. Post-Extraction Cleanup
                clean = clean.replace(/,(\s*[}\]])/g, '$1'); // Remove trailing commas
                
                try {
                    return JSON.parse(clean);
                } catch (e) {
                    // LAST DITCH: If it still fails, it's usually unescaped newlines in a string
                    try {
                        // Replace literal newlines inside JSON strings with escaped \n
                        const escaped = clean.replace(/\n/g, "\\n").replace(/\r/g, "\\r");
                        return JSON.parse(escaped);
                    } catch (e2) {
                        console.warn("[Utils] JSON Parse Failed despite extraction:", e.message);
                    }
                }
            }
        }
        
        console.warn("[Utils] JSON Parse Total Failure. Input sample:", input.substring(0, 100));
        return null;
    }
[–] iglais@lemmy.world 1 points 2 weeks ago (1 children)

i have no idea how to go about doing that. i haven't done serious coding in 3 decades and never in html/css or js. i'm using this project to help me get back into the mold of coding and to learn how to do html/css and js. so far, i've been relying pretty heavily on ai support to learn how to do this, slowly getting better at making my own edits.

this elf ear thing, that completely baffles me. maybe it's a flavor text thing? who knows.

 

I've created this generator and things are working okay. However, I've noticed that a lot of NPC portraits are coming out with long, pointed elven ears, even though I'm pretty sure I haven't put anything in the code to suggest such ears. I'd appreciate it if someone could take a look and let me know if I did something inadvertently or even why that is occurring.