From a5b42c261f76d75c3894d462e88cef168e925541 Mon Sep 17 00:00:00 2001 From: Yihui Xie Date: Mon, 1 May 2023 20:40:05 -0500 Subject: [PATCH] fix yihui/knitr#2254: use PCRE in embed_resources() in case there are emojis in text --- DESCRIPTION | 2 +- NEWS.md | 2 ++ R/render.R | 4 ++-- R/utils.R | 10 +++++----- 4 files changed, 10 insertions(+), 8 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 745c0d7..493052a 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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 = "xie@yihui.name", comment = c(ORCID = "0000-0003-0645-5666")), person("JJ", "Allaire", role = "aut"), diff --git a/NEWS.md b/NEWS.md index 84c3506..c118078 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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. diff --git a/R/render.R b/R/render.R index fe9aaaa..97b8626 100644 --- a/R/render.R +++ b/R/render.R @@ -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('(
)', '\\1\\2\\3', ret)
     # auto identifiers
@@ -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']])
diff --git a/R/utils.R b/R/utils.R
index 90fd46c..ced0620 100644
--- a/R/utils.R
+++ b/R/utils.R
@@ -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
   })
@@ -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)
   }
@@ -488,7 +488,7 @@ render_footnotes = function(x) {
 # add auto identifiers to headings
 auto_identifier = function(x) {
   r = '<(h[1-6])([^>]*)>(.+?)'
-  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
@@ -612,7 +612,7 @@ embed_resources = function(x, embed = 'local') {
     x = if (length(grep('', x)) != 1) {
       one_string(I(c(x, x2)))
     } else {
-      match_replace(x, '', fixed = TRUE, function(z) {
+      match_replace(x, '', fixed = TRUE, perl = FALSE, function(z) {
         one_string(I(c(x2, z)))
       })
     }