import style from "./style.module.scss";
import {useMemo} from "preact/hooks";
import {smileysMap} from "../../utils/smileys";
// const blocks = {
// text: "text",
// img: "img",
// }
//
// type Segment = {
// type: keyof typeof blocks,
// content: string,
// }
export function Preview(props: {
raw: string,
}) {
// console.log(parseRaw(props.raw))
// const segments = useMemo(() => {
// return parseRaw(props.raw);
// }, [props.raw]);
const html = useMemo(() => {
// console.log(escapeHtml(props.raw))
const escaped = escapeHtml(props.raw);
// const withBr = escaped.replace(/\n/g, '
')
// console.log(withBr)
return injectHTML(escaped).replace(/\n/g, '
')
}, [props.raw]);
return (
)
//
// return (
//
// {segments.map(seg => {
// if (seg.type === blocks.text) {
// return seg.content
// }
//
// if (seg.type === blocks.img) {
// return

// }
// })}
//
// )
}
const jvcodeMap: [RegExp, string | ((reg: RegExp, raw: string) => string)][] = [
// [/(https?:\/\/image\.noelshack\.com\/\S+)/g]: "
"
[ // Stickers
/(^| )https?:\/\/image\.noelshack\.com\/(?:fichiers|minis)(\S+)/gm,
"$1
"
],
[ // Vocaroo
/(^| )https:\/\/vocaroo.com\/(.+)/gm,
"$1"
],
[ // Citations
/^(?:>.*(?:\n>.*)*)/g,
(reg, raw) => {
// return raw
const match = reg.exec(raw);
if (!match) return raw;
console.log(match);
const index = match.index;
const length = match[0].length;
return raw.substring(0, index) + `${match[0].replace(/^>/gm, "")}
` + raw.substring(index + length);
// console.log(match)
// const content = match[0].replace(/^>/gm, "");
// return `${content}
`;
}
],
[ // Spoil
/<spoil>(.*?)<\/spoil>/gm,
(reg, raw) => { // Citations
return raw.replace(reg, (_, matched) => {
const randomId = (Math.random() + 1).toString(36).substring(2);
return `${matched}`;
});
}
],
[ // Regular links
/(^| )(https?:\/\/\S+)/gm,
"$1$2"
],
// Generate regexes for smileys
// ...smileysMap.map((maping) => {
// return [new RegExp(
// Object.keys(smileysMap).map(s => (
// "(?:(?:^| )" + s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') + ")"
// )).join("|"), "gm"
// ), `
`]
// })
// (() => {
// new RegExp(
// smileysMap.map((mapping) => (
// "(?:(?:^| )" + s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') + ")"
// )).join("|"), "gm"
// )
// return [/(https?:\/\/image\.noelshack\.com\/\S+)/g, "
"]
// })()
...smileysMap.map(mapping => {
return [
new RegExp("(?:(^| )" + mapping[0].replace(/[.*+?^${}()|[\]\\]/g, '\\$&') + ")", "gm"),
`$1
`
];
})
];
// console.log(jvcodeMap)
function injectHTML(input: string): string {
let previousInput;
// console.log(Object.entries(jvcodeMap))
do {
previousInput = input; // Keep track of input before replacements
for (const [regex, htmlOrFunc] of jvcodeMap) {
// const regex = new RegExp(bbCode, 'gi');
// console.log(regex, html, input)
if (htmlOrFunc instanceof Function) {
input = htmlOrFunc(regex, input);
} else {
input = input.replace(regex, htmlOrFunc as string);
}
}
// } while (input !== previousInput); // Repeat until no more replacements
} while (false); // Repeat until no more replacements
// console.log(input)
return input;
}
function escapeHtml(unsafe: string): string {
// unsafe.re
return unsafe
.replace(/&/g, "&")
.replace(//g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}
// function parseRaw(raw: string): Segment[] {
// // Regex pour capturer les URLs (HTTP, HTTPS)
// const urlRegex = /(https?:\/\/[^\s]+)/g;
//
// // Découper le texte en utilisant la regex, en gardant les URLs comme blocs séparés
// return raw.split(urlRegex).filter(seg => seg !== "").map(seg => {
// return {
// type: seg.includes(".png") || seg.includes(".jpg") ? blocks.img : blocks.text,
// content: seg,
// }
// });
// }