Skip to content

Commit

Permalink
Allow subsections in markdown
Browse files Browse the repository at this point in the history
  • Loading branch information
gaborcsardi committed Sep 13, 2019
1 parent 40cb4a6 commit bd7276d
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 5 deletions.
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# roxygen2 (development version)

* Subheadings, i.e. headings of level 2 and above, are now allowed in
markdown text. They generate subsections within the roxygen tag
(within `@description`, `@details`, `@return`, etc.) `##` creates a
subsection, `###` a sub-subsection, etc. Headings can be nested (#907).

* `\link{foo}` links in external inherited documentation are automatically
transformed to `\link{package}{foo}` so that they work in the generated
documented (#635).
Expand Down
6 changes: 3 additions & 3 deletions R/markdown.R
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,10 @@ escape_comment <- function(x) {
}

mdxml_heading <- function(xml, state) {
if (state$tag$tag != "@includeRmd") {
return(mdxml_unsupported(xml, state$tag, "markdown headings"))
}
level <- xml_attr(xml, "level")
if (state$tag$tag != "@includeRmd" && level == 1) {
return(mdxml_unsupported(xml, state$tag, "level 1 markdown headings"))
}
head <- paste0(
mdxml_close_sections(state, level),
"\n",
Expand Down
84 changes: 82 additions & 2 deletions tests/testthat/test-markdown.R
Original file line number Diff line number Diff line change
Expand Up @@ -393,12 +393,92 @@ test_that("unhandled markdown generates warning", {
text <- "
#' Title
#'
#' ## Heading
#' > block quotes do not work,
#' > sadly
#'
#' Blabla
#' @md
#' @name x
NULL
"
expect_warning(roc_proc_text(rd_roclet(), text), "markdown headings")
expect_warning(roc_proc_text(rd_roclet(), text), "block quotes")
})

test_that("level 1 heading in markdown generates warning", {
text <- "
#' Title
#'
#' # This is not good
#'
#' Blabla
#' @md
#' @name x
NULL
"
expect_warning(
roc_proc_text(rd_roclet(), text),
"level 1 markdown headings"
)
})

test_that("level >2 markdown headings work in @description", {
text <- "
#' Title
#'
#' @description
#' ## This is good
#' yes
#'
#' @details
#' Blabla
#' @md
#' @name x
NULL
"
out <- roc_proc_text(rd_roclet(), text)[[1]]
expect_equal_strings(
out$fields$description$values,
"\\subsection{This is good}{\n\nyes\n}"
)
})

test_that("level >2 markdown headings work in @details", {
text <- "
#' Title
#'
#' Description.
#'
#' @details
#' ## Heading 2
#' ### Heading 3
#' Text.
#' @md
#' @name x
NULL
"
out <- roc_proc_text(rd_roclet(), text)[[1]]
expect_equal_strings(
out$fields$details$values,
"\\subsection{Heading 2}{\n\\subsection{Heading 3}{\n\nText.\n}\n\n}"
)
})

test_that("level >2 markdown headings work in @return", {
text <- "
#' Title
#'
#' Description.
#'
#' @return Even this
#' ## Can have a subsection.
#' Yes.
#' @md
#' @name x
NULL
"
out <- roc_proc_text(rd_roclet(), text)[[1]]
expect_equal_strings(
out$fields$value$values,
"Even this\n\\subsection{Can have a subsection.}{\n\nYes.\n}"
)
})

0 comments on commit bd7276d

Please sign in to comment.