Skip to content

Commit 971e026

Browse files
committed
Fix passing encoding: undefined w/ options
Closes GH-47.
1 parent ae0de20 commit 971e026

File tree

2 files changed

+103
-19
lines changed

2 files changed

+103
-19
lines changed

dev/lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ const own = {}.hasOwnProperty
7575
* mdast tree.
7676
*/
7777
export function fromMarkdown(value, encoding, options) {
78-
if (typeof encoding !== 'string') {
78+
if (encoding && typeof encoding === 'object') {
7979
options = encoding
8080
encoding = undefined
8181
}

test/index.js

Lines changed: 102 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/**
2+
* @import {Extension} from 'mdast-util-from-markdown'
23
* @import {Root} from 'mdast'
34
*/
45

@@ -14,6 +15,21 @@ import {toString} from 'mdast-util-to-string'
1415
import {fromMarkdown} from 'mdast-util-from-markdown'
1516

1617
test('fromMarkdown', async function (t) {
18+
/** @type {Extension} */
19+
const tinyExample = {
20+
canContainEols: ['someType'],
21+
enter: {
22+
lineEnding(token) {
23+
this.enter({type: 'break'}, token)
24+
}
25+
},
26+
exit: {
27+
lineEnding(token) {
28+
this.exit(token)
29+
}
30+
}
31+
}
32+
1733
await t.test('should expose the public api', async function () {
1834
assert.deepEqual(
1935
Object.keys(await import('mdast-util-from-markdown')).sort(),
@@ -93,24 +109,7 @@ test('fromMarkdown', async function (t) {
93109

94110
await t.test('should support extensions', async function () {
95111
assert.deepEqual(
96-
fromMarkdown('a\nb', {
97-
mdastExtensions: [
98-
{
99-
// `canContainEols` is an array.
100-
canContainEols: ['someType'],
101-
enter: {
102-
lineEnding(token) {
103-
this.enter({type: 'break'}, token)
104-
}
105-
},
106-
exit: {
107-
lineEnding(token) {
108-
this.exit(token)
109-
}
110-
}
111-
}
112-
]
113-
}).children[0],
112+
fromMarkdown('a\nb', {mdastExtensions: [tinyExample]}).children[0],
114113
{
115114
type: 'paragraph',
116115
children: [
@@ -203,6 +202,91 @@ test('fromMarkdown', async function (t) {
203202
)
204203
})
205204

205+
await t.test('should support encoding and options', async function () {
206+
assert.deepEqual(
207+
fromMarkdown(
208+
new Uint8Array([0x61, 0x00, 0x0a, 0x00, 0x62, 0x00]),
209+
'utf-16le',
210+
{mdastExtensions: [tinyExample]}
211+
).children[0],
212+
{
213+
type: 'paragraph',
214+
children: [
215+
{
216+
type: 'text',
217+
value: 'a',
218+
position: {
219+
start: {line: 1, column: 1, offset: 0},
220+
end: {line: 1, column: 2, offset: 1}
221+
}
222+
},
223+
{
224+
type: 'break',
225+
position: {
226+
start: {line: 1, column: 2, offset: 1},
227+
end: {line: 2, column: 1, offset: 2}
228+
}
229+
},
230+
{
231+
type: 'text',
232+
value: 'b',
233+
position: {
234+
start: {line: 2, column: 1, offset: 2},
235+
end: {line: 2, column: 2, offset: 3}
236+
}
237+
}
238+
],
239+
position: {
240+
start: {line: 1, column: 1, offset: 0},
241+
end: {line: 2, column: 2, offset: 3}
242+
}
243+
}
244+
)
245+
})
246+
247+
await t.test(
248+
'should support `encoding: undefined` and options',
249+
async function () {
250+
assert.deepEqual(
251+
fromMarkdown(new Uint8Array([0x61, 0x0a, 0x62]), undefined, {
252+
mdastExtensions: [tinyExample]
253+
}).children[0],
254+
{
255+
type: 'paragraph',
256+
children: [
257+
{
258+
type: 'text',
259+
value: 'a',
260+
position: {
261+
start: {line: 1, column: 1, offset: 0},
262+
end: {line: 1, column: 2, offset: 1}
263+
}
264+
},
265+
{
266+
type: 'break',
267+
position: {
268+
start: {line: 1, column: 2, offset: 1},
269+
end: {line: 2, column: 1, offset: 2}
270+
}
271+
},
272+
{
273+
type: 'text',
274+
value: 'b',
275+
position: {
276+
start: {line: 2, column: 1, offset: 2},
277+
end: {line: 2, column: 2, offset: 3}
278+
}
279+
}
280+
],
281+
position: {
282+
start: {line: 1, column: 1, offset: 0},
283+
end: {line: 2, column: 2, offset: 3}
284+
}
285+
}
286+
)
287+
}
288+
)
289+
206290
await t.test('should support `transforms` in extensions', async function () {
207291
assert.deepEqual(
208292
fromMarkdown('*a*', {

0 commit comments

Comments
 (0)