Blog

Tags:

Read More Buttons and Blog Post Tags

26/10/2022

The most complicated part of making this website was undoubtably using JavaScript for the functionality of the read-more/read-less buttons on these posts and the tags to filter posts by. Although I did find lots of examples of code online which I could have copied, pasted, and slightly altered to get this functionality, I wanted to learn and write the JS myself. What I came up with might not be the most efficient way of getting these buttons to work, but it does work!


In explaining this I am going to use a simplified version of the code just to avoid taking up loads of space, but if you want to read the full version you can find the source code to this site on my GitHub, which is linked in the navigation bar.

(There is an error in the code above: on the second more/less button the line should read "onclick=btnSelection('more1')", not 'more5'.)


Blog Post Tags

Above you can see simplified versions of the html and css for a blog page. Ultimately how you style the buttons and content doesn't matter, so the only focus of this is the classes I have assigned to each html tag, and the classes .show and .hide in the css. For the blog post tags, on clicking on the tag the user initiates the function tagSelection(c) where c is the tag which this clicked. The purpose of this function is very simple. It just needs to add the class .show to all blog posts with the class "c", and add the class .hide to all blog posts without the class "c". For example when you click 'Tag 1', you initiate tagSelection('tag1'), which hides all blog posts without "tag1" as a class, and shows the ones with "tag1" as a class. Inside the function tagSelection(c) we initially define a variable x, which is a NodeList (sort of like an array) of all the classes attributed to each blog post. I use document.querySelectorAll(".all") since every blog post has the "all" class, and querySelectorAll exactly returns the classes included with "all". The NodeList (x) hence has as many nodes as there are blog posts. So for the first post, x[1], the node is article.all.tag1 and for node 2 it is article.all.tag2. Moving on then to the for loop, this just cycles through each node one at a time, and of each node the if statement asks if "c" (the tag we selected) appears in that node. If it does then we add .show to that blog post, if it doesn't we add .hide. Adding the classes "show" and "hide" respectively was not hard, 'element.className += " " + arr2;' is the only line needed to do this, where 'element' is 'x[i]' and 'name' is either "show" or "hide". The problem I found from here, however, is that if the user wants to click on another tag after clicking the first one, all the posts now already have the class "hide" or "show" added, which will affect which posts show/hide in this second click. To solve this problem I created the function checkAndRemove, which checks for "show" and "hide", and removes them if they appear.

The important objects to take away from this are:
  • document.querySelectorAll(".all") : which gets all the classes of the element with the class "all";
  • .split() : which splits our NodeList into individual strings;
  • .indexOf() : which checks if the string "c" == one of our node strings, if it does it returns the position of c in that node, else it returns -1.

Read More/Read Less

The read-more/-less buttons work on much the same principle as the blog post tags. First btnSelection(c) is initiated by a click, on which a for loop asks if each of the possible spans which are hidden are equivalent to the "c" that was clicked. When it finds the one that does contain the class "c" it toggles show. Then it changes the inner text of the button corresponding to the span that has been shown.

There is only one important take away from this:
  • .innerText : allows us to check and change the inner text of an element.

Hosting my site with Netlify

21/10/2022

My website is a static site, meaning that the webpage you view in your browser exists exactly as it is stored in the HTML files I wrote and the site won't change unless I directly change the code in those files. I use Netlify - a web-hosting infrastructure - to render the files I have written and then to upload them to the web. In short Netlify has three massive benefits when compared to other web-hosters: it operates through my GitHub repository; it's fast; and it's free.


Netlify operates through my GitHub repository, meaning that it takes the code from my repository and then renders it so it can be viewed from a browser. Since I use Visual Studio Code to edit my code, this feature makes changing and updating my site incredibly simple. I have VScode linked to my GitHub, so with just a few clicks on VScode after editing one of the webpages I can commit all the changes and then push them to my sites GitHub repository. This repository is then automatically used by Netlify in rendering my site, so any changes I make in VScode essentially almost automatically update the site as viewed in your browser. The ability to commit and push code to GitHub from VScode is something I discovered after I has started using VScode, but nonetheless this happening in just a few clicks, rather than havening to copy and paste HTML files into a web-hosting service, has saved me so much time.

The second and third advantages of Netlify are its speed and cost. When you visit Netlify it provides a pre-loaded version of the site rather than loading the site for each individual visit. This reduces load times and while I don't think this has huge impliciations for me, due to the small size and simplicity of my site, it nonetheless is a benefit of the service. Netlify being free is probably the largest reason for which I choose to use it. For no fee Netlify hosts static sites for a bandwidth of 100GB per month. I did of course have to buy my domain for the next few years, but that was really the only cost involved in generating this site.

Building a Static Website in HTML, CSS, and JavaScript

21/10/2022

