Skip to content

Commit

Permalink
fix yihui/knitr#2254: use PCRE in embed_resources() in case there are…
Browse files Browse the repository at this point in the history
… emojis in text
  • Loading branch information
yihui committed May 2, 2023
1 parent 6728493 commit a5b42c2
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 8 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: markdown
Type: Package
Title: Render Markdown with 'commonmark'
Version: 1.6.2
Version: 1.6.3
Authors@R: c(
person("Yihui", "Xie", role = c("aut", "cre"), email = "[email protected]", comment = c(ORCID = "0000-0003-0645-5666")),
person("JJ", "Allaire", role = "aut"),
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

- The `file` argument of `mark()` will be treated as a file path only if the file exists and the value is not wrapped in `I()`. Previously, it would be treated as a file path when it has a file extension, which could lead to confusing errors like #100 (thanks, @LukasWallrich).

- When there are emojis in the text, `mark()` may fail to identify and embed web resources (thanks, @tdhock, yihui/knitr#2254).

# CHANGES IN markdown VERSION 1.6

- Added support for footnotes, fenced `Div`s, section numbers, `{}` attributes for images/headings/fenced `Div`s, and appendices. See `vignette('intro', package = 'markdown')` for details.
Expand Down
4 changes: 2 additions & 2 deletions R/render.R
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ mark = function(
# discard other types of raw content blocks
x[!(i1 | i2)] = ''
x
})
}, perl = FALSE)
# commonmark doesn't support ```{.class}, which should be treated as ```class
ret = gsub('(<pre><code class="language-)\\{[.]([^}]+)}(">)', '\\1\\2\\3', ret)
# auto identifiers
Expand Down Expand Up @@ -249,7 +249,7 @@ mark = function(
# TODO: support code highlighting for latex (listings or highr::hi_latex)
x = gsub(r4, '\\1\\3\\4', x)
x
})
}, perl = FALSE)
# fix horizontal rules from --- (\linethickness doesn't work)
ret = gsub('{\\linethickness}', '{1pt}', ret, fixed = TRUE)
ret = redefine_level(ret, options[['top_level']])
Expand Down
10 changes: 5 additions & 5 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ id_string = function(text, lens = c(2:10, 20), times = 20) {
}

# a shorthand for gregexpr() and regmatches()
match_replace = function(x, pattern, replace = identity, ...) {
m = gregexpr(pattern, x, ...)
match_replace = function(x, pattern, replace = identity, ..., perl = TRUE) {
m = gregexpr(pattern, x, ..., perl = perl)
regmatches(x, m) = lapply(regmatches(x, m), function(z) {
if (length(z)) replace(z) else z
})
Expand Down Expand Up @@ -478,7 +478,7 @@ render_footnotes = function(x) {
f1 <<- c(f1, sub(r, '\\2', z))
f2 <<- c(f2, sub(r, '\\3', z))
gsub(r, '\\1', z)
})
}, perl = FALSE)
for (i in seq_along(f1)) {
x = sub(f1[i], sprintf('\\footnote{%s}', f2[i]), x, fixed = TRUE)
}
Expand All @@ -488,7 +488,7 @@ render_footnotes = function(x) {
# add auto identifiers to headings
auto_identifier = function(x) {
r = '<(h[1-6])([^>]*)>(.+?)</\\1>'
match_replace(x, r, perl = TRUE, function(z) {
match_replace(x, r, function(z) {
z1 = sub(r, '\\1', z) # tag
z2 = sub(r, '\\2', z) # attrs
z3 = sub(r, '\\3', z) # content
Expand Down Expand Up @@ -612,7 +612,7 @@ embed_resources = function(x, embed = 'local') {
x = if (length(grep('</body>', x)) != 1) {
one_string(I(c(x, x2)))
} else {
match_replace(x, '</body>', fixed = TRUE, function(z) {
match_replace(x, '</body>', fixed = TRUE, perl = FALSE, function(z) {
one_string(I(c(x2, z)))
})
}
Expand Down

0 comments on commit a5b42c2

Please sign in to comment.