1+ /**
2+ * @typedef {import('mdast-util-from-markdown').Extension } FromMarkdownExtension
3+ * @typedef {import('mdast-util-from-markdown').Transform } FromMarkdownTransform
4+ * @typedef {import('mdast-util-from-markdown').Handle } FromMarkdownHandle
5+ * @typedef {import('mdast-util-to-markdown/lib/types.js').Options } ToMarkdownExtension
6+ * @typedef {import('mdast-util-find-and-replace').ReplaceFunction } ReplaceFunction
7+ * @typedef {import('mdast-util-find-and-replace').RegExpMatchObject } RegExpMatchObject
8+ * @typedef {import('mdast-util-find-and-replace').PhrasingContent } PhrasingContent
9+ */
10+
111import { ccount } from 'ccount'
212import { findAndReplace } from 'mdast-util-find-and-replace'
3- import unicodePunctuation from 'micromark/dist/character/unicode-punctuation.js'
4- import unicodeWhitespace from 'micromark/dist/character/unicode-whitespace.js'
13+ import { unicodePunctuation , unicodeWhitespace } from 'micromark-util-character'
514
615const inConstruct = 'phrasing'
716const notInConstruct = [ 'autolink' , 'link' , 'image' , 'label' ]
817
18+ /** @type {FromMarkdownExtension } */
919export const gfmAutolinkLiteralFromMarkdown = {
1020 transforms : [ transformGfmAutolinkLiterals ] ,
1121 enter : {
@@ -22,6 +32,7 @@ export const gfmAutolinkLiteralFromMarkdown = {
2232 }
2333}
2434
35+ /** @type {ToMarkdownExtension } */
2536export const gfmAutolinkLiteralToMarkdown = {
2637 unsafe : [
2738 {
@@ -42,31 +53,39 @@ export const gfmAutolinkLiteralToMarkdown = {
4253 ]
4354}
4455
56+ /** @type {FromMarkdownHandle } */
4557function enterLiteralAutolink ( token ) {
58+ // @ts -expect-error: `null` is fine.
4659 this . enter ( { type : 'link' , title : null , url : '' , children : [ ] } , token )
4760}
4861
62+ /** @type {FromMarkdownHandle } */
4963function enterLiteralAutolinkValue ( token ) {
5064 this . config . enter . autolinkProtocol . call ( this , token )
5165}
5266
67+ /** @type {FromMarkdownHandle } */
5368function exitLiteralAutolinkHttp ( token ) {
5469 this . config . exit . autolinkProtocol . call ( this , token )
5570}
5671
72+ /** @type {FromMarkdownHandle } */
5773function exitLiteralAutolinkWww ( token ) {
5874 this . config . exit . data . call ( this , token )
5975 this . stack [ this . stack . length - 1 ] . url = 'http://' + this . sliceSerialize ( token )
6076}
6177
78+ /** @type {FromMarkdownHandle } */
6279function exitLiteralAutolinkEmail ( token ) {
6380 this . config . exit . autolinkEmail . call ( this , token )
6481}
6582
83+ /** @type {FromMarkdownHandle } */
6684function exitLiteralAutolink ( token ) {
6785 this . exit ( token )
6886}
6987
88+ /** @type {FromMarkdownTransform } */
7089function transformGfmAutolinkLiterals ( tree ) {
7190 findAndReplace (
7291 tree ,
@@ -78,8 +97,16 @@ function transformGfmAutolinkLiterals(tree) {
7897 )
7998}
8099
100+ /**
101+ * @type {ReplaceFunction }
102+ * @param {string } _
103+ * @param {string } protocol
104+ * @param {string } domain
105+ * @param {string } path
106+ * @param {RegExpMatchObject } match
107+ */
81108// eslint-disable-next-line max-params
82- function findUrl ( $0 , protocol , domain , path , match ) {
109+ function findUrl ( _ , protocol , domain , path , match ) {
83110 let prefix = ''
84111
85112 // Not an expected previous character.
@@ -102,34 +129,48 @@ function findUrl($0, protocol, domain, path, match) {
102129
103130 if ( ! parts [ 0 ] ) return false
104131
105- let result = {
132+ /** @type {PhrasingContent } */
133+ // @ts -expect-error: `null` is fine.
134+ const result = {
106135 type : 'link' ,
107136 title : null ,
108137 url : prefix + protocol + parts [ 0 ] ,
109138 children : [ { type : 'text' , value : protocol + parts [ 0 ] } ]
110139 }
111140
112141 if ( parts [ 1 ] ) {
113- result = [ result , { type : 'text' , value : parts [ 1 ] } ]
142+ return [ result , { type : 'text' , value : parts [ 1 ] } ]
114143 }
115144
116145 return result
117146}
118147
119- function findEmail ( $0 , atext , label , match ) {
148+ /**
149+ * @type {ReplaceFunction }
150+ * @param {string } _
151+ * @param {string } atext
152+ * @param {string } label
153+ * @param {RegExpMatchObject } match
154+ */
155+ function findEmail ( _ , atext , label , match ) {
120156 // Not an expected previous character.
121157 if ( ! previous ( match , true ) || / [ _ - ] $ / . test ( label ) ) {
122158 return false
123159 }
124160
125161 return {
126162 type : 'link' ,
163+ // @ts -expect-error: `null` is fine.
127164 title : null ,
128165 url : 'mailto:' + atext + '@' + label ,
129166 children : [ { type : 'text' , value : atext + '@' + label } ]
130167 }
131168}
132169
170+ /**
171+ * @param {string } domain
172+ * @returns {boolean }
173+ */
133174function isCorrectDomain ( domain ) {
134175 const parts = domain . split ( '.' )
135176
@@ -148,15 +189,24 @@ function isCorrectDomain(domain) {
148189 return true
149190}
150191
192+ /**
193+ * @param {string } url
194+ * @returns {[string, string|undefined] }
195+ */
151196function splitUrl ( url ) {
152- let trail = / [ ! " & ' ) , . : ; < > ? \] } ] + $ / . exec ( url )
197+ const trailExec = / [ ! " & ' ) , . : ; < > ? \] } ] + $ / . exec ( url )
198+ /** @type {number } */
153199 let closingParenIndex
200+ /** @type {number } */
154201 let openingParens
202+ /** @type {number } */
155203 let closingParens
204+ /** @type {string|undefined } */
205+ let trail
156206
157- if ( trail ) {
158- url = url . slice ( 0 , trail . index )
159- trail = trail [ 0 ]
207+ if ( trailExec ) {
208+ url = url . slice ( 0 , trailExec . index )
209+ trail = trailExec [ 0 ]
160210 closingParenIndex = trail . indexOf ( ')' )
161211 openingParens = ccount ( url , '(' )
162212 closingParens = ccount ( url , ')' )
@@ -172,8 +222,14 @@ function splitUrl(url) {
172222 return [ url , trail ]
173223}
174224
225+ /**
226+ * @param {RegExpMatchObject } match
227+ * @param {boolean } [email=false]
228+ * @returns {boolean }
229+ */
175230function previous ( match , email ) {
176231 const code = match . input . charCodeAt ( match . index - 1 )
232+
177233 return (
178234 ( match . index === 0 ||
179235 unicodeWhitespace ( code ) ||
0 commit comments