Skip to content

Commit

Permalink
Fix to prefer footnote instead of reference label
Browse files Browse the repository at this point in the history
Closes GH-403.
Closes GH-419.
  • Loading branch information
wooorm authored Jul 11, 2019
1 parent e2ea7e8 commit c73f409
Show file tree
Hide file tree
Showing 6 changed files with 388 additions and 9 deletions.
12 changes: 9 additions & 3 deletions packages/remark-parse/lib/tokenize/reference.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var caret = '^'
function reference(eat, value, silent) {
var self = this
var commonmark = self.options.commonmark
var footnotes = self.options.footnotes
var character = value.charAt(0)
var index = 0
var length = value.length
Expand Down Expand Up @@ -55,7 +56,7 @@ function reference(eat, value, silent) {
queue = ''

// Check whether we’re eating a footnote.
if (self.options.footnotes && value.charAt(index) === caret) {
if (footnotes && value.charAt(index) === caret) {
// Exit if `![^` is found, so the `!` will be seen as text after this,
// and we’ll enter this function again when `[^` is found.
if (type === image) {
Expand Down Expand Up @@ -123,8 +124,13 @@ function reference(eat, value, silent) {

character = value.charAt(index)

// Inline footnotes cannot have an identifier.
if (type !== footnote && character === leftSquareBracket) {
// Inline footnotes cannot have a label.
// If footnotes are enabled, link labels cannot start with a caret.
if (
type !== footnote &&
character === leftSquareBracket &&
(!footnotes || value.charAt(index + 1) !== caret)
) {
identifier = ''
queue += character
index++
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/input/footnote-or-reference-label.text
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[alpha][^bravo]

[^bravo]: Footnote definition.

[alpha]: https://example.com
24 changes: 21 additions & 3 deletions test/fixtures/tree/footnote-like.footnotes.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
},
{
"type": "linkReference",
"identifier": "^foo",
"label": "^foo",
"referenceType": "full",
"identifier": "defined",
"label": "defined",
"referenceType": "shortcut",
"children": [
{
"type": "text",
Expand All @@ -51,6 +51,24 @@
"column": 21,
"offset": 20
},
"end": {
"line": 1,
"column": 30,
"offset": 29
},
"indent": []
}
},
{
"type": "footnoteReference",
"identifier": "foo",
"label": "foo",
"position": {
"start": {
"line": 1,
"column": 30,
"offset": 29
},
"end": {
"line": 1,
"column": 36,
Expand Down
24 changes: 21 additions & 3 deletions test/fixtures/tree/footnote-matrix.footnotes.json
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,9 @@
"children": [
{
"type": "linkReference",
"identifier": "^bar",
"label": "^bar",
"referenceType": "full",
"identifier": "foo",
"label": "foo",
"referenceType": "shortcut",
"children": [
{
"type": "text",
Expand All @@ -215,6 +215,24 @@
"column": 4,
"offset": 32
},
"end": {
"line": 3,
"column": 9,
"offset": 37
},
"indent": []
}
},
{
"type": "footnoteReference",
"identifier": "bar",
"label": "bar",
"position": {
"start": {
"line": 3,
"column": 9,
"offset": 37
},
"end": {
"line": 3,
"column": 15,
Expand Down
166 changes: 166 additions & 0 deletions test/fixtures/tree/footnote-or-reference-label.footnotes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
{
"type": "root",
"children": [
{
"type": "paragraph",
"children": [
{
"type": "linkReference",
"identifier": "alpha",
"label": "alpha",
"referenceType": "shortcut",
"children": [
{
"type": "text",
"value": "alpha",
"position": {
"start": {
"line": 1,
"column": 2,
"offset": 1
},
"end": {
"line": 1,
"column": 7,
"offset": 6
},
"indent": []
}
}
],
"position": {
"start": {
"line": 1,
"column": 1,
"offset": 0
},
"end": {
"line": 1,
"column": 8,
"offset": 7
},
"indent": []
}
},
{
"type": "footnoteReference",
"identifier": "bravo",
"label": "bravo",
"position": {
"start": {
"line": 1,
"column": 8,
"offset": 7
},
"end": {
"line": 1,
"column": 16,
"offset": 15
},
"indent": []
}
}
],
"position": {
"start": {
"line": 1,
"column": 1,
"offset": 0
},
"end": {
"line": 1,
"column": 16,
"offset": 15
},
"indent": []
}
},
{
"type": "footnoteDefinition",
"identifier": "bravo",
"label": "bravo",
"children": [
{
"type": "paragraph",
"children": [
{
"type": "text",
"value": "Footnote definition.",
"position": {
"start": {
"line": 3,
"column": 11,
"offset": 27
},
"end": {
"line": 3,
"column": 31,
"offset": 47
},
"indent": []
}
}
],
"position": {
"start": {
"line": 3,
"column": 11,
"offset": 27
},
"end": {
"line": 3,
"column": 31,
"offset": 47
},
"indent": []
}
}
],
"position": {
"start": {
"line": 3,
"column": 1,
"offset": 17
},
"end": {
"line": 3,
"column": 31,
"offset": 47
},
"indent": []
}
},
{
"type": "definition",
"identifier": "alpha",
"label": "alpha",
"title": null,
"url": "https://example.com",
"position": {
"start": {
"line": 5,
"column": 1,
"offset": 49
},
"end": {
"line": 5,
"column": 29,
"offset": 77
},
"indent": []
}
}
],
"position": {
"start": {
"line": 1,
"column": 1,
"offset": 0
},
"end": {
"line": 6,
"column": 1,
"offset": 78
}
}
}
Loading

0 comments on commit c73f409

Please sign in to comment.