Skip to content
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

Implement LaTeX footnotes using \footnotemark and \footnotetext #32

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: commonmark
Type: Package
Title: High Performance CommonMark and Github Markdown Rendering in R
Version: 1.9.3
Version: 1.9.4
Authors@R: c(
person("Jeroen", "Ooms", ,"[email protected]", role = c("aut", "cre"),
comment = c(ORCID = "0000-0002-4035-0289")),
Expand Down
1 change: 1 addition & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
1.9.4
- Apply upstream PR https://github.com/github/cmark-gfm/pull/362
- Require double-tilde `~~` for the strikethrough extension, for consistency with Pandoc's Markdown
- Implement LaTeX footnotes (#32)

1.9.1
- Update libcmark-gfm to 0.29.0.gfm.13
Expand Down
14 changes: 13 additions & 1 deletion src/cmark/latex.c
Original file line number Diff line number Diff line change
Expand Up @@ -447,8 +447,20 @@ static int S_render_node(cmark_renderer *renderer, cmark_node *node,
break;

case CMARK_NODE_FOOTNOTE_DEFINITION:
if (entering) {
LIT("\\footnotetext[");
OUT(cmark_chunk_to_cstr(renderer->mem, &node->as.literal), false, LITERAL);
LIT("]{");
} else {
LIT("}");
}
break;
case CMARK_NODE_FOOTNOTE_REFERENCE:
// TODO
if (entering) {
LIT("\\footnotemark[");
OUT(cmark_chunk_to_cstr(renderer->mem, &node->parent_footnote_def->as.literal), false, LITERAL);
LIT("]");
}
break;

default:
Expand Down
3 changes: 2 additions & 1 deletion src/patches/apply.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

# Upstream PR: https://github.com/github/cmark-gfm/pull/362
patch -p2 -d ../cmark < 362.diff

# Support footnotes for LaTeX: https://github.com/r-lib/commonmark/pull/32
patch -p2 -d ../cmark < latex-footnotes.diff
26 changes: 26 additions & 0 deletions src/patches/latex-footnotes.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
diff --git a/src/cmark/latex.c b/src/cmark/latex.c
index 1a6367a..5fab7ee 100644
--- a/src/cmark/latex.c
+++ b/src/cmark/latex.c
@@ -447,8 +447,20 @@ static int S_render_node(cmark_renderer *renderer, cmark_node *node,
break;

case CMARK_NODE_FOOTNOTE_DEFINITION:
+ if (entering) {
+ LIT("\\footnotetext[");
+ OUT(cmark_chunk_to_cstr(renderer->mem, &node->as.literal), false, LITERAL);
+ LIT("]{");
+ } else {
+ LIT("}");
+ }
+ break;
case CMARK_NODE_FOOTNOTE_REFERENCE:
- // TODO
+ if (entering) {
+ LIT("\\footnotemark[");
+ OUT(cmark_chunk_to_cstr(renderer->mem, &node->parent_footnote_def->as.literal), false, LITERAL);
+ LIT("]");
+ }
break;

default:
12 changes: 12 additions & 0 deletions tests/testthat/test-extensions.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ test_that("autolink", {

})

test_that("footnotes", {
# a single footnote
md <- "Hello[^1]\n\n[^1]: A footnote."
expect_equal(markdown_latex(md, footnotes = FALSE), "Hello{[}\\^{}1{]}\n\n{[}\\^{}1{]}: A footnote.\n")
expect_equal(markdown_latex(md, footnotes = TRUE), "Hello\\footnotemark[1]\n\n\\footnotetext[1]{A footnote.\n\n}\n")

# multiple footnotes
md <- "Hello[^1] World[^foo-2]\n\n[^1]: A footnote.\n\n[^foo-2]: Footnote ID does not have to be a number."
expect_equal(markdown_latex(md, footnotes = FALSE), "Hello{[}\\^{}1{]} World{[}\\^{}foo-2{]}\n\n{[}\\^{}1{]}: A footnote.\n\n{[}\\^{}foo-2{]}: Footnote ID does not have to be a number.\n")
expect_equal(markdown_latex(md, footnotes = TRUE), "Hello\\footnotemark[1] World\\footnotemark[foo-2]\n\n\\footnotetext[1]{A footnote.\n\n}\\footnotetext[foo-2]{Footnote ID does not have to be a number.\n\n}\n")
})

test_that("embedded images do not get filtered", {
md <- '<img src="data:image/png;base64,foobar" />\n'
expect_equal(md, markdown_html(md))
Expand Down
Loading