Aight

Venture: : Post Format Parser



Why
Now that I've started to write a blog and have no underlying platform, I figure I need a parser for stuff like formatting. Shitting <br>s all over the place is just really not what I wanna be doing for any longer than I have to, so I'm writing a tiny parser.


What
So first off I'm pretty sure that the first thing I've gotta do is escape unwanted characters.
I'm pretty sure there was a js function for that.

Nope, there's a jquery one, but that's just too much for what I'm tryina do.
I don't have anything against jquery, but for what I need it's overkill.

I found another function tho:

function escapeHtml(text) {
  return text
    .replace(/&/g, "&amp;")
    .replace(/</g, "&lt;")
    .replace(/>/g, "&gt;")
    .replace(/"/g, "&quot;")
    .replace(/'/g, "&#039;");
}
    

So yeah, let's escape ourselves some of that html:



Works like a charm:


Now gotta do the newlines, decided to use replace as well. It doesn't need to be pretty, it needs to work.

Also, changed the container to a hidden textarea so chrome wouldn't automatically place the closing tags for things that aren't actually tags (as you can see it did in the 2 previous pics).



which now works:


Now onto the elements that matter. Let's do links first.
I'm imagining links having this format:



But that easily becomes a problem when you have to account for the fact that there might be a "]" somewhere in the text of the link, because I'd like this as restriction-free as possible.

So, even though it's really ugly, I've decided that, for every tag, I will use the following combination of characters: "[<~" for start and "~>]" for end.

So I figure the best thing to do is use a regex. I have no experience with regexes so Imma read on that.

Magic of writing, I just did. Took me half an hour, took you however much time to shift your eyes from the previous sentence.

So regexes are weird but fun, the one I've ended up making is:

/\[__LINK=(?:\"|&quot;)(.+?)(?:\"|&quot;)\]\[(?:&lt;|<)~(.+?)~(?:&gt;|>)\]/g

Not gonna get to explaining it, it's fairly simple to learn, so if you curious, go search.

.

I've now finished what I would consider to be a semi-decent version of this.
Don't get me wrong, it's pretty fucking shit compared to stuff that's supposed to be decent (such as the stackoverflow or discord ones), but it gets the job done for this blog.

This post was already made using this, inspect the page to find the original html (Imma leave it as comment).
You can test it here.

Here's it:


And it working:



I will admit this wasn't a project I had too much fun doing, didn't loose too much time with it, it just comes in handy, maybe in the future I'll make it decent.

Hope I could help or entertain, have a nice day or something. I never know what to say to people. Can't you just like say "sup" and "l8r" to literally everyone?. It pisses me off, I never get morning/afternoon right (may not make much sense cuz it's different in portuguese).
(in case you're wondering about that ?. , yes it was on purpose and it's what I use to make it as obvious as possible that it's a rectorical question (it's not like you can answer buy meh))

Good talk bye.