Skip to content

Commit

Permalink
Merge pull request #610 from renkun-ken/vscode-notebook-cell
Browse files Browse the repository at this point in the history
Handle vscode-notebook-cell in `path_from_uri`
  • Loading branch information
renkun-ken authored Apr 20, 2023
2 parents adc2189 + a78137c commit 4752951
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,24 @@ path_from_uri <- function(uri) {
if (length(uri) == 0) {
return(character())
}
if (!startsWith(uri, "file:///")) {

if (startsWith(uri, "file:///")) {
start_char <- if (.Platform$OS.type == "windows") 9 else 8
path <- substr(uri, start_char, nchar(uri))
} else if (startsWith(uri, "vscode-notebook-cell://")) {
# Windows: vscode-notebook-cell://C:/Users/Username/Documents/Notebooks/MyNotebook.ipynb#MyCellId
# Unix: vscode-notebook-cell:///home/username/Documents/Notebooks/MyNotebook.ipynb#MyCellId
# WSL: vscode-notebook-cell://wsl+ubuntu-20.04/home/username/Documents/Notebooks/MyNotebook.ipynb#MyCellId
path <- sub("^vscode-notebook-cell://(.+)#.*$", "\\1", uri)
if (.Platform$OS.type != "windows" && !startsWith(path, "/")) {
path <- sub("^[^/]+(/.+)$", "\\1", path)
}
} else {
return("")
}

# URLdecode gives unknown encoding, we need to mark them as UTF-8
start_char <- if (.Platform$OS.type == "windows") 9 else 8
path <- utils::URLdecode(substr(uri, start_char, nchar(uri)))
path <- utils::URLdecode(path)
Encoding(path) <- "UTF-8"
path
}
Expand Down

0 comments on commit 4752951

Please sign in to comment.