Skip to content

Commit

Permalink
add end_of_block check for kramdown tags
Browse files Browse the repository at this point in the history
I will forever be grateful when we move away from this wretched markdown
engine. For reference, I wasted an hour trying to track down a non-bug
because of this: swcarpentry/r-novice-inflammation#497
  • Loading branch information
zkamvar committed Jan 27, 2021
1 parent a30cf06 commit 02e8798
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions R/kramdown_tags.R
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,17 @@ set_ktag_block <- function(tags) {
)
)

# Sometimes, we just have to check to make sure the tag is really at the end
# of the block. In this case, we already know that the parent is a blockquote
# and we know that the parents and tags are blanaced, so here we want to
# just confirm that they are the end of the block.
balanced <- length(parents) == length(are_tags)
if (balanced) {
end_of_block <- get_lineend(tags) == get_lineend(parents)
} else {
end_of_block <- FALSE
}

# exclude the first tag if it's after a code block
# In Rmarkdown documents, all code blocks are tagged with a language attribute
#
Expand All @@ -108,7 +119,7 @@ set_ktag_block <- function(tags) {
}


if (after_code || is_language || is_output) {
if (!all(end_of_block) && (after_code || is_language || is_output)) {
ctag <- children[[are_tags[1]]]
are_tags <- are_tags[-1]
if (after_code || is_language) {
Expand All @@ -124,7 +135,7 @@ set_ktag_block <- function(tags) {
#
# When this happens, we need to find the nested block quote and
# get its parents
if (length(parents) < length(are_tags) && length(parents) == 1) {
if (!balanced && length(parents) < length(are_tags) && length(parents) == 1) {
blq <- glue::glue(".//{ns}:block_quote[not(@ktag)]/*")
if (xml2::xml_find_lgl(parents, glue::glue("boolean({blq})"))) {
parents <- xml2::xml_parents(
Expand Down Expand Up @@ -167,7 +178,7 @@ set_ktag_block <- function(tags) {
# Grab the correct parent from the list
the_parent <- parents[tag]
this_tag <- xml2::xml_text(children[are_tags[tag]])
xml2::xml_attr(the_parent, "ktag") <-this_tag
xml2::xml_attr(the_parent, "ktag") <- this_tag

}
xml2::xml_remove(children[are_tags])
Expand Down

0 comments on commit 02e8798

Please sign in to comment.