Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display anchors on right-hand side of headings #1782

Closed
hadley opened this issue Sep 23, 2021 · 1 comment · Fixed by #1815
Closed

Display anchors on right-hand side of headings #1782

hadley opened this issue Sep 23, 2021 · 1 comment · Fixed by #1815
Labels
feature a feature request or enhancement front end 🌷 General HTML, CSS, and JS issues

Comments

@hadley
Copy link
Member

hadley commented Sep 23, 2021

So that you can also use them on mobile. Should copy what bs4_book() does.

After implementing can delete:

table a.anchor {
  position: absolute;
}

table a.anchor {
  position: absolute;
}
@hadley hadley added feature a feature request or enhancement front end 🌷 General HTML, CSS, and JS issues labels Sep 23, 2021
@hadley
Copy link
Member Author

hadley commented Oct 13, 2021

From bookdown:

tweak_anchors <- function(html) {
  main <- xml2::xml_find_first(html, ".//main")
  headings <- xml2::xml_find_all(main, "(.//h1|.//h2|.//h3|.//h4|.//h5|.//h6)")
  id <- xml2::xml_attr(xml2::xml_find_first(headings, "parent::div"), "id")

  headings <- headings[!is.na(id)]
  id <- id[!is.na(id)]

  anchor <- paste0(
    "<a class='anchor' aria-label='anchor' href='#", id, "'>",
    "<i class='fas fa-link'></i>",
    "</a>"
  )

  for (i in seq_along(id)) {
    xml2::xml_add_child(headings[[i]], xml2::read_xml(anchor[[i]]))
  }
}

And css

.anchor {
  font-size: max(0.5em, 1rem);
  margin-left: 0.5rem;
  display: none;
}
h1:hover .anchor,
h2:hover .anchor,
h3:hover .anchor,
h4:hover .anchor,
h5:hover .anchor,
h6:hover .anchor {
  display: inline;
}

hadley added a commit that referenced this issue Oct 13, 2021
* Consistently add anchors with `tweak_ancohrs()`
* Move anchor to right (fixes #1782)
@hadley hadley mentioned this issue Oct 13, 2021
hadley added a commit that referenced this issue Oct 14, 2021
* Consistently add anchors with `tweak_anchors()`
* Move anchor to right (fixes #1782)
* Ensure that section ids are unique
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature a feature request or enhancement front end 🌷 General HTML, CSS, and JS issues
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant