fix_sidebar_href: Fix the refs for a vector of sidebar nodes

View source: R/utils-sidebar.R

fix_sidebar_hrefR Documentation

Fix the refs for a vector of sidebar nodes


update links from a list of HTML node


  path = NULL,
  scheme = NULL,
  server = NULL,
  query = NULL,
  fragment = NULL


append(first, sep = "#", last, trim = TRUE)



a text representation of HTML nodes that contain ⁠<a>⁠ elements.

path, scheme, server, query, fragment

character vectors of elements to replace. This can be a single element vector, which will be recycled or a vector with the same length as item.


a data frame produced via xml2::url_parse


a character vector


a character vector of length 1


a character vector, same length as first or length 1


a logical indicating if the leading and trailing sep should be trimmed from first and last.


Repeat after me: parsing HTML with regular expressions is bad. This function uses xml2::read_html() to parse incoming HTML content to convert the HTML string into an XML document where we can extract all of the anchor links, parse them and replace their contents without regex. This is acheived via xml2::url_parse() separating the URL into pieces and updating those pieces for each node.

fix_sidebar_href() is useful because The sidebar nodes needs to be updated for the 404 page so that all links use the published URL. NOTE: this does not take into account port or user.

The auxilary functions make_url(), append() and prepend() are used to convert the output of xml2::url_parse() back to a URL.


the text representation of HTML nodes with the href element modified.


my_links <- c(
  "<div id='one'><div id='one-one'><a href='index.html'>Index</a></div></div>",
  "<div id='two'><div id='two-two'><a href='two.html'><em>Two</em></a></div></div>",
  "<div id='three'><div id='three-three'><a href='three.html'>Three</a></div></div>",
  "<div id='four'><div id='four-four'><a href='four.html'>Four</a></div></div>",
  "<div id='five'><div id='five-five'><a href='five.html'>Five</a></div></div>"

snd <- asNamespace("sandpaper")
# Prepend a server to the links
snd$fix_sidebar_href(my_links, scheme = "https", server = "")
snd$fix_sidebar_href(my_links, server = "")

# Add an anchor to the links
snd$fix_sidebar_href(my_links, scheme = "https", fragment = "anchor")

# NOTE: this will _always_ return a character vector, even if the input is
# incorrect
snd$fix_sidebar_href(list(), server = "")

zkamvar/sandpaper documentation built on May 11, 2024, 7:26 a.m.