Something went wrong. Try again.
Logiverse library. (beta)
Something went wrong. Try again.
2.1 kB · 61 lines
TypeScript
at main
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162import type { Context } from "../index.js"
/** * Logari.Reader hook to convert a log's content into HTML */export function HyperText({ user: { instance }, content, cache, gif }: Context): { html: string, gif: string | null } { if (content === 'please add gifs') return { html: '', gif: 'https://todepond.com/lab/login/gif.gif' } /** * This regex has six hardcoded usernames, for the six users who have legacy usernames that include a space in them. * Going forward, it is expected that no more usernames with spaces will be created. If such a username was to be registered, * mentions of it with be disregarded. */ const mentionRegex = /(@(?:(?:lord cat)|(?:Jarvis Jordan)|(?:the beep man)|(?:Welcome back)|(?:Julia Eepy Moe)|(?:PodeTond 💚)|(?:[^ @]+))(?:@[a-z0-9.]+)?)/gi const operations = { "**": "b", "*": "i", "_": "i", "`": "code" }
const isOdd = (n: number) => !(n % 2) const queue: string[] = []
const html = content .replace(/&/g, '&') .replace(/</g, '<') .replace(/>/g, '>') .replace(/"/g, '"') .replace(/'/g, ''') .replace(/^> /, '<blockquote>') .split(/(\\[\\`\*_]|\`|\*\*|\*|_)/ig) .map((str: string, i: number) => { if (isOdd(i)) return str.split(mentionRegex) .map((str: string, i: number) => { // mentions const [, name, inst = instance] = str.split('@') const mention = `${name}@${inst}` if (isOdd(i) || !cache.has(mention)) return str else return `<a data-mention="${mention}">${str}</a>` }) .join('') else { // markup if (str.startsWith('\\')) return str.substring(1) const closingTime = queue.includes(str) if (closingTime) { queue.splice(queue.indexOf(str), 1) } else queue.unshift(str) return `<${((closingTime ? '/' : '') + operations[str as keyof typeof operations])}>` } }) .join('')
// add unclosed tags + queue.map(op => `</${operations[op as keyof typeof operations]}>`) .join('') + (content.startsWith('> ') ? '</blockquote>' : '')
return { html, gif }}