// text.js // Copyright (C) 2024 DTP Technologies, LLC // All Rights Reserved 'use strict'; import striptags from 'striptags'; import unzalgo from 'unzalgo'; import shoetest from 'shoetest'; import diacritics from 'diacritics'; import DtpTextFilter from './lib/edit-with-vi.js'; import { SiteService, SiteError } from '../../lib/site-lib.js'; export default class TextService extends SiteService { static get slug () { return 'text'; } static get name ( ) { return 'TextService'; } constructor (dtp) { super(dtp, TextService); } /** * Basic text cleaning function to remove Zalgo and tags. * @param {String} text The text to be cleaned * @returns The cleaned text */ clean (text) { text = unzalgo.clean(text); text = striptags(text.trim()); return text; } /** * The heavy hammer of text filtering that removes all malicious and annoying * things I know about as of this writing. Zalgo, tags, shoetest, diacritics, * and our own custom nonsense UTF-8 and Unicode filters. * * This filter is very heavy-handed and merciless. * * @param {String} text The text to be filtered * @returns The filtered text */ filter (text) { if (!text || (typeof text !== 'string') || (text.length < 1)) { return text; } text = DtpTextFilter.filterNonsense(text); text = DtpTextFilter.filterGuff(text); text = DtpTextFilter.filterHtml(text); text = shoetest.simplify(text); text = diacritics.remove(text); for (const filter of this.chatFilters) { const regex = new RegExp(filter, 'gi'); if (text.match(regex)) { this.log.alert('chat filter text match', { filter }); throw new SiteError(403, 'Text input rejected'); } } /* * Once all the stupidity has been stripped, strip the HTML * tags that might remain. */ return this.clean(text); } }