carpentries / workbench

Repository for Discussions and Materials about The Carpentries Workbench
https://carpentries.github.io/workbench/
Creative Commons Attribution 4.0 International
17 stars 7 forks source link

Link to sub-sections of setup instructions not being processed #70

Closed tobyhodges closed 1 year ago

tobyhodges commented 1 year ago

As discussed in https://github.com/carpentries-incubator/geospatial-python/pull/162, internal links to a specific heading within learners/setup.md currently resolve to index.html#setup i.e. the top of the setup instructions.

It would be nice to be able to link to specific sub-sections of the setup instructions, when an anchor is provided with the link to setup.md.

zkamvar commented 1 year ago

I was a bit confused by what this meant, but I think @fnattino gave a good description in https://github.com/carpentries/sandpaper-docs/issues/164, and it's definitely a bug (you would, as the author, expect to be able to link to a subsection of the setup)

I believe this is coming from fix_setup_link() in R/utils-xml.R

The function replaces any occurance of setup.html with index.html#setup, but neglects the fragment (everything after the #). It could be modified like so:

 fix_setup_link <- function(nodes = NULL) {
   if (length(nodes) == 0) return(nodes)
   links <- xml2::xml_find_all(nodes, ".//a")
   hrefs <- xml2::url_parse(xml2::xml_attr(links, "href"))
   setup_links <- hrefs$scheme == "" &
     hrefs$server == "" &
     hrefs$path == "setup.html"
+  section_ids <- hrefs$fragment[setup_links] 
+  section_ids[section_ids == ""] <- "setup"
+  replacement <- paste0("index.html#", section_ids)
+  xml2::xml_set_attr(links[setup_links], "href", replacement)
-  xml2::xml_set_attr(links[setup_links], "href", "index.html#setup")
   invisible(nodes)
}
zkamvar commented 1 year ago

This has been fixed in https://github.com/carpentries/sandpaper/pull/522 and will be available later today