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

expand repo links #1447

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ BugReports: https://github.com/rstudio/bookdown/issues
SystemRequirements: Pandoc (>= 1.17.2)
RoxygenNote: 7.2.3
Encoding: UTF-8
Config/Needs/book: remotes, webshot
Config/Needs/book: remotes, webshot, svglite
Config/Needs/website: pkgdown, tidyverse/tidytemplate, rstudio/quillt
Config/testthat/edition: 3
VignetteBuilder: knitr
30 changes: 26 additions & 4 deletions R/bs4_book.R
Original file line number Diff line number Diff line change
Expand Up @@ -419,16 +419,37 @@ tweak_navbar <- function(html, toc, active = "", rmd_index = NULL, repo = NULL)
repo$subdir <- paste0(repo$subdir, "/")
}

repo_edit <- paste0(repo$base, "/edit/", repo$branch, "/", repo$subdir, rmd_index[[active]])
repo_view <- paste0(repo$base, "/blob/", repo$branch, "/", repo$subdir, rmd_index[[active]])
if(grepl("dev.azure", repo$base)) {
repo_edit <- paste0(repo$base, "?path=/", rmd_index[[active]], "&version=GB",
repo$branch)
repo_view <- repo_edit
} else if(grepl("bitbucket", repo$base)){
repo_edit <- paste0(repo$base, "/src/", repo$branch, "/", repo$subdir, rmd_index[[active]],
"?mode=edit&at=", repo$branch)
repo_view <- paste0(repo$base, "/src/", repo$branch, "/", repo$subdir, rmd_index[[active]])
} else {
repo_edit <- paste0(repo$base, "/edit/", repo$branch, "/", repo$subdir, rmd_index[[active]])
repo_view <- paste0(repo$base, "/blob/", repo$branch, "/", repo$subdir, rmd_index[[active]])
}

} else {
repo_edit <- NULL
repo_view <- NULL
}

if (!is.null(repo$base)) {
icon <- repo$icon %n%
ifelse(grepl("github\\.com", repo$base), "fab fa-github", "fab fa-gitlab")
icon <- repo$icon %n% {
if(grepl("github\\.com", repo$base)) {
"fab fa-github"
} else if (grepl("dev.azure", repo$base)) {
"fab fa-microsoft"
} else if (grepl("bitbucket", repo$base)) {
"fab fa-bitbucket"
} else {
"fab fa-gitlab"
}
}

template_link_icon(html, ".//a[@id='book-repo']", icon)
template_link_icon(html, ".//a[@id='book-source']", icon)
template_link_icon(html, ".//a[@id='book-edit']", icon)
Expand Down Expand Up @@ -547,6 +568,7 @@ template_link <- function(html, xpath, href) {
xml2::xml_remove(xml2::xml_parent(node))
} else {
xml2::xml_attr(node, "href") <- href
xml2::xml_attr(node, "target") <- "_blank"
}
}

Expand Down
16 changes: 10 additions & 6 deletions man/publish_book.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

66 changes: 66 additions & 0 deletions tests/testthat/test-bs4_book.R
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,36 @@ test_that("bs4_book() repo specification works - GitLab", {
)
})

test_that("bs4_book() repo specification works - Azure Devops", {
skip_if_bs4_book_deps_missing()
book <- local_bs4_book(
output_options = list(
repo = "https://dev.azure.com/elenasaletter/_git/test_book"
)
)
html <- xml2::read_html(file.path(book, "_book", "index.html"))

expect_equal(
xml2::xml_attr(xml2::xml_child(xml2::xml_find_first(html, "//a[@id='book-repo']")), "class"),
"fab fa-microsoft"
)
})

test_that("bs4_book() repo specification works - Bitbucket", {
skip_if_bs4_book_deps_missing()
book <- local_bs4_book(
output_options = list(
repo = "https://bitbucket.org/test-book/test_book/src/master/"
)
)
html <- xml2::read_html(file.path(book, "_book", "index.html"))

expect_equal(
xml2::xml_attr(xml2::xml_child(xml2::xml_find_first(html, "//a[@id='book-repo']")), "class"),
"fab fa-bitbucket"
)
})

test_that("bs4_book() repo specification works - custom icon", {
skip_if_bs4_book_deps_missing()
book <- local_bs4_book(
Expand Down Expand Up @@ -118,6 +148,42 @@ test_that("bs4_book() repo specification works - custom icon GitHub", {
)
})

test_that("bs4_book() repo specification works - custom icon - Azure Devops", {
skip_if_bs4_book_deps_missing()
book <- local_bs4_book(
output_options = list(
repo = list(
base = "https://dev.azure.com/elenasaletter/_git/test_book",
icon = "fas fa-air-freshener"
)
)
)
html <- xml2::read_html(file.path(book, "_book", "index.html"))

expect_equal(
xml2::xml_attr(xml2::xml_child(xml2::xml_find_first(html, "//a[@id='book-repo']")), "class"),
"fas fa-air-freshener"
)
})

test_that("bs4_book() repo specification works - custom icon - Bitbucket", {
skip_if_bs4_book_deps_missing()
book <- local_bs4_book(
output_options = list(
repo = list(
base = "https://bitbucket.org/test-book/test_book/src/master/",
icon = "fas fa-air-freshener"
)
)
)
html <- xml2::read_html(file.path(book, "_book", "index.html"))

expect_equal(
xml2::xml_attr(xml2::xml_child(xml2::xml_find_first(html, "//a[@id='book-repo']")), "class"),
"fas fa-air-freshener"
)
})

test_that("bs4_book() metadata tweaking works -- index", {
skip_if_bs4_book_deps_missing()
book <- local_bs4_book(description = "A very nice book.", url = 'https://example.com/')
Expand Down
Loading