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,
}) {
const html = useMemo(() => {
const escaped = escapeHtml(props.raw);
return injectHTML(escaped)/*.replace(/\n/g, '
')*/;
}, [props.raw]);
return (
${quoteReplacer(reg, match[0].replace(/^> ?/gm, ""))}` + raw.substring(index + length) ); } // WARNING: the order is important const jvcodeMap: [RegExp, string | ((reg: RegExp, raw: string) => string)][] = [ [ // Quotes /^>.*(?:\n>.*)*/m, quoteReplacer ], [ // Wrap text with paragraphs /(^|>)([^<]+)($|<)/g, "$1
$2
$3" ], [ // Replace line breaks between tags with
/
\s*([\s\S]*?)\s*<\/p>/g, (reg, raw) => { // console.log(raw) return raw.replace(reg, (_, matched) => { // const randomId = (Math.random() + 1).toString(36).substring(2); console.log(matched) return `
${matched.replace(/\n/g, "
")}
| )https?:\/\/image\.noelshack\.com\/(?:fichiers|minis)([A-z0-9/\-_.]+)/gm,
/(^|<[a-z]+\/?>| )https?:\/\/image\.noelshack\.com\/(?:fichiers|minis)([A-z0-9/\-_.]+)/gm,
"$1"
],
[ // Vocaroo
/(^|<[a-z]+\/?>| )https:\/\/vocaroo.com\/(.+)/gm,
"$1
| )" + mapping[0].replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), "gm"),
`$1`
];
})
];
function injectHTML(input: string): string {
// console.log("---------------------")
let text = input.slice()
for (const [regex, htmlOrFunc] of jvcodeMap) {
// Reset the regex @see https://stackoverflow.com/a/11477448/5912637
regex.lastIndex = 0;
// const regex = new RegExp(bbCode, 'gi');
// console.log(regex, html, input)
if (htmlOrFunc instanceof Function) {
text = htmlOrFunc(regex, text);
} else {
// console.log(regex)
// console.log(text)
text = text.replace(regex, htmlOrFunc as string);
}
}
// console.log(input)
return text;
}
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,
// }
// });
// }