distill icon indicating copy to clipboard operation
distill copied to clipboard

Getting rid of `// get rid of index.html references in header`

Open matthiasgomolka opened this issue 3 years ago • 5 comments

First of all I want to thank you for {distill}! I know almost nothing about html and it allows me to build useful ans pretty websites without any hassle.

What I'm doing

I'm building a small website using {distill}. One thing which is special about it is the fact that it's not meant to run in the browser, but as an https://nwjs.io/ app.

This is pretty straight-forward. I only need to copy a small package.jsonfile in to the _site directory created by {distill}. Then, I can launch the website as a local app. This works fine.

However, there is one small drawback:

The html files created from the Rmd files contain a small section with the comment // get rid of index.html references in header:

// get rid of index.html references in header
if (window.location.protocol !== "file:") {
  $('.distill-site-header a[href]').each(function(i,val) {
    $(this).attr('href', $(this).attr('href').replace("index.html", "./"));
  });
}

This section replaces all links to index.html in the top bar by ./, when I open the website as a local nw.js app. This breaks those links and the respective page cannot be found.

My hack to solve the problem

For now, my dirty workaround is this:

lapply(
  fs::dir_ls("_site", glob = "*.html"),
  function(html) {
    content <- paste(readLines(html), collapse = "\r\n")
    writeLines(
      gsub(
        '\\/\\/ get rid of index\\.html references in header(.|\\r|\\n)+?(?=\\s+\\/\\/)',
        '',
        content,
        perl = TRUE
      ),
      con = html
    )
  }
)

This simply deletes the mentioned lines of code from all html files in the _site directory.

My questions

  1. Is there a simpler / better way to do this?
  2. Which functionality might be affected by my hack?

matthiasgomolka avatar May 13 '22 15:05 matthiasgomolka

Hi !

Which version of distill are you using ?

I think I may have solve this old issue just this week (https://github.com/rstudio/distill/issues/91) and the version release yesterday on CRAN may fix your issue. It is 1.4.

But maybe the fix is for the issue #91 is not enough for your usage.

Can you try it ?

cderv avatar May 13 '22 20:05 cderv

I was indeed still on version 1.3. However, updating to 1.4 did not help.

Sorry for not providing the seesionInfo() right away:

> sessionInfo()
R version 4.0.2 (2020-06-22)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19042)

Matrix products: default

locale:
[1] LC_COLLATE=German_Germany.1252  LC_CTYPE=German_Germany.1252    LC_MONETARY=German_Germany.1252
[4] LC_NUMERIC=C                    LC_TIME=German_Germany.1252    

attached base packages:
[1] stats     graphics  grDevices datasets  utils     methods   base     

loaded via a namespace (and not attached):
 [1] rstudioapi_0.13 knitr_1.38      xml2_1.3.3      magrittr_2.0.3  downlit_0.4.0   R6_2.5.1       
 [7] rlang_1.0.2     fastmap_1.1.0   fansi_1.0.3     stringr_1.4.0   tools_4.0.2     xfun_0.30      
[13] utf8_1.2.2      cli_3.2.0       jquerylib_0.1.4 ellipsis_0.3.2  htmltools_0.5.2 yaml_2.3.5     
[19] digest_0.6.29   lifecycle_1.0.1 tibble_3.1.6    crayon_1.5.1    bookdown_0.26   sass_0.4.1     
[25] vctrs_0.4.1     fs_1.5.2        distill_1.4     glue_1.6.2      memoise_2.0.1   cachem_1.0.6   
[31] evaluate_0.15   rmarkdown_2.13  stringi_1.7.6   pillar_1.7.0    compiler_4.0.2  bslib_0.3.1    
[37] jsonlite_1.8.0  renv_0.15.4     pkgconfig_2.0.3

matthiasgomolka avatar May 16 '22 07:05 matthiasgomolka

The html files created from the Rmd files contain a small section with the comment // get rid of index.html references in header:

Can you check that this section in your HTML rendered using 1.4 has changed ? Specifically the regex should be difference that just index.html

This section replaces all links to index.html in the top bar by ./, when I open the website as a local nw.js app. This breaks those links and the respective page cannot be found.

Is there a specific behavior of nwjs.io that could help me understand what to change ?

I think we currently need this trick in JS for some purpose with the RStudio IDE. I'll try to find the original reason.

If possible, can you help me build a reproductible example with nwjs.io and using for example the demo distill template ? we could use a specific github repo as example for that. That would help me reproduce on my side, and do some test.

Thanks !

cderv avatar May 16 '22 08:05 cderv

Sorry for the deleay!

Updating to 1.4 did not help.

Unfortunately, I'm not an expert of nw.js at all, so I can't direct you into any direction regarding nw.js.

I'll create a GitHub Repo in the next days for you to reproduce the problem.

matthiasgomolka avatar May 18 '22 06:05 matthiasgomolka

Here is the repo: https://github.com/matthiasgomolka/distill_462

Minimal instructions are available in https://github.com/matthiasgomolka/distill_462/blob/main/index.Rmd.

You can reproduce the problem when you start the app (as described in index.Rmd) und then click on the top left Button which should lead to index.html.

Hope this helps.

matthiasgomolka avatar May 20 '22 14:05 matthiasgomolka