Skip to content

Commit 20ae207

Browse files
committed
Change to expose functions
1 parent 8c8d4c7 commit 20ae207

File tree

3 files changed

+89
-67
lines changed

3 files changed

+89
-67
lines changed

lib/index.js

Lines changed: 49 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -24,52 +24,64 @@ const inConstruct = 'phrasing'
2424
/** @type {Array<ConstructName>} */
2525
const notInConstruct = ['autolink', 'link', 'image', 'label']
2626

27-
// To do: next major: expose functions instead of extensions.
28-
2927
/**
30-
* Extension for `mdast-util-from-markdown` to enable GFM autolink literals.
28+
* Create an extension for `mdast-util-from-markdown` to enable GFM autolink
29+
* literals in markdown.
3130
*
32-
* @type {FromMarkdownExtension}
31+
* @returns {FromMarkdownExtension}
32+
* Extension for `mdast-util-to-markdown` to enable GFM autolink literals.
3333
*/
34-
export const gfmAutolinkLiteralFromMarkdown = {
35-
transforms: [transformGfmAutolinkLiterals],
36-
enter: {
37-
literalAutolink: enterLiteralAutolink,
38-
literalAutolinkEmail: enterLiteralAutolinkValue,
39-
literalAutolinkHttp: enterLiteralAutolinkValue,
40-
literalAutolinkWww: enterLiteralAutolinkValue
41-
},
42-
exit: {
43-
literalAutolink: exitLiteralAutolink,
44-
literalAutolinkEmail: exitLiteralAutolinkEmail,
45-
literalAutolinkHttp: exitLiteralAutolinkHttp,
46-
literalAutolinkWww: exitLiteralAutolinkWww
34+
export function gfmAutolinkLiteralFromMarkdown() {
35+
return {
36+
transforms: [transformGfmAutolinkLiterals],
37+
enter: {
38+
literalAutolink: enterLiteralAutolink,
39+
literalAutolinkEmail: enterLiteralAutolinkValue,
40+
literalAutolinkHttp: enterLiteralAutolinkValue,
41+
literalAutolinkWww: enterLiteralAutolinkValue
42+
},
43+
exit: {
44+
literalAutolink: exitLiteralAutolink,
45+
literalAutolinkEmail: exitLiteralAutolinkEmail,
46+
literalAutolinkHttp: exitLiteralAutolinkHttp,
47+
literalAutolinkWww: exitLiteralAutolinkWww
48+
}
4749
}
4850
}
4951

5052
/**
51-
* Extension for `mdast-util-to-markdown` to enable GFM autolink literals.
53+
* Create an extension for `mdast-util-to-markdown` to enable GFM autolink
54+
* literals in markdown.
5255
*
53-
* @type {ToMarkdownExtension}
56+
* @returns {ToMarkdownExtension}
57+
* Extension for `mdast-util-to-markdown` to enable GFM autolink literals.
5458
*/
55-
export const gfmAutolinkLiteralToMarkdown = {
56-
unsafe: [
57-
{
58-
character: '@',
59-
before: '[+\\-.\\w]',
60-
after: '[\\-.\\w]',
61-
inConstruct,
62-
notInConstruct
63-
},
64-
{
65-
character: '.',
66-
before: '[Ww]',
67-
after: '[\\-.\\w]',
68-
inConstruct,
69-
notInConstruct
70-
},
71-
{character: ':', before: '[ps]', after: '\\/', inConstruct, notInConstruct}
72-
]
59+
export function gfmAutolinkLiteralToMarkdown() {
60+
return {
61+
unsafe: [
62+
{
63+
character: '@',
64+
before: '[+\\-.\\w]',
65+
after: '[\\-.\\w]',
66+
inConstruct,
67+
notInConstruct
68+
},
69+
{
70+
character: '.',
71+
before: '[Ww]',
72+
after: '[\\-.\\w]',
73+
inConstruct,
74+
notInConstruct
75+
},
76+
{
77+
character: ':',
78+
before: '[ps]',
79+
after: '\\/',
80+
inConstruct,
81+
notInConstruct
82+
}
83+
]
84+
}
7385
}
7486

7587
/**

readme.md

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
* [Install](#install)
1818
* [Use](#use)
1919
* [API](#api)
20-
* [`gfmAutolinkLiteralFromMarkdown`](#gfmautolinkliteralfrommarkdown)
21-
* [`gfmAutolinkLiteralToMarkdown`](#gfmautolinkliteraltomarkdown)
20+
* [`gfmAutolinkLiteralFromMarkdown()`](#gfmautolinkliteralfrommarkdown)
21+
* [`gfmAutolinkLiteralToMarkdown()`](#gfmautolinkliteraltomarkdown)
2222
* [HTML](#html)
2323
* [Syntax](#syntax)
2424
* [Syntax tree](#syntax-tree)
@@ -120,12 +120,12 @@ const doc = await fs.readFile('example.md')
120120

121121
const tree = fromMarkdown(doc, {
122122
extensions: [gfmAutolinkLiteral()],
123-
mdastExtensions: [gfmAutolinkLiteralFromMarkdown]
123+
mdastExtensions: [gfmAutolinkLiteralFromMarkdown()]
124124
})
125125

126126
console.log(tree)
127127

128-
const out = toMarkdown(tree, {extensions: [gfmAutolinkLiteralToMarkdown]})
128+
const out = toMarkdown(tree, {extensions: [gfmAutolinkLiteralToMarkdown()]})
129129

130130
console.log(out)
131131
```
@@ -177,15 +177,25 @@ This package exports the identifiers
177177
[`gfmAutolinkLiteralToMarkdown`][api-gfm-autolink-literal-to-markdown].
178178
There is no default export.
179179

180-
### `gfmAutolinkLiteralFromMarkdown`
180+
### `gfmAutolinkLiteralFromMarkdown()`
181181

182-
Extension for [`mdast-util-from-markdown`][mdast-util-from-markdown] to enable
183-
GFM autolink literals ([`FromMarkdownExtension`][from-markdown-extension]).
182+
Create an extension for [`mdast-util-from-markdown`][mdast-util-from-markdown]
183+
to enable GFM autolink literals in markdown.
184184

185-
### `gfmAutolinkLiteralToMarkdown`
185+
###### Returns
186186

187-
Extension for [`mdast-util-to-markdown`][mdast-util-to-markdown] to enable
188-
GFM autolink literals ([`ToMarkdownExtension`][to-markdown-extension]).
187+
Extension for `mdast-util-to-markdown` to enable GFM autolink literals
188+
([`FromMarkdownExtension`][from-markdown-extension]).
189+
190+
### `gfmAutolinkLiteralToMarkdown()`
191+
192+
Create an extension for [`mdast-util-to-markdown`][mdast-util-to-markdown] to
193+
enable GFM autolink literals in markdown.
194+
195+
###### Returns
196+
197+
Extension for `mdast-util-to-markdown` to enable GFM autolink literals
198+
([`ToMarkdownExtension`][to-markdown-extension]).
189199

190200
## HTML
191201

test/index.js

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ test('core', async function (t) {
2020
})
2121
})
2222

23-
test('gfmAutolinkLiteralFromMarkdown', async function (t) {
23+
test('gfmAutolinkLiteralFromMarkdown()', async function (t) {
2424
await t.test('should support autolink literals', async function () {
2525
assert.deepEqual(
2626
fromMarkdown(
2727
'www.example.com, https://example.com, and contact@example.com.',
2828
{
2929
extensions: [gfmAutolinkLiteral()],
30-
mdastExtensions: [gfmAutolinkLiteralFromMarkdown]
30+
mdastExtensions: [gfmAutolinkLiteralFromMarkdown()]
3131
}
3232
),
3333
{
@@ -136,7 +136,7 @@ test('gfmAutolinkLiteralFromMarkdown', async function (t) {
136136
assert.deepEqual(
137137
fromMarkdown('[https://google.com](https://google.com)', {
138138
extensions: [gfmAutolinkLiteral()],
139-
mdastExtensions: [gfmAutolinkLiteralFromMarkdown]
139+
mdastExtensions: [gfmAutolinkLiteralFromMarkdown()]
140140
}),
141141
{
142142
type: 'root',
@@ -179,7 +179,7 @@ test('gfmAutolinkLiteralFromMarkdown', async function (t) {
179179
})
180180
})
181181

182-
test('gfmAutolinkLiteralToMarkdown', async function (t) {
182+
test('gfmAutolinkLiteralToMarkdown()', async function (t) {
183183
await t.test('should not serialize autolink literals', async function () {
184184
assert.deepEqual(
185185
toMarkdown(
@@ -196,7 +196,7 @@ test('gfmAutolinkLiteralToMarkdown', async function (t) {
196196
{type: 'text', value: ' c.'}
197197
]
198198
},
199-
{extensions: [gfmAutolinkLiteralToMarkdown]}
199+
{extensions: [gfmAutolinkLiteralToMarkdown()]}
200200
),
201201
'a <contact@example.com> c.\n'
202202
)
@@ -208,7 +208,7 @@ test('gfmAutolinkLiteralToMarkdown', async function (t) {
208208
assert.deepEqual(
209209
toMarkdown(
210210
{type: 'paragraph', children: [{type: 'text', value: 'a b@c.d'}]},
211-
{extensions: [gfmAutolinkLiteralToMarkdown]}
211+
{extensions: [gfmAutolinkLiteralToMarkdown()]}
212212
),
213213
'a b\\@c.d\n'
214214
)
@@ -221,7 +221,7 @@ test('gfmAutolinkLiteralToMarkdown', async function (t) {
221221
assert.deepEqual(
222222
toMarkdown(
223223
{type: 'paragraph', children: [{type: 'text', value: 'a @c'}]},
224-
{extensions: [gfmAutolinkLiteralToMarkdown]}
224+
{extensions: [gfmAutolinkLiteralToMarkdown()]}
225225
),
226226
'a @c\n'
227227
)
@@ -234,7 +234,7 @@ test('gfmAutolinkLiteralToMarkdown', async function (t) {
234234
assert.deepEqual(
235235
toMarkdown(
236236
{type: 'paragraph', children: [{type: 'text', value: 'a www.b.c'}]},
237-
{extensions: [gfmAutolinkLiteralToMarkdown]}
237+
{extensions: [gfmAutolinkLiteralToMarkdown()]}
238238
),
239239
'a www\\.b.c\n'
240240
)
@@ -247,7 +247,7 @@ test('gfmAutolinkLiteralToMarkdown', async function (t) {
247247
assert.deepEqual(
248248
toMarkdown(
249249
{type: 'paragraph', children: [{type: 'text', value: 'a.b'}]},
250-
{extensions: [gfmAutolinkLiteralToMarkdown]}
250+
{extensions: [gfmAutolinkLiteralToMarkdown()]}
251251
),
252252
'a.b\n'
253253
)
@@ -260,7 +260,7 @@ test('gfmAutolinkLiteralToMarkdown', async function (t) {
260260
assert.deepEqual(
261261
toMarkdown(
262262
{type: 'paragraph', children: [{type: 'text', value: 'https:/'}]},
263-
{extensions: [gfmAutolinkLiteralToMarkdown]}
263+
{extensions: [gfmAutolinkLiteralToMarkdown()]}
264264
),
265265
'https\\:/\n'
266266
)
@@ -273,7 +273,7 @@ test('gfmAutolinkLiteralToMarkdown', async function (t) {
273273
assert.deepEqual(
274274
toMarkdown(
275275
{type: 'paragraph', children: [{type: 'text', value: 'https:a'}]},
276-
{extensions: [gfmAutolinkLiteralToMarkdown]}
276+
{extensions: [gfmAutolinkLiteralToMarkdown()]}
277277
),
278278
'https:a\n'
279279
)
@@ -286,7 +286,7 @@ test('gfmAutolinkLiteralToMarkdown', async function (t) {
286286
assert.deepEqual(
287287
toMarkdown(
288288
{type: 'definition', label: 'http://a', identifier: '', url: ''},
289-
{extensions: [gfmAutolinkLiteralToMarkdown]}
289+
{extensions: [gfmAutolinkLiteralToMarkdown()]}
290290
),
291291
'[http://a]: <>\n'
292292
)
@@ -310,7 +310,7 @@ test('gfmAutolinkLiteralToMarkdown', async function (t) {
310310
}
311311
]
312312
},
313-
{extensions: [gfmAutolinkLiteralToMarkdown]}
313+
{extensions: [gfmAutolinkLiteralToMarkdown()]}
314314
),
315315
'[http://a][]\n'
316316
)
@@ -334,7 +334,7 @@ test('gfmAutolinkLiteralToMarkdown', async function (t) {
334334
}
335335
]
336336
},
337-
{extensions: [gfmAutolinkLiteralToMarkdown]}
337+
{extensions: [gfmAutolinkLiteralToMarkdown()]}
338338
),
339339
'[http://a][a]\n'
340340
)
@@ -358,7 +358,7 @@ test('gfmAutolinkLiteralToMarkdown', async function (t) {
358358
}
359359
]
360360
},
361-
{extensions: [gfmAutolinkLiteralToMarkdown]}
361+
{extensions: [gfmAutolinkLiteralToMarkdown()]}
362362
),
363363
'[a][http://a]\n'
364364
)
@@ -380,7 +380,7 @@ test('gfmAutolinkLiteralToMarkdown', async function (t) {
380380
}
381381
]
382382
},
383-
{extensions: [gfmAutolinkLiteralToMarkdown]}
383+
{extensions: [gfmAutolinkLiteralToMarkdown()]}
384384
),
385385
'[a](http://a)\n'
386386
)
@@ -404,7 +404,7 @@ test('gfmAutolinkLiteralToMarkdown', async function (t) {
404404
}
405405
]
406406
},
407-
{extensions: [gfmAutolinkLiteralToMarkdown]}
407+
{extensions: [gfmAutolinkLiteralToMarkdown()]}
408408
),
409409
'![a][http://a]\n'
410410
)
@@ -428,7 +428,7 @@ test('gfmAutolinkLiteralToMarkdown', async function (t) {
428428
}
429429
]
430430
},
431-
{extensions: [gfmAutolinkLiteralToMarkdown]}
431+
{extensions: [gfmAutolinkLiteralToMarkdown()]}
432432
),
433433
'![http://a][a]\n'
434434
)
@@ -444,7 +444,7 @@ test('gfmAutolinkLiteralToMarkdown', async function (t) {
444444
type: 'paragraph',
445445
children: [{type: 'image', url: 'http://a', alt: 'a'}]
446446
},
447-
{extensions: [gfmAutolinkLiteralToMarkdown]}
447+
{extensions: [gfmAutolinkLiteralToMarkdown()]}
448448
),
449449
'![a](http://a)\n'
450450
)
@@ -474,7 +474,7 @@ test('fixtures', async function (t) {
474474

475475
const mdast = fromMarkdown(input, {
476476
extensions: [gfmAutolinkLiteral()],
477-
mdastExtensions: [gfmAutolinkLiteralFromMarkdown]
477+
mdastExtensions: [gfmAutolinkLiteralFromMarkdown()]
478478
})
479479

480480
// @ts-expect-error: to do, remove when `to-hast` is released.

0 commit comments

Comments
 (0)