import { existsSync } from 'fs'; import { v4 as uuidV4, v5 as uuidV5, NIL as uuidNIL } from 'uuid'; import { saveImg, getImgType } from './image'; import type { Thread, Comment } from '../models/Thread' export async function processThreadIn(board: string, thread: Thread) { for(let comment of thread.comments) { comment.id += thread.id; comment.content = replaceURLs(comment.content, board, thread.id); } } export function processThreadOut(form: any): Thread { let img = form.get('image'); let t: Thread = { title: form.get('title'), content: form.get('content'), image: saveImg(img), imagetype: getImgType(img), } return t; } export function processCommentOut(form: any): Comment { let img = form.get('image'); let c: Comment = { content: form.get('content'), image: saveImg(img), imagetype: getImgType(img), } return c; } function replaceURLs(text: string, board: string, OPtid?: string): string { if(!text) return; let quoteRegex = />\s.*/g; text = text.replace(quoteRegex, (q) => { return ` ${q} `; }); let replyRegex = />>\d+/g; text = text.replace(replyRegex, (id) => { let showid = id; id = id.replace(/>/g, ''); if(id == OPtid) showid += "(OP)"; // to highlight referenced id return ` ${showid} `; }); // link to thread on other board // let otherthreadlinkRegex = />>>\/.*\/[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}/g; // text = text.replace(otherthreadlinkRegex, (id) => { // let link = id.slice(4); // let parts = link.split('/'); // let linkid = parts[1]; let board = parts[0]; // return ` >>>/${board}/${linkid} ` // }); // link to thread on this board // let threadlinkRegex = />>>[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}/g; // text = text.replace(threadlinkRegex, (id) => { // let linkid = id.slice(3); // remove the first >>> // return ` ${id} ` // }); // link to another board // let otherboardRegex = />>>\/.*\/($|[^0-9a-fA-F])/g; // text = text.replace(otherboardRegex, (id) => { // let boardid = id.split('/')[1]; // return ` >>>/${boardid}/ ` // }); let newlineRegex = /(\r\n|\r|\n)/g; text = text.replace(newlineRegex, () => { return '
' }); let urlRegex = /(((https?:\/\/)|(www\.))[^\s]+)/g; return text.replace(urlRegex, (url) => { var hyperlink = url; if (!hyperlink.match('^https?:\/\/')) { hyperlink = 'http://' + hyperlink; } return ' ' + url + ' ' }); }