Lorem ipsum dolor sit amet consectetur adipisicing elit. Dicta tempore qui inventore necessitatibus eligendi quae sapiente illo sequi, commodi aspernatur aperiam! Nostrum harum quae ducimus quam cumque amet delectus aliquam. Lorem ipsum dolor sit amet consectetur adipisicing elit. Similique debitis, eligendi incidunt officiis omnis porro quasi consectetur laborum perspiciatis atque, blanditiis reiciendis harum natus soluta! Recusandae eos laborum repellat molestiae? Lorem, ipsum dolor sit amet consectetur adipisicing elit. Quod, natus. Reprehenderit, nisi facere. Molestiae recusandae tempora dolores vitae, ipsa itaque facilis iste temporibus vel architecto sapiente aperiam laboriosam quaerat explicabo? Lorem ipsum dolor sit amet consectetur adipisicing elit. Odit veritatis aut tempore, magnam dolores recusandae minima error consequuntur eaque excepturi, quasi eligendi accusantium ipsam corrupti explicabo placeat quod quos asperiores.
Lorem ipsum dolor sit amet consectetur adipisicing elit. Dicta tempore qui inventore necessitatibus eligendi quae sapiente illo sequi, commodi aspernatur aperiam! Nostrum harum quae ducimus quam cumque amet delectus aliquam. Lorem ipsum dolor sit amet consectetur adipisicing elit. Similique debitis, eligendi incidunt officiis omnis porro quasi consectetur laborum perspiciatis atque, blanditiis reiciendis harum natus soluta! Recusandae eos laborum repellat molestiae? Lorem, ipsum dolor sit amet consectetur adipisicing elit. Quod, natus. Reprehenderit, nisi facere. Molestiae recusandae tempora dolores vitae, ipsa itaque facilis iste temporibus vel architecto sapiente aperiam laboriosam quaerat explicabo? Lorem ipsum dolor sit amet consectetur adipisicing elit. Odit veritatis aut tempore, magnam dolores recusandae minima error consequuntur eaque excepturi, quasi eligendi accusantium ipsam corrupti explicabo placeat quod quos asperiores.

Transferring MathType (Word) into LaTeX

25/10/2022

I wrote my finals notes in MS Word but when I decided to make this site and share them I wanted to use a PDF format. I started copying and pasting my notes into R Studio to be converted into LaTeX PDFs and almost immediately noticed a serious problem with equations I had written in Word. All the equations I had written used the MathType add-on which, at the time, was very helpful and sped up my revision massively. The problem with it, however, is that once an equation has been written it is outputted as an image into your Word document. When copying and pasting from Word to R Studio I was therefore really copying and pasting an image rather than an equation.
This meant none of my equations could be made into the necessary $$x=\alpha + \beta y$$ LaTeX format, and so couldn't be read by R Markdown. Initially the only solution seemed to be re-typing all the equations into LaTeX, though very quickly after starting that tedious process I decided to search for a shortcut instead.

The best solution I found was to use an online service called Mathpix. Mathpix, for a $4.99 subscription fee, offers 500 pages of PDF conversion into the appropiate LaTeX format. It uses AI powered document conversion technology to convert either snips (such as photos) or entire documents into LaTeX. Although it was annoying to have to pay, the service turned what would have been a 3 month job into one that took a matter of minutes. That said the software is by no means perfect. Often vectors and other symbols that I had used $\boldsymbol$ for got converted to $\mathbb$ or things got to converted to $\mathrm{}$ when I just wanted plain LaTeX maths. Similarly Mathpix didn't seem to recognise bullet points and converted them to $\circ$'s instead. I solved all these problems simply by using ctrl+f and then replacing, for example, $\circ$ with '-'. I'm sure this wasn't the fastest solution but I couldn't be bothered to search for another fix.

Making LaTeX PDFs in R Markdown

21/10/2022

Lorem ipsum dolor sit amet consectetur adipisicing elit. Dicta tempore qui inventore necessitatibus eligendi quae sapiente illo sequi, commodi aspernatur aperiam! Nostrum harum quae ducimus quam cumque amet delectus aliquam. Lorem ipsum dolor sit amet consectetur adipisicing elit. Similique debitis, eligendi incidunt officiis omnis porro quasi consectetur laborum perspiciatis atque, blanditiis reiciendis harum natus soluta! Recusandae eos laborum repellat molestiae? Lorem, ipsum dolor sit amet consectetur adipisicing elit. Quod, natus. Reprehenderit, nisi facere. Molestiae recusandae tempora dolores vitae, ipsa itaque facilis iste temporibus vel architecto sapiente aperiam laboriosam quaerat explicabo? Lorem ipsum dolor sit amet consectetur adipisicing elit. Odit veritatis aut tempore, magnam dolores recusandae minima error consequuntur eaque excepturi, quasi eligendi accusantium ipsam corrupti explicabo placeat quod quos asperiores.
Lorem ipsum dolor sit amet consectetur adipisicing elit. Dicta tempore qui inventore necessitatibus eligendi quae sapiente illo sequi, commodi aspernatur aperiam! Nostrum harum quae ducimus quam cumque amet delectus aliquam. Lorem ipsum dolor sit amet consectetur adipisicing elit. Similique debitis, eligendi incidunt officiis omnis porro quasi consectetur laborum perspiciatis atque, blanditiis reiciendis harum natus soluta! Recusandae eos laborum repellat molestiae? Lorem, ipsum dolor sit amet consectetur adipisicing elit. Quod, natus. Reprehenderit, nisi facere. Molestiae recusandae tempora dolores vitae, ipsa itaque facilis iste temporibus vel architecto sapiente aperiam laboriosam quaerat explicabo? Lorem ipsum dolor sit amet consectetur adipisicing elit. Odit veritatis aut tempore, magnam dolores recusandae minima error consequuntur eaque excepturi, quasi eligendi accusantium ipsam corrupti explicabo placeat quod quos asperiores.