From 8f5278eb443864910dd9c2131c992d71e3af2d20 Mon Sep 17 00:00:00 2001 From: kartofen Date: Fri, 26 Aug 2022 23:54:17 +0300 Subject: Big bang --- src/lib/thread.ts | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 src/lib/thread.ts (limited to 'src/lib/thread.ts') diff --git a/src/lib/thread.ts b/src/lib/thread.ts new file mode 100644 index 0000000..d0a4fce --- /dev/null +++ b/src/lib/thread.ts @@ -0,0 +1,80 @@ +import { writeFileSync, existsSync } from 'fs'; +import { getImg } from './image'; +import type Thread from '../models/Thread' + +export async function processThreadIn(board: string, thread: Thread, procComments?: false) { + if(!thread || !board) return; + let imageId: string = thread.imageId; + + if(existsSync(`public/images/${imageId}`)) + thread.imageId = `/images/${imageId}`; + else + thread.imageId = await getImg(imageId); + + thread.threadText = replaceURLs(thread.threadText, board, thread.id); + + if(!procComments) return; + + for(let comment of thread.comments) + { + let cimageId = comment.imageId; + if(existsSync(`public/images/${cimageId}`)) + comment.imageId = `/images/${cimageId}`; + else + comment.imageId = await getImg(cimageId); + + comment.commentText = replaceURLs(comment.commentText, board, thread.id); + } +} + +function replaceURLs(text: string, board: string, OPtid?: string): string { + if(!text) return; + + if(OPtid) { + let replyRegex = /(^|[^>])>>[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(replyRegex, (id) => { + let showid = id; + id = id.replace(/(>|\n| )/g, ''); + if(id == OPtid) showid += "(OP)"; + // this adds the targeted class + // to the target id when its hovered on + // and removes it when it stops hovering + 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 + ' ' + }); +} -- cgit v1.2.3