Skip to content

Commit 5b04b2a

Browse files
committed
Fix exception for link in image, identifier matching
1 parent 6c89b18 commit 5b04b2a

File tree

7 files changed

+80
-39
lines changed

7 files changed

+80
-39
lines changed

index.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ function compiler() {
125125
var characterReferenceType
126126
var expectingFirstListItemValue
127127
var atHardBreak
128-
var inImage
129128
var inReference
130129
var referenceType
131130

@@ -319,7 +318,6 @@ function compiler() {
319318

320319
function enter(token) {
321320
var node = create(token)
322-
323321
context.children.push(node)
324322
context = node
325323
stack.push(node)
@@ -399,8 +397,10 @@ function compiler() {
399397
context.value = data
400398
}
401399

402-
function onexitdefinitionlabelstring() {
403-
var data = resume()
400+
function onexitdefinitionlabelstring(token) {
401+
// Discard label, use the source content instead.
402+
resume()
403+
var data = this.sliceSerialize(token)
404404
context.label = data
405405
context.identifier = normalizeIdentifier(data).toLowerCase()
406406
}
@@ -503,7 +503,6 @@ function compiler() {
503503

504504
function onenterimage() {
505505
buffer()
506-
inImage = true
507506
}
508507

509508
function onexitlink() {
@@ -551,10 +550,10 @@ function compiler() {
551550
// Assume a reference.
552551
inReference = true
553552

554-
if (inImage) {
553+
// If we’re in a fragment, we’re in an image and buffering.
554+
if (context.type === 'fragment') {
555555
data = resume()
556556
context.alt = data
557-
inImage = undefined
558557
}
559558
}
560559

@@ -576,8 +575,9 @@ function compiler() {
576575
referenceType = 'collapsed'
577576
}
578577

579-
function onexitreferencestring() {
580-
var data = resume()
578+
function onexitreferencestring(token) {
579+
resume()
580+
var data = this.sliceSerialize(token)
581581
context.label = data
582582
context.identifier = normalizeIdentifier(data).toLowerCase()
583583
referenceType = 'full'
@@ -659,11 +659,11 @@ function compiler() {
659659
}
660660

661661
function image() {
662-
return {type: 'image', title: null, url: null, alt: null}
662+
return {type: 'image', title: null, url: '', alt: null}
663663
}
664664

665665
function link() {
666-
return {type: 'link', title: null, url: null, children: []}
666+
return {type: 'link', title: null, url: '', children: []}
667667
}
668668

669669
function list(token) {

package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,19 @@
3636
},
3737
"devDependencies": {
3838
"browserify": "^16.0.0",
39+
"commonmark.json": "^0.29.0",
3940
"gzip-size-cli": "^3.0.0",
41+
"hast-util-to-html": "^7.0.0",
42+
"mdast-util-to-hast": "^9.0.0",
4043
"nyc": "^15.0.0",
4144
"prettier": "^2.0.0",
45+
"rehype-parse": "^7.0.0",
46+
"rehype-stringify": "^8.0.0",
4247
"remark-cli": "^8.0.0",
4348
"remark-preset-wooorm": "^7.0.0",
4449
"tape": "^5.0.0",
4550
"tinyify": "^3.0.0",
51+
"unified": "^9.0.0",
4652
"xo": "^0.33.0"
4753
},
4854
"scripts": {
@@ -72,6 +78,7 @@
7278
"esnext": false,
7379
"rules": {
7480
"complexity": "off",
81+
"unicorn/no-fn-reference-in-iterator": "off",
7582
"unicorn/prefer-number-properties": "off",
7683
"unicorn/prefer-optional-catch-binding": "off",
7784
"unicorn/prefer-set-has": "off"

test/fixtures/image-reference.json

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,6 @@
5555
},
5656
{
5757
"type": "imageReference",
58-
"identifier": "e",
59-
"label": "e",
60-
"referenceType": "shortcut",
6158
"alt": "e",
6259
"position": {
6360
"start": {
@@ -70,7 +67,10 @@
7067
"column": 17,
7168
"offset": 79
7269
}
73-
}
70+
},
71+
"label": "e",
72+
"identifier": "e",
73+
"referenceType": "shortcut"
7474
},
7575
{
7676
"type": "text",
@@ -90,9 +90,6 @@
9090
},
9191
{
9292
"type": "imageReference",
93-
"identifier": "f",
94-
"label": "f",
95-
"referenceType": "collapsed",
9693
"alt": "f",
9794
"position": {
9895
"start": {
@@ -105,7 +102,10 @@
105102
"column": 25,
106103
"offset": 87
107104
}
108-
}
105+
},
106+
"label": "f",
107+
"identifier": "f",
108+
"referenceType": "collapsed"
109109
},
110110
{
111111
"type": "text",
@@ -125,9 +125,6 @@
125125
},
126126
{
127127
"type": "imageReference",
128-
"identifier": "h",
129-
"label": "h",
130-
"referenceType": "full",
131128
"alt": "g",
132129
"position": {
133130
"start": {
@@ -140,7 +137,10 @@
140137
"column": 34,
141138
"offset": 96
142139
}
143-
}
140+
},
141+
"label": "h",
142+
"identifier": "h",
143+
"referenceType": "full"
144144
}
145145
],
146146
"position": {

test/fixtures/image-resource.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@
293293
{
294294
"type": "image",
295295
"title": null,
296-
"url": null,
296+
"url": "",
297297
"alt": "",
298298
"position": {
299299
"start": {

test/fixtures/link-reference.json

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,6 @@
5555
},
5656
{
5757
"type": "linkReference",
58-
"identifier": "e",
59-
"label": "e",
60-
"referenceType": "shortcut",
6158
"children": [
6259
{
6360
"type": "text",
@@ -87,7 +84,10 @@
8784
"column": 16,
8885
"offset": 75
8986
}
90-
}
87+
},
88+
"label": "e",
89+
"identifier": "e",
90+
"referenceType": "shortcut"
9191
},
9292
{
9393
"type": "text",
@@ -107,9 +107,6 @@
107107
},
108108
{
109109
"type": "linkReference",
110-
"identifier": "f",
111-
"label": "f",
112-
"referenceType": "collapsed",
113110
"children": [
114111
{
115112
"type": "text",
@@ -139,7 +136,10 @@
139136
"column": 23,
140137
"offset": 82
141138
}
142-
}
139+
},
140+
"label": "f",
141+
"identifier": "f",
142+
"referenceType": "collapsed"
143143
},
144144
{
145145
"type": "text",
@@ -159,9 +159,6 @@
159159
},
160160
{
161161
"type": "linkReference",
162-
"identifier": "h",
163-
"label": "h",
164-
"referenceType": "full",
165162
"children": [
166163
{
167164
"type": "text",
@@ -191,7 +188,10 @@
191188
"column": 31,
192189
"offset": 90
193190
}
194-
}
191+
},
192+
"label": "h",
193+
"identifier": "h",
194+
"referenceType": "full"
195195
}
196196
],
197197
"position": {

test/fixtures/link-resource.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@
460460
{
461461
"type": "link",
462462
"title": null,
463-
"url": null,
463+
"url": "",
464464
"children": [],
465465
"position": {
466466
"start": {

test/index.js

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,13 @@ var fs = require('fs')
44
var path = require('path')
55
var test = require('tape')
66
var unified = require('unified')
7-
var parse = require('remark-parse')
7+
var remarkParse = require('remark-parse')
8+
var rehypeParse = require('rehype-parse')
9+
var rehypeStringify = require('rehype-stringify')
810
var visit = require('unist-util-visit')
11+
var toHast = require('mdast-util-to-hast')
12+
var toHtml = require('hast-util-to-html')
13+
var commonmark = require('commonmark.json')
914
var fromMarkdown = require('..')
1015

1116
var join = path.join
@@ -798,8 +803,37 @@ test('fixtures', function (t) {
798803
}
799804
})
800805

806+
test('commonmark', function (t) {
807+
commonmark.forEach(each)
808+
809+
t.end()
810+
811+
function each(example, index) {
812+
var html = toHtml(
813+
toHast(fromMarkdown(example.markdown.slice(0, -1)), {
814+
allowDangerousHtml: true,
815+
commonmark: true
816+
}),
817+
{
818+
allowDangerousHtml: true,
819+
entities: {useNamedReferences: true},
820+
closeSelfClosing: true
821+
}
822+
)
823+
824+
var reformat = unified()
825+
.use(rehypeParse, {fragment: true})
826+
.use(rehypeStringify)
827+
828+
var actual = reformat.processSync(html).toString()
829+
var expected = reformat.processSync(example.html.slice(0, -1)).toString()
830+
831+
t.equal(actual, expected, example.section + ' (' + index + ')')
832+
}
833+
})
834+
801835
function remarkLegacyParse(doc) {
802-
var processor = unified().use(parse, {commonmark: true}).use(clean)
836+
var processor = unified().use(remarkParse, {commonmark: true}).use(clean)
803837
return processor.runSync(processor.parse(doc))
804838
}
805839

0 commit comments

Comments
 (0)