Skip to content

Commit 6c29d2c

Browse files
yihuijeroen
andauthored
Implement LaTeX footnotes using \footnotemark and \footnotetext (#32)
* use \footnotetext and \footnotemark to implement LaTeX footnotes * git diff be62b2b c3be50e > src/patches/latex-footnotes.diff * add tests, news, and bump version --------- Co-authored-by: Jeroen Ooms <[email protected]>
1 parent 1edd611 commit 6c29d2c

File tree

5 files changed

+56
-2
lines changed

5 files changed

+56
-2
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
2.0.0
22
- The tagfilter extension for markdown_html now actually works (#15)
33
- Backport patch to filter illegal control characters in markdown_xml()
4+
- Implement LaTeX footnotes (#32)
45

56
1.9.5
67
- Fix parallel make problem

src/cmark/latex.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,8 +447,20 @@ static int S_render_node(cmark_renderer *renderer, cmark_node *node,
447447
break;
448448

449449
case CMARK_NODE_FOOTNOTE_DEFINITION:
450+
if (entering) {
451+
LIT("\\footnotetext[");
452+
OUT(cmark_chunk_to_cstr(renderer->mem, &node->as.literal), false, LITERAL);
453+
LIT("]{");
454+
} else {
455+
LIT("}");
456+
}
457+
break;
450458
case CMARK_NODE_FOOTNOTE_REFERENCE:
451-
// TODO
459+
if (entering) {
460+
LIT("\\footnotemark[");
461+
OUT(cmark_chunk_to_cstr(renderer->mem, &node->parent_footnote_def->as.literal), false, LITERAL);
462+
LIT("]");
463+
}
452464
break;
453465

454466
default:

src/patches/apply.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22

33
# Upstream PR: https://github.com/github/cmark-gfm/pull/362
44
patch -p2 -d ../cmark < 362.diff
5-
5+
# Support footnotes for LaTeX: https://github.com/r-lib/commonmark/pull/32
6+
patch -p2 -d ../cmark < latex-footnotes.diff

src/patches/latex-footnotes.diff

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
diff --git a/src/cmark/latex.c b/src/cmark/latex.c
2+
index 1a6367a..5fab7ee 100644
3+
--- a/src/cmark/latex.c
4+
+++ b/src/cmark/latex.c
5+
@@ -447,8 +447,20 @@ static int S_render_node(cmark_renderer *renderer, cmark_node *node,
6+
break;
7+
8+
case CMARK_NODE_FOOTNOTE_DEFINITION:
9+
+ if (entering) {
10+
+ LIT("\\footnotetext[");
11+
+ OUT(cmark_chunk_to_cstr(renderer->mem, &node->as.literal), false, LITERAL);
12+
+ LIT("]{");
13+
+ } else {
14+
+ LIT("}");
15+
+ }
16+
+ break;
17+
case CMARK_NODE_FOOTNOTE_REFERENCE:
18+
- // TODO
19+
+ if (entering) {
20+
+ LIT("\\footnotemark[");
21+
+ OUT(cmark_chunk_to_cstr(renderer->mem, &node->parent_footnote_def->as.literal), false, LITERAL);
22+
+ LIT("]");
23+
+ }
24+
break;
25+
26+
default:

tests/testthat/test-extensions.R

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,20 @@ test_that("autolink", {
4242

4343
})
4444

45+
46+
test_that("footnotes", {
47+
# a single footnote
48+
md <- "Hello[^1]\n\n[^1]: A footnote."
49+
expect_equal(markdown_latex(md, footnotes = FALSE), "Hello{[}\\^{}1{]}\n\n{[}\\^{}1{]}: A footnote.\n")
50+
expect_equal(markdown_latex(md, footnotes = TRUE), "Hello\\footnotemark[1]\n\n\\footnotetext[1]{A footnote.\n\n}\n")
51+
52+
# multiple footnotes
53+
md <- "Hello[^1] World[^foo-2]\n\n[^1]: A footnote.\n\n[^foo-2]: Footnote ID does not have to be a number."
54+
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")
55+
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")
56+
})
57+
58+
4559
test_that('tagefilter', {
4660
input <- "<title><style></style></title>\n"
4761
expect_equal(input, markdown_html(input))

0 commit comments

Comments
 (0)