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

Work with absolute paths #41

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Imports:
digest,
evaluate,
fansi,
fs,
rlang,
vctrs,
yaml
Expand Down
11 changes: 7 additions & 4 deletions R/downlit-md.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ downlit_md_path <- function(in_path, out_path, format = NULL) {
ast_path <- tempfile()
on.exit(unlink(ast_path))

in_path <- fs::path_abs(in_path)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason not to use normalizePath() here? I'd prefer to keep the dependencies lighter.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The file must exist, this is generally not true for out_path:

normalizePath("bogus")
#> Warning in normalizePath("bogus"): path[1]="bogus": No such file or directory
#> [1] "bogus"

Created on 2020-09-03 by the reprex package (v0.3.0)

Same with mustWork = FALSE sans warning.

We can work around if you think it's worth it, or copy code from fs.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the other option is to require/document that the user supply absolute paths? That might be easier given that I expect relatively few people to be calling this function.

out_path <- fs::path_abs(out_path)

md2ast(in_path, ast_path, format = format)

ast <- jsonlite::read_json(ast_path)
Expand Down Expand Up @@ -58,8 +61,8 @@ downlit_md_string <- function(x, format = NULL) {
md2ast <- function(path, out_path, format = NULL) {
format <- format %||% md_format()
rmarkdown::pandoc_convert(
input = normalizePath(path, mustWork = FALSE),
output = normalizePath(out_path, mustWork = FALSE),
input = path,
output = out_path,
from = format,
to = "json"
)
Expand All @@ -75,8 +78,8 @@ ast2md <- function(path, out_path, format = NULL) {
)

rmarkdown::pandoc_convert(
input = normalizePath(path, mustWork = FALSE),
output = normalizePath(out_path, mustWork = FALSE),
input = path,
output = out_path,
from = "json",
to = format,
options = options
Expand Down