aboutsummaryrefslogtreecommitdiff
path: root/src/lib/thread.ts
blob: 6bc54ce6e498215e8048babc94c5783ea33cc65d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import { writeFileSync, existsSync } from 'fs';
import { getImg } from './image';
import type Thread from '../models/Thread'

export async function processThreadIn(board: string, thread: Thread, comments? =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(!comments) 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);
  }
}

export async function processThreadOUT(board: string, thread: Thread, comments? = false) {

}

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 ` <a class="${id}" href="#${id}" onmouseover="document.getElementById('${id}').classList.add('targeted')" onmouseleave="document.getElementById('${id}').classList.remove('targeted')"> ${showid} </a> `;
    });
  }

  // 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 ` <a href="${board}/${linkid}#{linkid}"> >>>/${board}/${linkid} </a> `
  });
  // 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 ` <a href="../${board}/${linkid}#${linkid}"> ${id} </a> `
  });
  // link to another board
  let otherboardRegex = />>>\/.*\/($|[^0-9a-fA-F])/g;
  text = text.replace(otherboardRegex, (id) => {
    let boardid = id.split('/')[1];
    return ` <a href="../${boardid}"> >>>/${boardid}/ </a> `
  });

  let newlineRegex = /(\r\n|\r|\n)/g;
  text = text.replace(newlineRegex, () => {
	return ' <br> '
  });

  let urlRegex = /(((https?:\/\/)|(www\.))[^\s]+)/g;
  return text.replace(urlRegex, (url) => {
	var hyperlink = url;
	if (!hyperlink.match('^https?:\/\/')) {
	  hyperlink = 'http://' + hyperlink;
	}
	return ' <a href="' + hyperlink + '" target="_blank" rel="noopener noreferrer">' + url + '</a> '
  });
}