import { debugError, } from '@ska/utils' /** Parse HTML from AI response. */ export const getHTML = (input: string) => { input = input.trim() if(input.indexOf('```html') > 0) { input = input.split('```html')[1].trim() } input = input.replaceAll('```html', '').replaceAll('```', '').trim() const parser = new DOMParser() const doc = parser.parseFromString(input, 'text/html') return doc.body.querySelectorAll('*').length > 0 ? doc.body.innerHTML : false } /** Parse JSON from AI response. */ export const getJSON = (input: string): object | false => { let json = {} input = input.trim() if(input.indexOf('```json') > 0) { input = input.split('```json')[1].trim() } input = input.replaceAll('```json', '').replaceAll('```', '').trim() try { json = JSON.parse(input) } catch(e) { debugError('Failed parsing JSON from AI response', input, e) return false } return json }