1
1
/**
2
2
* @typedef {import('mdast').Link } Link
3
+ * @typedef {import('mdast').PhrasingContent } PhrasingContent
4
+ *
3
5
* @typedef {import('mdast-util-from-markdown').CompileContext } CompileContext
4
6
* @typedef {import('mdast-util-from-markdown').Extension } FromMarkdownExtension
5
- * @typedef {import('mdast-util-from-markdown').Transform } FromMarkdownTransform
6
7
* @typedef {import('mdast-util-from-markdown').Handle } FromMarkdownHandle
8
+ * @typedef {import('mdast-util-from-markdown').Transform } FromMarkdownTransform
9
+ *
7
10
* @typedef {import('mdast-util-to-markdown').ConstructName } ConstructName
8
- * @typedef {import('mdast-util-to-markdown/lib/types.js').Options } ToMarkdownExtension
11
+ * @typedef {import('mdast-util-to-markdown').Options } ToMarkdownExtension
12
+ *
9
13
* @typedef {import('mdast-util-find-and-replace').ReplaceFunction } ReplaceFunction
10
14
* @typedef {import('mdast-util-find-and-replace').RegExpMatchObject } RegExpMatchObject
11
- * @typedef {import('mdast').PhrasingContent } PhrasingContent
12
15
*/
13
16
14
17
import { ccount } from 'ccount'
@@ -20,7 +23,11 @@ const inConstruct = 'phrasing'
20
23
/** @type {Array<ConstructName> } */
21
24
const notInConstruct = [ 'autolink' , 'link' , 'image' , 'label' ]
22
25
23
- /** @type {FromMarkdownExtension } */
26
+ /**
27
+ * Extension for `mdast-util-from-markdown` to enable GFM autolink literals.
28
+ *
29
+ * @type {FromMarkdownExtension }
30
+ */
24
31
export const gfmAutolinkLiteralFromMarkdown = {
25
32
transforms : [ transformGfmAutolinkLiterals ] ,
26
33
enter : {
@@ -37,7 +44,11 @@ export const gfmAutolinkLiteralFromMarkdown = {
37
44
}
38
45
}
39
46
40
- /** @type {ToMarkdownExtension } */
47
+ /**
48
+ * Extension for `mdast-util-to-markdown` to enable GFM autolink literals.
49
+ *
50
+ * @type {ToMarkdownExtension }
51
+ */
41
52
export const gfmAutolinkLiteralToMarkdown = {
42
53
unsafe : [
43
54
{
@@ -54,13 +65,7 @@ export const gfmAutolinkLiteralToMarkdown = {
54
65
inConstruct,
55
66
notInConstruct
56
67
} ,
57
- {
58
- character : ':' ,
59
- before : '[ps]' ,
60
- after : '\\/' ,
61
- inConstruct,
62
- notInConstruct
63
- }
68
+ { character : ':' , before : '[ps]' , after : '\\/' , inConstruct, notInConstruct}
64
69
]
65
70
}
66
71
@@ -133,6 +138,7 @@ function transformGfmAutolinkLiterals(tree) {
133
138
* @param {string } domain
134
139
* @param {string } path
135
140
* @param {RegExpMatchObject } match
141
+ * @returns {Link | Array<PhrasingContent> | false }
136
142
*/
137
143
// eslint-disable-next-line max-params
138
144
function findUrl ( _ , protocol , domain , path , match ) {
@@ -158,7 +164,7 @@ function findUrl(_, protocol, domain, path, match) {
158
164
159
165
if ( ! parts [ 0 ] ) return false
160
166
161
- /** @type {PhrasingContent } */
167
+ /** @type {Link } */
162
168
const result = {
163
169
type : 'link' ,
164
170
title : null ,
@@ -179,6 +185,7 @@ function findUrl(_, protocol, domain, path, match) {
179
185
* @param {string } atext
180
186
* @param {string } label
181
187
* @param {RegExpMatchObject } match
188
+ * @returns {Link | false }
182
189
*/
183
190
function findEmail ( _ , atext , label , match ) {
184
191
if (
@@ -222,40 +229,35 @@ function isCorrectDomain(domain) {
222
229
223
230
/**
224
231
* @param {string } url
225
- * @returns {[string, string| undefined] }
232
+ * @returns {[string, string | undefined] }
226
233
*/
227
234
function splitUrl ( url ) {
228
235
const trailExec = / [ ! " & ' ) , . : ; < > ? \] } ] + $ / . exec ( url )
229
- /** @type {number } */
230
- let closingParenIndex
231
- /** @type {number } */
232
- let openingParens
233
- /** @type {number } */
234
- let closingParens
235
- /** @type {string|undefined } */
236
- let trail
237
-
238
- if ( trailExec ) {
239
- url = url . slice ( 0 , trailExec . index )
240
- trail = trailExec [ 0 ]
236
+
237
+ if ( ! trailExec ) {
238
+ return [ url , undefined ]
239
+ }
240
+
241
+ url = url . slice ( 0 , trailExec . index )
242
+
243
+ let trail = trailExec [ 0 ]
244
+ let closingParenIndex = trail . indexOf ( ')' )
245
+ const openingParens = ccount ( url , '(' )
246
+ let closingParens = ccount ( url , ')' )
247
+
248
+ while ( closingParenIndex !== - 1 && openingParens > closingParens ) {
249
+ url += trail . slice ( 0 , closingParenIndex + 1 )
250
+ trail = trail . slice ( closingParenIndex + 1 )
241
251
closingParenIndex = trail . indexOf ( ')' )
242
- openingParens = ccount ( url , '(' )
243
- closingParens = ccount ( url , ')' )
244
-
245
- while ( closingParenIndex !== - 1 && openingParens > closingParens ) {
246
- url += trail . slice ( 0 , closingParenIndex + 1 )
247
- trail = trail . slice ( closingParenIndex + 1 )
248
- closingParenIndex = trail . indexOf ( ')' )
249
- closingParens ++
250
- }
252
+ closingParens ++
251
253
}
252
254
253
255
return [ url , trail ]
254
256
}
255
257
256
258
/**
257
259
* @param {RegExpMatchObject } match
258
- * @param {boolean } [email=false]
260
+ * @param {boolean | null | undefined } [email=false]
259
261
* @returns {boolean }
260
262
*/
261
263
function previous ( match , email ) {
0 commit comments