Skip to content

Commit 849a112

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

File tree

4 files changed

+139
-80
lines changed

4 files changed

+139
-80
lines changed

complex-types.d.ts

+3-38
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,4 @@
1-
import type {Parent} from 'unist'
2-
import type {PhrasingContent, BlockContent} from 'mdast'
1+
// To do: next major: remove this file.
2+
export type {ContainerDirective, LeafDirective, TextDirective} from './index.js'
33

4-
type DirectiveAttributes = Record<string, string>
5-
6-
/* eslint-disable @typescript-eslint/consistent-type-definitions */
7-
8-
interface DirectiveFields {
9-
name: string
10-
attributes?: DirectiveAttributes
11-
}
12-
13-
export interface TextDirective extends Parent, DirectiveFields {
14-
type: 'textDirective'
15-
children: PhrasingContent[]
16-
}
17-
18-
export interface LeafDirective extends Parent, DirectiveFields {
19-
type: 'leafDirective'
20-
children: PhrasingContent[]
21-
}
22-
23-
export interface ContainerDirective extends Parent, DirectiveFields {
24-
type: 'containerDirective'
25-
children: BlockContent[]
26-
}
27-
28-
declare module 'mdast' {
29-
interface StaticPhrasingContentMap {
30-
textDirective: TextDirective
31-
}
32-
33-
interface BlockContentMap {
34-
containerDirective: ContainerDirective
35-
leafDirective: LeafDirective
36-
}
37-
}
38-
39-
/* eslint-enable @typescript-eslint/consistent-type-definitions */
4+
/// <reference types="./index.js" />

index.d.ts

+96-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,75 @@
1-
export type {
2-
ContainerDirective,
3-
Directive,
4-
LeafDirective,
5-
TextDirective
6-
} from './lib/index.js'
1+
import type {BlockContent, PhrasingContent} from 'mdast'
72

83
export {directiveFromMarkdown, directiveToMarkdown} from './lib/index.js'
94

5+
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
6+
interface DirectiveFields {
7+
/**
8+
* Directive name.
9+
*/
10+
name: string
11+
12+
/**
13+
* Directive attributes.
14+
*/
15+
attributes?: Record<string, string> | undefined
16+
}
17+
18+
/**
19+
* Directive in flow content (such as in the root document, or block
20+
* quotes), which contains further flow content.
21+
*/
22+
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
23+
export interface ContainerDirective extends Parent, DirectiveFields {
24+
/**
25+
* Node type.
26+
*/
27+
type: 'containerDirective'
28+
29+
/**
30+
* Content.
31+
*/
32+
children: BlockContent[]
33+
}
34+
35+
/**
36+
* Directive in flow content (such as in the root document, or block
37+
* quotes), which contains nothing.
38+
*/
39+
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
40+
export interface LeafDirective extends Parent, DirectiveFields {
41+
/**
42+
* Node type.
43+
*/
44+
type: 'leafDirective'
45+
46+
/**
47+
* Content.
48+
*/
49+
children: PhrasingContent[]
50+
}
51+
52+
/**
53+
* Directive in phrasing content (such as in paragraphs, headings).
54+
*/
55+
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
56+
export interface TextDirective extends Parent, DirectiveFields {
57+
/**
58+
* Node type.
59+
*/
60+
type: 'textDirective'
61+
62+
/**
63+
* Content.
64+
*/
65+
children: PhrasingContent[]
66+
}
67+
68+
/**
69+
* The different directive nodes.
70+
*/
71+
export type Directive = ContainerDirective | LeafDirective | TextDirective
72+
1073
// Add custom data tracked to turn markdown into a tree.
1174
declare module 'mdast-util-from-markdown' {
1275
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
@@ -18,6 +81,7 @@ declare module 'mdast-util-from-markdown' {
1881
}
1982
}
2083

84+
// Add custom data tracked to turn a syntax tree into markdown.
2185
declare module 'mdast-util-to-markdown' {
2286
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
2387
interface ConstructNameMap {
@@ -85,3 +149,29 @@ declare module 'mdast-util-to-markdown' {
85149
textDirectiveLabel: 'textDirectiveLabel'
86150
}
87151
}
152+
153+
// Add nodes to content.
154+
declare module 'mdast' {
155+
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
156+
interface StaticPhrasingContentMap {
157+
/**
158+
* Directive in phrasing content (such as in paragraphs, headings).
159+
*/
160+
textDirective: TextDirective
161+
}
162+
163+
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
164+
interface BlockContentMap {
165+
/**
166+
* Directive in flow content (such as in the root document, or block
167+
* quotes), which contains further flow content.
168+
*/
169+
containerDirective: ContainerDirective
170+
171+
/**
172+
* Directive in flow content (such as in the root document, or block
173+
* quotes), which contains nothing.
174+
*/
175+
leafDirective: LeafDirective
176+
}
177+
}

lib/index.js

+39-36
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
/**
22
* @typedef {import('mdast').BlockContent} BlockContent
3-
* @typedef {import('mdast').Root} Root
43
* @typedef {import('mdast').Paragraph} Paragraph
5-
* @typedef {import('mdast-util-from-markdown').Handle} FromMarkdownHandle
6-
* @typedef {import('mdast-util-from-markdown').Extension} FromMarkdownExtension
4+
*
75
* @typedef {import('mdast-util-from-markdown').CompileContext} CompileContext
6+
* @typedef {import('mdast-util-from-markdown').Extension} FromMarkdownExtension
7+
* @typedef {import('mdast-util-from-markdown').Handle} FromMarkdownHandle
88
* @typedef {import('mdast-util-from-markdown').Token} Token
9+
*
910
* @typedef {import('mdast-util-to-markdown').ConstructName} ConstructName
10-
* @typedef {import('mdast-util-to-markdown/lib/types.js').Handle} ToMarkdownHandle
11+
* @typedef {import('mdast-util-to-markdown').Handle} ToMarkdownHandle
12+
* @typedef {import('mdast-util-to-markdown').Options} ToMarkdownExtension
1113
* @typedef {import('mdast-util-to-markdown').State} State
12-
* @typedef {import('mdast-util-to-markdown/lib/types.js').Options} ToMarkdownExtension
13-
* @typedef {import('../complex-types.js').ContainerDirective} ContainerDirective
14-
* @typedef {import('../complex-types.js').LeafDirective} LeafDirective
15-
* @typedef {import('../complex-types.js').TextDirective} TextDirective
16-
* @typedef {ContainerDirective|LeafDirective|TextDirective} Directive
14+
*
15+
* @typedef {import('../index.js').LeafDirective} LeafDirective
16+
* @typedef {import('../index.js').TextDirective} TextDirective
17+
* @typedef {import('../index.js').Directive} Directive
1718
*/
1819

1920
import {parseEntities} from 'parse-entities'
@@ -24,6 +25,9 @@ import {containerPhrasing} from 'mdast-util-to-markdown/lib/util/container-phras
2425
import {checkQuote} from 'mdast-util-to-markdown/lib/util/check-quote.js'
2526
import {track} from 'mdast-util-to-markdown/lib/util/track.js'
2627

28+
// To do: next major: replace `containerFlow`, `containerPhrasing`, `track`
29+
// with `state` methods.
30+
2731
const own = {}.hasOwnProperty
2832

2933
const shortcut = /^[^\t\n\r "#'.<=>`}]+$/
@@ -173,7 +177,7 @@ function enterAttributes() {
173177
* @type {FromMarkdownHandle}
174178
*/
175179
function exitAttributeIdValue(token) {
176-
const list = /** @type {Array.<[string, string]>} */ (
180+
const list = /** @type {Array<[string, string]>} */ (
177181
this.getData('directiveAttributes')
178182
)
179183
list.push([
@@ -189,7 +193,7 @@ function exitAttributeIdValue(token) {
189193
* @type {FromMarkdownHandle}
190194
*/
191195
function exitAttributeClassValue(token) {
192-
const list = /** @type {Array.<[string, string]>} */ (
196+
const list = /** @type {Array<[string, string]>} */ (
193197
this.getData('directiveAttributes')
194198
)
195199
list.push([
@@ -205,7 +209,7 @@ function exitAttributeClassValue(token) {
205209
* @type {FromMarkdownHandle}
206210
*/
207211
function exitAttributeValue(token) {
208-
const list = /** @type {Array.<[string, string]>} */ (
212+
const list = /** @type {Array<[string, string]>} */ (
209213
this.getData('directiveAttributes')
210214
)
211215
list[list.length - 1][1] = parseEntities(this.sliceSerialize(token), {
@@ -218,7 +222,7 @@ function exitAttributeValue(token) {
218222
* @type {FromMarkdownHandle}
219223
*/
220224
function exitAttributeName(token) {
221-
const list = /** @type {Array.<[string, string]>} */ (
225+
const list = /** @type {Array<[string, string]>} */ (
222226
this.getData('directiveAttributes')
223227
)
224228

@@ -232,10 +236,10 @@ function exitAttributeName(token) {
232236
* @type {FromMarkdownHandle}
233237
*/
234238
function exitAttributes() {
235-
const list = /** @type {Array.<[string, string]>} */ (
239+
const list = /** @type {Array<[string, string]>} */ (
236240
this.getData('directiveAttributes')
237241
)
238-
/** @type {Record.<string, string>} */
242+
/** @type {Record<string, string>} */
239243
const cleaned = {}
240244
let index = -1
241245

@@ -336,13 +340,13 @@ function attributes(node, state) {
336340
const quote = checkQuote(state)
337341
const subset = node.type === 'textDirective' ? [quote] : [quote, '\n', '\r']
338342
const attrs = node.attributes || {}
339-
/** @type {Array.<string>} */
343+
/** @type {Array<string>} */
340344
const values = []
341-
/** @type {string|undefined} */
345+
/** @type {string | undefined} */
342346
let classesFull
343-
/** @type {string|undefined} */
347+
/** @type {string | undefined} */
344348
let classes
345-
/** @type {string|undefined} */
349+
/** @type {string | undefined} */
346350
let id
347351
/** @type {string} */
348352
let key
@@ -359,9 +363,9 @@ function attributes(node, state) {
359363
id = shortcut.test(value) ? '#' + value : quoted('id', value)
360364
} else if (key === 'class') {
361365
const list = value.split(/[\t\n\r ]+/g)
362-
/** @type {Array.<string>} */
366+
/** @type {Array<string>} */
363367
const classesFullList = []
364-
/** @type {Array.<string>} */
368+
/** @type {Array<string>} */
365369
const classesList = []
366370
let index = -1
367371

@@ -429,7 +433,20 @@ function fence(node) {
429433
let size = 0
430434

431435
if (node.type === 'containerDirective') {
432-
visitParents(node, 'containerDirective', onvisit)
436+
visitParents(node, function (node, parents) {
437+
if (node.type === 'containerDirective') {
438+
let index = parents.length
439+
let nesting = 0
440+
441+
while (index--) {
442+
if (parents[index].type === 'containerDirective') {
443+
nesting++
444+
}
445+
}
446+
447+
if (nesting > size) size = nesting
448+
}
449+
})
433450
size += 3
434451
} else if (node.type === 'leafDirective') {
435452
size = 2
@@ -438,18 +455,4 @@ function fence(node) {
438455
}
439456

440457
return ':'.repeat(size)
441-
442-
/** @type {import('unist-util-visit-parents').BuildVisitor<Root, Directive>} */
443-
function onvisit(_, parents) {
444-
let index = parents.length
445-
let nesting = 0
446-
447-
while (index--) {
448-
if (parents[index].type === 'containerDirective') {
449-
nesting++
450-
}
451-
}
452-
453-
if (nesting > size) size = nesting
454-
}
455458
}

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"dependencies": {
4040
"@types/mdast": "^3.0.0",
4141
"@types/unist": "^2.0.0",
42+
"mdast-util-from-markdown": "^1.3.0",
4243
"mdast-util-to-markdown": "^1.5.0",
4344
"parse-entities": "^4.0.0",
4445
"stringify-entities": "^4.0.0",

0 commit comments

Comments
 (0)