-
Notifications
You must be signed in to change notification settings - Fork 3
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
remove empty text nodes #60
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you! 🕵️
@@ -46,6 +46,7 @@ to_md <- function(yaml_xml_list, path = NULL, stylesheet_path = stylesheet()){ | |||
stylesheet <- read_stylesheet(stylesheet_path) | |||
|
|||
transform_code_blocks(body) | |||
remove_phantom_text(body) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👻
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
keeping with the season 🎃
@@ -65,6 +66,22 @@ transform_to_md <- function(body, yaml, stylesheet) { | |||
c(yaml, body) | |||
} | |||
|
|||
# remove phantom text nodes that occur before links, images, and asis nodes that |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the comments are so clear ✨
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🙏
to_sever <- xml2::xml_find_all(body, | ||
".//md:text[string-length(text())=0]", ns = md_ns()) | ||
if (length(to_sever)) { | ||
xml2::xml_remove(to_sever) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it'd be fine to remove the if
library("xml2")
xml <- read_xml("<foo><bar /></foo>")
nodes <- xml2::xml_find_all(xml, ".//fooo")
xml2::xml_remove(nodes)
Created on 2021-10-19 by the reprex package (v2.0.0)
Feel free to disagree, of course.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The difference is minuscule, but there is a slight overhead for removing nodes that may or may not exist, so I'm going to leave it.
library("xml2")
xml <- read_xml("<foo><bar /></foo>")
nodes <- xml2::xml_find_all(xml, ".//fooo")
rm <- function(nodes) { xml2::xml_remove(nodes); return(NULL) }
chk <- function(nodes) { if (length(nodes)) xml2::xml_remove(nodes); return(NULL) }
bench::mark(rm(nodes), chk(nodes), iterations = 1e5)
#> # A tibble: 2 × 6
#> expression min median `itr/sec` mem_alloc `gc/sec`
#> <bch:expr> <bch:tm> <bch:tm> <dbl> <bch:byt> <dbl>
#> 1 rm(nodes) 1.94µs 2.25µs 372373. 6.21KB 22.3
#> 2 chk(nodes) 599.89ns 696.05ns 1194081. 0B 11.9
Created on 2021-10-19 by the reprex package (v2.0.1)
This removes empty text nodes that occur before bare links and asis nodes. Two examples below show asis nodes and bare links, respectively from #58
Asis nodes (math)
this PR removes the empty text node before the “asis” node
Created on 2021-10-18 by the reprex package (v2.0.1)
Bare Links
this PR removes the empty text node before the “link” node
Created on 2021-10-18 by the reprex package (v2.0.1)
This will fix #58