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

Copy-Code button does not appear in articles if there is not at least one H2 and one H3 header #1959

Closed
francoisluc opened this issue Dec 8, 2021 · 6 comments · Fixed by #1998
Labels
bug an unexpected problem or unintended behavior front end 🌷 General HTML, CSS, and JS issues
Milestone

Comments

@francoisluc
Copy link

This Rmd will add a copy-code button

image

This Rmd won't add a copy-code button in articles

image

This is an odd behavior and I don't think this is expected. Thanks !

Here is my session info.

library(pkgdown)

Created on 2021-12-08 by the reprex package (v2.0.1)

Session info
sessioninfo::session_info()
#> - Session info ---------------------------------------------------------------
#>  setting  value
#>  version  R version 4.1.1 (2021-08-10)
#>  os       Windows 10 x64 (build 18363)
#>  system   x86_64, mingw32
#>  ui       RTerm
#>  language (EN)
#>  collate  French_Canada.1252
#>  ctype    French_Canada.1252
#>  tz       America/New_York
#>  date     2021-12-08
#>  pandoc   2.11.4 @ C:/Program Files/RStudio/bin/pandoc/ (via rmarkdown)
#> 
#> - Packages -------------------------------------------------------------------
#>  package     * version date (UTC) lib source
#>  backports     1.4.0   2021-11-23 [1] CRAN (R 4.1.1)
#>  cachem        1.0.6   2021-08-19 [1] CRAN (R 4.1.1)
#>  cli           3.1.0   2021-10-27 [1] CRAN (R 4.1.1)
#>  crayon        1.4.2   2021-10-29 [1] CRAN (R 4.1.1)
#>  digest        0.6.29  2021-12-01 [1] CRAN (R 4.1.2)
#>  ellipsis      0.3.2   2021-04-29 [1] CRAN (R 4.1.1)
#>  evaluate      0.14    2019-05-28 [1] CRAN (R 4.1.1)
#>  fansi         0.5.0   2021-05-25 [1] CRAN (R 4.1.1)
#>  fastmap       1.1.0   2021-01-25 [1] CRAN (R 4.1.1)
#>  fs            1.5.1   2021-11-30 [1] CRAN (R 4.1.2)
#>  glue          1.5.1   2021-11-30 [1] CRAN (R 4.1.2)
#>  highr         0.9     2021-04-16 [1] CRAN (R 4.1.1)
#>  htmltools     0.5.2   2021-08-25 [1] CRAN (R 4.1.1)
#>  knitr         1.36    2021-09-29 [1] CRAN (R 4.1.2)
#>  lifecycle     1.0.1   2021-09-24 [1] CRAN (R 4.1.1)
#>  magrittr      2.0.1   2020-11-17 [1] CRAN (R 4.1.1)
#>  memoise       2.0.1   2021-11-26 [1] CRAN (R 4.1.2)
#>  pillar        1.6.4   2021-10-18 [1] CRAN (R 4.1.1)
#>  pkgconfig     2.0.3   2019-09-22 [1] CRAN (R 4.1.1)
#>  pkgdown     * 2.0.1   2021-12-03 [1] CRAN (R 4.1.1)
#>  purrr         0.3.4   2020-04-17 [1] CRAN (R 4.1.1)
#>  R.cache       0.15.0  2021-04-30 [1] CRAN (R 4.1.1)
#>  R.methodsS3   1.8.1   2020-08-26 [1] CRAN (R 4.1.1)
#>  R.oo          1.24.0  2020-08-26 [1] CRAN (R 4.1.1)
#>  R.utils       2.11.0  2021-09-26 [1] CRAN (R 4.1.1)
#>  reprex        2.0.1   2021-08-05 [1] CRAN (R 4.1.1)
#>  rlang         0.4.12  2021-10-18 [1] CRAN (R 4.1.1)
#>  rmarkdown     2.11    2021-09-14 [1] CRAN (R 4.1.1)
#>  rstudioapi    0.13    2020-11-12 [1] CRAN (R 4.1.1)
#>  sessioninfo   1.2.2   2021-12-06 [1] CRAN (R 4.1.1)
#>  stringi       1.7.6   2021-11-29 [1] CRAN (R 4.1.2)
#>  stringr       1.4.0   2019-02-10 [1] CRAN (R 4.1.1)
#>  styler        1.6.2   2021-09-23 [1] CRAN (R 4.1.2)
#>  tibble        3.1.6   2021-11-07 [1] CRAN (R 4.1.1)
#>  utf8          1.2.2   2021-07-24 [1] CRAN (R 4.1.1)
#>  vctrs         0.3.8   2021-04-29 [1] CRAN (R 4.1.1)
#>  withr         2.4.3   2021-11-30 [1] CRAN (R 4.1.2)
#>  xfun          0.28    2021-11-04 [1] CRAN (R 4.1.2)
#>  yaml          2.2.1   2020-02-01 [1] CRAN (R 4.1.1)
#> 
#>  [1] C:/Program Files/R/R-4.1.1/library
#> 
#> ------------------------------------------------------------------------------
@maelle
Copy link
Collaborator

maelle commented Dec 8, 2021

I can reproduce this.

Now if I remove everything that has to do with the table of contents on that page (all the JS i.e. I manually delete the script from the head in docs/articles/test.html + the lines in pkgdown.js) then things work. Another weird edge case that might make #1890 look a tad more useful 😅

@maelle
Copy link
Collaborator

maelle commented Dec 8, 2021

The problem does not appear with BS3, with BS3 the TOC is added even when there is one h2 only.

@francoisluc
Copy link
Author

ok thanks Maëlle. Currently I'm just making sure to have a H2 and H3 header as a work around. I'm happy to see that you already have a PR that would fix that.

@gadenbuie
Copy link
Contributor

gadenbuie commented Dec 9, 2021

@maelle I think the issue is ultimately caused by calling scrollspy (below) when the #toc element doesn't exist

$('body').scrollspy({
target: '#toc',
offset: 56 // headroom height
});

It seems like, in this case, scrollspy does it's own thing and tries to apply to the <nav> element, where it breaks because the nav links aren't set up as expected.

The fix might be as easy as avoiding calling scrollspy when the #toc element doesn't exist:

    if ($('#toc').length) {  
      $('body').scrollspy({
        target: '#toc',
        offset: 56 // headroom height
      });
    }

@maelle
Copy link
Collaborator

maelle commented Dec 9, 2021

This sounds smart 😃

@maelle
Copy link
Collaborator

maelle commented Dec 10, 2021

@gadenbuie btw do you want to make a PR? 🙂 (I don't want to steal your fix 😉 )

@hadley hadley added bug an unexpected problem or unintended behavior front end 🌷 General HTML, CSS, and JS issues labels Jan 6, 2022
@hadley hadley added this to the v2.0.2 milestone Jan 6, 2022
hadley pushed a commit that referenced this issue Jan 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior front end 🌷 General HTML, CSS, and JS issues
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants