Skip to content

Commit 1cc104f

Browse files
committed
Refactor code-style
* Add more docs to JSDoc * Add support for `null` in input of API types
1 parent 8509af4 commit 1cc104f

File tree

1 file changed

+38
-36
lines changed

1 file changed

+38
-36
lines changed

lib/index.js

+38-36
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
/**
22
* @typedef {import('mdast').Link} Link
3+
* @typedef {import('mdast').PhrasingContent} PhrasingContent
4+
*
35
* @typedef {import('mdast-util-from-markdown').CompileContext} CompileContext
46
* @typedef {import('mdast-util-from-markdown').Extension} FromMarkdownExtension
5-
* @typedef {import('mdast-util-from-markdown').Transform} FromMarkdownTransform
67
* @typedef {import('mdast-util-from-markdown').Handle} FromMarkdownHandle
8+
* @typedef {import('mdast-util-from-markdown').Transform} FromMarkdownTransform
9+
*
710
* @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+
*
913
* @typedef {import('mdast-util-find-and-replace').ReplaceFunction} ReplaceFunction
1014
* @typedef {import('mdast-util-find-and-replace').RegExpMatchObject} RegExpMatchObject
11-
* @typedef {import('mdast').PhrasingContent} PhrasingContent
1215
*/
1316

1417
import {ccount} from 'ccount'
@@ -20,7 +23,11 @@ const inConstruct = 'phrasing'
2023
/** @type {Array<ConstructName>} */
2124
const notInConstruct = ['autolink', 'link', 'image', 'label']
2225

23-
/** @type {FromMarkdownExtension} */
26+
/**
27+
* Extension for `mdast-util-from-markdown` to enable GFM autolink literals.
28+
*
29+
* @type {FromMarkdownExtension}
30+
*/
2431
export const gfmAutolinkLiteralFromMarkdown = {
2532
transforms: [transformGfmAutolinkLiterals],
2633
enter: {
@@ -37,7 +44,11 @@ export const gfmAutolinkLiteralFromMarkdown = {
3744
}
3845
}
3946

40-
/** @type {ToMarkdownExtension} */
47+
/**
48+
* Extension for `mdast-util-to-markdown` to enable GFM autolink literals.
49+
*
50+
* @type {ToMarkdownExtension}
51+
*/
4152
export const gfmAutolinkLiteralToMarkdown = {
4253
unsafe: [
4354
{
@@ -54,13 +65,7 @@ export const gfmAutolinkLiteralToMarkdown = {
5465
inConstruct,
5566
notInConstruct
5667
},
57-
{
58-
character: ':',
59-
before: '[ps]',
60-
after: '\\/',
61-
inConstruct,
62-
notInConstruct
63-
}
68+
{character: ':', before: '[ps]', after: '\\/', inConstruct, notInConstruct}
6469
]
6570
}
6671

@@ -133,6 +138,7 @@ function transformGfmAutolinkLiterals(tree) {
133138
* @param {string} domain
134139
* @param {string} path
135140
* @param {RegExpMatchObject} match
141+
* @returns {Link | Array<PhrasingContent> | false}
136142
*/
137143
// eslint-disable-next-line max-params
138144
function findUrl(_, protocol, domain, path, match) {
@@ -158,7 +164,7 @@ function findUrl(_, protocol, domain, path, match) {
158164

159165
if (!parts[0]) return false
160166

161-
/** @type {PhrasingContent} */
167+
/** @type {Link} */
162168
const result = {
163169
type: 'link',
164170
title: null,
@@ -179,6 +185,7 @@ function findUrl(_, protocol, domain, path, match) {
179185
* @param {string} atext
180186
* @param {string} label
181187
* @param {RegExpMatchObject} match
188+
* @returns {Link | false}
182189
*/
183190
function findEmail(_, atext, label, match) {
184191
if (
@@ -222,40 +229,35 @@ function isCorrectDomain(domain) {
222229

223230
/**
224231
* @param {string} url
225-
* @returns {[string, string|undefined]}
232+
* @returns {[string, string | undefined]}
226233
*/
227234
function splitUrl(url) {
228235
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)
241251
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++
251253
}
252254

253255
return [url, trail]
254256
}
255257

256258
/**
257259
* @param {RegExpMatchObject} match
258-
* @param {boolean} [email=false]
260+
* @param {boolean | null | undefined} [email=false]
259261
* @returns {boolean}
260262
*/
261263
function previous(match, email) {

0 commit comments

Comments
 (0)