{"action":"created","issue":{"url":"https://api.github.com/repos/rstudio/rmarkdown/issues/808","repository_url":"https://api.github.com/repos/rstudio/rmarkdown","labels_url":"https://api.github.com/repos/rstudio/rmarkdown/issues/808/labels{/name}","comments_url":"https://api.github.com/repos/rstudio/rmarkdown/issues/808/comments","events_url":"https://api.github.com/repos/rstudio/rmarkdown/issues/808/events","html_url":"https://github.com/rstudio/rmarkdown/issues/808","id":177655417,"number":808,"title":"Relative paths are rewritten to absolute paths","user":{"login":"wjhopper","id":9057226,"avatar_url":"https://avatars.githubusercontent.com/u/9057226?v=3","gravatar_id":"","url":"https://api.github.com/users/wjhopper","html_url":"https://github.com/wjhopper","followers_url":"https://api.github.com/users/wjhopper/followers","following_url":"https://api.github.com/users/wjhopper/following{/other_user}","gists_url":"https://api.github.com/users/wjhopper/gists{/gist_id}","starred_url":"https://api.github.com/users/wjhopper/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/wjhopper/subscriptions","organizations_url":"https://api.github.com/users/wjhopper/orgs","repos_url":"https://api.github.com/users/wjhopper/repos","events_url":"https://api.github.com/users/wjhopper/events{/privacy}","received_events_url":"https://api.github.com/users/wjhopper/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2016-09-18T14:38:37Z","updated_at":"2016-09-18T16:44:40Z","closed_at":null,"body":"I just discovered something unusual in how relative paths containing `../` are handled when rendering Rmarkdown documents. I also posted about this issue on [StackOverflow](http://stackoverflow.com/questions/39481092/how-to-stop-relative-paths-from-being-rewritten-to-absolute-paths-in-rmarkdown-d).\r\n\r\n Let's say I have a project with a structure as shown below:\r\n\r\n\r\n    ./\r\n    ├── files/\r\n    │   ├── some_image.jpg\r\n    │   └── test.Rmd\r\n    └── some_other_image.jpg\r\n\r\nAnd I want to render the Rmarkdown document `test.Rmd` into HTML containing both jpegs `some_image.jpg` and `some_other_image.jpg`, like so:\r\n\r\n    ---\r\n    title: \"Path Test\"\r\n    output:\r\n      html_fragment:\r\n        self_contained: false\r\n        keep_md: true\r\n    ---\r\n\r\n    Path without ../ is not altered\r\n    ![Unaltered](some_image.jpg)\r\n\r\n    Path with ../ is converted to an absolute path!\r\n    ![Altered](../some_other_image.jpg)\r\n\r\nIf I create the HTML document with `rmarkdown::render(\"files/test.Rmd\", clean = FALSE)`, it produces the following HTML:\r\n\r\n    <p>Path without ../ is not altered <img src=\"some_image.jpg\" alt=\"Unaltered\" /></p>\r\n    <p>Path with ../ is converted to an absolute path! <img src=\"/home/will/test_project/some_other_image.jpg\" alt=\"Altered\" /></p>\r\n\r\n\r\nIf you look at the `src` attribute for each image, you can see that **the image path that was specified using `../` gets rewritten to use an absolute path!!** Moreover, this conversions *only* happens when the specified file exists!\r\n\r\nMy questions: Why is this happening? This doesn't happen to me when manually using `pandoc` to convert this very same markdown to HTML? And if you inspect the intermediate markdown file produced, the path to `some_other_image.jpg` is still `../some_other_image.jpg`.\r\n\r\nAnd what can I do to stop it? It works fine on my particular machine of course, but this is a big problem if the page is going to be deployed somewhere else.\r\n\r\nI'm running Rmarkdown version 1.0 with R version 3.3.1 on debian testing, in case that's relevant for reproducing."},"comment":{"url":"https://api.github.com/repos/rstudio/rmarkdown/issues/comments/247858651","html_url":"https://github.com/rstudio/rmarkdown/issues/808#issuecomment-247858651","issue_url":"https://api.github.com/repos/rstudio/rmarkdown/issues/808","id":247858651,"user":{"login":"wjhopper","id":9057226,"avatar_url":"https://avatars.githubusercontent.com/u/9057226?v=3","gravatar_id":"","url":"https://api.github.com/users/wjhopper","html_url":"https://github.com/wjhopper","followers_url":"https://api.github.com/users/wjhopper/followers","following_url":"https://api.github.com/users/wjhopper/following{/other_user}","gists_url":"https://api.github.com/users/wjhopper/gists{/gist_id}","starred_url":"https://api.github.com/users/wjhopper/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/wjhopper/subscriptions","organizations_url":"https://api.github.com/users/wjhopper/orgs","repos_url":"https://api.github.com/users/wjhopper/repos","events_url":"https://api.github.com/users/wjhopper/events{/privacy}","received_events_url":"https://api.github.com/users/wjhopper/received_events","type":"User","site_admin":false},"created_at":"2016-09-18T16:44:40Z","updated_at":"2016-09-18T16:44:40Z","body":"I dug into this  a bit more, and it appears the cause is the post-processing of the pandoc output. The path rewritting happens as a result of lines 155-167 in `html_document_base.R`\r\n\r\n```r\r\n    } else if (!self_contained) {\r\n      # if we're not self-contained, find absolute references to the output\r\n      # directory and replace them with relative ones\r\n      image_relative <- function(img_src, src) {\r\n        in_file <- utils::URLdecode(src)\r\n        if (length(in_file) && file.exists(in_file)) {\r\n          img_src <- sub(\r\n            src, utils::URLencode(normalized_relative_to(output_dir, in_file)),\r\n            img_src, fixed = TRUE)\r\n        }\r\n        img_src\r\n      }\r\n      output_str <- process_images(output_str, image_relative)\r\n    }\r\n```\r\n\r\nThe call to the function `normalized_relative_to` ends up rewriting the relative path to be an absolute one. I don't know what all the consequences of *not* calling this function here might be, but maybe one easy fix would to be allowing users to disable the post-processing of the pandoc output?"}}
{"actor":{"display_login":"wjhopper"}}