Using R to Automate Markdown Code
I recently learned that Google requires affiliate links to be “nofollow” links. Compared to the often default “follow” links, “nofollow” links essentially remove the website endorsement typically implied by websites linking to each other. For example, if I link to a free online book (as I often do), I am signaling to Google that I trust that website in terms of being malware-free and having helpful content. However, companies should not be able to pay for this type of linking through affiliate programs, as it sends biased signals to Google, artificially inflating how many of these positive signals a company receives. My website host Squarespace, despite its many benefits, does not allow me to adjust how links are treated in a drop-down menu. I was left with the tedious task of switching all of the pages I have affiliate links on to Markdown text, so I could have all of my affiliate links with the proper “nofollow” tag. After writing markdown code for two links, I realized that I should automate this process because it would have taken too much time to keep typing the code out and checking the unfamiliar syntax.
I wrote two nearly identical functions, “DoFollow” and “NoFollow”, as the names imply “DoFollow” are for websites that should receive my endorsement, such as a high quality online book. The “NoFollow” is for affiliate links that as I discussed should not be given that endorsement in the eyes of search engines. The functions each have two inputs the actual link URL, and the text to be displayed to the reader. In the functions’ first line the code checks if display text is provided, if not the display text will simply be the link provided. This is my personal preference, I like to show my website visitors the exact URL they are clicking on, instead of hiding it. The second line makes sure the link provided starts with the standard “http”, because in my experience without that explicitly present the links tried to connect to non-existent sub-pages on my website. The magic here is the “cat” and “paste” functions, the cat function simply cleans the output and makes the markdown code display properly. The paste function does the heavy lifting by inserting the links and display text in the correct places, where it is surrounded by the repeating markdown code. With the cat function being used, I simply had to surround the chunks of text with single quotes, and I was able to input the double quotes needed in the markdown code. The functions I created are below. I am sure they can be modified for more complex tasks as needed. Before the code, I am also including example “dofollow” and “nofollow” code output. The major sections are “href” which is the actual URL being linked to, “rel” which allows you to specify that the link should be “nofollow”, “target” which tells the link to open on a new page, and the text in between the >< which is the text displayed to the website visitor.
Do Follow: <a href="https://statswithr.com" target="_blank">statswithr.com</a>
No Follow: <a rel="nofollow" href="https://statswithr.com" target="_blank">statswithr.com</a>
DoFollow <- function(Link, Display_Text = NA){
# If specific text is not given, the link will be displayed
if(is.na(Display_Text) == TRUE){Display_Text = Link}
# If the link does not start with "http", this adds that information
if(substr(Link,1,4) != "http"){Link = paste("https://",Link,sep = "")}
return(
cat(
# The link, a command to make it open in a new page, and the text to be displayed
paste('<a href="',Link,'" target="_blank">',Display_Text,'</a>',sep = ""
)
)
)
}
NoFollow <- function(Link, Display_Text = NA){
# If specific text is not given, the link will be displayed
if(is.na(Display_Text) == TRUE){Display_Text = Link}
# If the link does not start with "http", this adds that information
if(substr(Link,1,4) != "http"){Link = paste("https://",Link,sep = "")}
return(
cat(
# The "nofollow" command, the link, new page command, and the text to be displayed
paste('<a rel="nofollow" href="',Link,'" target="_blank">',Display_Text,'</a>',sep = ""
)
)
)
}