Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I get the same text formatting effect as on the website? #42

Closed
lriley2020 opened this issue Aug 20, 2021 · 2 comments
Closed
Labels

Comments

@lriley2020
Copy link

I know this isn't an issue with the project, so sorry in advance. I was wondering how you created the nice multi-color formatting effect on the front page of the gwsocket.io as this would be useful for my own implementation. I would appreciate it if you could share some pointers as to how this could be recreated as I am completely clueless here!

@allinurl
Copy link
Owner

I do a simple regex replace/highlight. Here's the code that inserts the relevant class:

	function hl(needle, haystack, cl) {
		var reg = new RegExp(needle, 'gi');
		return haystack.replace(reg, function(str) {
			return '<span class="' + cl + '">' + str + '</span>'
		});
	}

	function addMessage(msg) {
		var msgs = $('.prompt .message');
		var count = msgs.length;

		msg = hl('GET|POST', msg, 'purple');
		msg = hl('HTTP\/1\.1|HTTP\/1\.0', msg, 'orange');
		msg = hl(':80', msg, 'red');
		msg = hl('\\[.*\\]', msg, 'green');
		msg = hl('(?:[0-9]{1,3}\.){3}[0-9]{1,3}', msg, 'blue');

		if (count == 20)
			msgs.first().remove();

		$('<div />', {
			'class': 'message'
		})
		.html(msg)
		.appendTo($('.prompt'));
	}

Hope that helps.

@allinurl
Copy link
Owner

Closing this. Feel free to reopen it if needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants