Skip to content

Commit db09826

Browse files
committed
Handle references within footnotes
1 parent 757514a commit db09826

2 files changed

Lines changed: 20 additions & 25 deletions

File tree

TextformatterFootnotes.info.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
$info = [
44
"title" => "Footnotes",
5-
"version" => "0.1.0",
5+
"version" => "0.1.1",
66
"summary" => "Adds footnotes using Markdown Extra’s syntax, minus Markdown",
77
"author" => "EPRC",
88
"href" => "https://github.com/eprcstudio/TextformatterFootnotes",

TextformatterFootnotes.module.php

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -84,21 +84,30 @@ public function ___addFootnotes($str, $options = [], $field = "") {
8484
$references = [];
8585
foreach($matches[0] as $key => $match) {
8686
$identifier = $matches[1][$key];
87-
if(!array_key_exists($identifier, $references)) {
88-
$references[$identifier] = [
89-
"id" => (!$options["continuous"] ? "$footnotesId:" : "") . $footnoteIndex,
90-
"identifier" => $identifier,
91-
"index" => $footnoteIndex,
92-
"str" => $match
93-
];
94-
$footnoteIndex++;
95-
}
87+
// Have we already processed this identifier?
88+
if(array_key_exists($identifier, $references)) continue;
89+
// Are there any matching footnote?
90+
if(!preg_match("/\[\^$identifier\]:/", $str)) continue;
91+
$id = (!$options["continuous"] ? "$footnotesId:" : "") . $footnoteIndex;
92+
$references[$identifier] = [
93+
"id" => $id,
94+
"identifier" => $identifier,
95+
"index" => $footnoteIndex,
96+
"str" => $match
97+
];
98+
// Convert references into anchor links
99+
$ref =
100+
"<sup id='fnref$id' class='$options[referenceClass]'>" .
101+
"<a href='#fn$id' role='doc-noteref'>$footnoteIndex</a>" .
102+
"</sup>";
103+
$str = preg_replace("/\[\^$identifier\](?!:)/", $ref, $str);
104+
$footnoteIndex++;
96105
}
97106

98107
if(empty($references)) return $str;
99108

100109
// Get footnotes
101-
if(!preg_match_all("/\[\^(\d+)\]\:(?:.(?!\[\^))*/", $str, $matches)) return $str;
110+
if(!preg_match_all("/\[\^(\d+)\]:.+?(?=\[\^\d+\]:|$)/m", $str, $matches)) return $str;
102111
$footnotes = [];
103112
foreach($matches[0] as $key => $match) {
104113
$identifier = $matches[1][$key];
@@ -124,20 +133,6 @@ public function ___addFootnotes($str, $options = [], $field = "") {
124133

125134
ksort($footnotes);
126135

127-
// Convert references into anchor links
128-
$identifiers = array_column($footnotes, "identifier");
129-
foreach($references as $reference) {
130-
$identifier = $reference["identifier"];
131-
// Cross-check
132-
if(!in_array($identifier, $identifiers)) continue;
133-
// Replace reference with anchor link
134-
$ref =
135-
"<sup id='fnref$reference[id]' class='$options[referenceClass]'>" .
136-
"<a href='#fn$reference[id]' role='doc-noteref'>$reference[index]</a>" .
137-
"</sup>";
138-
$str = preg_replace("/\[\^$identifier\](?!:)/", $ref, $str);
139-
}
140-
141136
// Append footnotes
142137
if(!$options["outputAsArray"]) {
143138
$str .= "\n" . $this->generateFootnotesMarkup($footnotes, $options);

0 commit comments

Comments
 (0)