Skip to content

Commit f002af6

Browse files
committed
Refactor code-style
* Add more docs to JSDoc
1 parent 5c204e2 commit f002af6

File tree

1 file changed

+152
-141
lines changed

1 file changed

+152
-141
lines changed

lib/index.js

Lines changed: 152 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,17 @@ import {indentLines} from 'mdast-util-to-markdown/lib/util/indent-lines.js'
1616
import {safe} from 'mdast-util-to-markdown/lib/util/safe.js'
1717
import {track} from 'mdast-util-to-markdown/lib/util/track.js'
1818

19+
footnoteReference.peek = footnoteReferencePeek
20+
21+
// To do: next major: rename `context` -> `state`, `safeOptions` to `info`, use
22+
// utilities on `state`.
23+
1924
/**
25+
* Create an extension for `mdast-util-from-markdown` to enable footnotes
26+
* in markdown like in GFM.
27+
*
2028
* @returns {FromMarkdownExtension}
29+
* Extension for `mdast-util-from-markdown`.
2130
*/
2231
export function gfmFootnoteFromMarkdown() {
2332
return {
@@ -34,163 +43,165 @@ export function gfmFootnoteFromMarkdown() {
3443
gfmFootnoteCallString: exitFootnoteCallString
3544
}
3645
}
46+
}
3747

38-
/**
39-
* @this {CompileContext}
40-
* @type {FromMarkdownHandle}
41-
*/
42-
function enterFootnoteDefinition(token) {
43-
this.enter(
44-
{type: 'footnoteDefinition', identifier: '', label: '', children: []},
45-
token
46-
)
48+
/**
49+
* Create an extension for `mdast-util-to-markdown` to enable footnotes
50+
* in markdown like in GFM.
51+
*
52+
* @returns {ToMarkdownExtension}
53+
* Extension for `mdast-util-to-markdown`.
54+
*/
55+
export function gfmFootnoteToMarkdown() {
56+
return {
57+
// This is on by default already.
58+
unsafe: [{character: '[', inConstruct: ['phrasing', 'label', 'reference']}],
59+
handlers: {footnoteDefinition, footnoteReference}
4760
}
61+
}
4862

49-
/**
50-
* @this {CompileContext}
51-
* @type {FromMarkdownHandle}
52-
*/
53-
function enterFootnoteDefinitionLabelString() {
54-
this.buffer()
55-
}
63+
/**
64+
* @this {CompileContext}
65+
* @type {FromMarkdownHandle}
66+
*/
67+
function enterFootnoteDefinition(token) {
68+
this.enter(
69+
{type: 'footnoteDefinition', identifier: '', label: '', children: []},
70+
token
71+
)
72+
}
5673

57-
/**
58-
* @this {CompileContext}
59-
* @type {FromMarkdownHandle}
60-
*/
61-
function exitFootnoteDefinitionLabelString(token) {
62-
const label = this.resume()
63-
const node = /** @type {FootnoteDefinition} */ (
64-
this.stack[this.stack.length - 1]
65-
)
66-
node.label = label
67-
node.identifier = normalizeIdentifier(
68-
this.sliceSerialize(token)
69-
).toLowerCase()
70-
}
74+
/**
75+
* @this {CompileContext}
76+
* @type {FromMarkdownHandle}
77+
*/
78+
function enterFootnoteDefinitionLabelString() {
79+
this.buffer()
80+
}
7181

72-
/**
73-
* @this {CompileContext}
74-
* @type {FromMarkdownHandle}
75-
*/
76-
function exitFootnoteDefinition(token) {
77-
this.exit(token)
78-
}
82+
/**
83+
* @this {CompileContext}
84+
* @type {FromMarkdownHandle}
85+
*/
86+
function exitFootnoteDefinitionLabelString(token) {
87+
const label = this.resume()
88+
const node = /** @type {FootnoteDefinition} */ (
89+
this.stack[this.stack.length - 1]
90+
)
91+
node.label = label
92+
node.identifier = normalizeIdentifier(
93+
this.sliceSerialize(token)
94+
).toLowerCase()
95+
}
7996

80-
/**
81-
* @this {CompileContext}
82-
* @type {FromMarkdownHandle}
83-
*/
84-
function enterFootnoteCall(token) {
85-
this.enter({type: 'footnoteReference', identifier: '', label: ''}, token)
86-
}
97+
/**
98+
* @this {CompileContext}
99+
* @type {FromMarkdownHandle}
100+
*/
101+
function exitFootnoteDefinition(token) {
102+
this.exit(token)
103+
}
87104

88-
/**
89-
* @this {CompileContext}
90-
* @type {FromMarkdownHandle}
91-
*/
92-
function enterFootnoteCallString() {
93-
this.buffer()
94-
}
105+
/**
106+
* @this {CompileContext}
107+
* @type {FromMarkdownHandle}
108+
*/
109+
function enterFootnoteCall(token) {
110+
this.enter({type: 'footnoteReference', identifier: '', label: ''}, token)
111+
}
95112

96-
/**
97-
* @this {CompileContext}
98-
* @type {FromMarkdownHandle}
99-
*/
100-
function exitFootnoteCallString(token) {
101-
const label = this.resume()
102-
const node = /** @type {FootnoteDefinition} */ (
103-
this.stack[this.stack.length - 1]
104-
)
105-
node.label = label
106-
node.identifier = normalizeIdentifier(
107-
this.sliceSerialize(token)
108-
).toLowerCase()
109-
}
113+
/**
114+
* @this {CompileContext}
115+
* @type {FromMarkdownHandle}
116+
*/
117+
function enterFootnoteCallString() {
118+
this.buffer()
119+
}
110120

111-
/**
112-
* @this {CompileContext}
113-
* @type {FromMarkdownHandle}
114-
*/
115-
function exitFootnoteCall(token) {
116-
this.exit(token)
117-
}
121+
/**
122+
* @this {CompileContext}
123+
* @type {FromMarkdownHandle}
124+
*/
125+
function exitFootnoteCallString(token) {
126+
const label = this.resume()
127+
const node = /** @type {FootnoteDefinition} */ (
128+
this.stack[this.stack.length - 1]
129+
)
130+
node.label = label
131+
node.identifier = normalizeIdentifier(
132+
this.sliceSerialize(token)
133+
).toLowerCase()
118134
}
119135

120136
/**
121-
* @returns {ToMarkdownExtension}
137+
* @this {CompileContext}
138+
* @type {FromMarkdownHandle}
122139
*/
123-
export function gfmFootnoteToMarkdown() {
124-
footnoteReference.peek = footnoteReferencePeek
140+
function exitFootnoteCall(token) {
141+
this.exit(token)
142+
}
125143

126-
return {
127-
// This is on by default already.
128-
unsafe: [{character: '[', inConstruct: ['phrasing', 'label', 'reference']}],
129-
handlers: {footnoteDefinition, footnoteReference}
130-
}
144+
/**
145+
* @type {ToMarkdownHandle}
146+
* @param {FootnoteReference} node
147+
*/
148+
function footnoteReference(node, _, context, safeOptions) {
149+
const tracker = track(safeOptions)
150+
let value = tracker.move('[^')
151+
const exit = context.enter('footnoteReference')
152+
const subexit = context.enter('reference')
153+
value += tracker.move(
154+
safe(context, association(node), {
155+
...tracker.current(),
156+
before: value,
157+
after: ']'
158+
})
159+
)
160+
subexit()
161+
exit()
162+
value += tracker.move(']')
163+
return value
164+
}
131165

132-
/**
133-
* @type {ToMarkdownHandle}
134-
* @param {FootnoteReference} node
135-
*/
136-
function footnoteReference(node, _, context, safeOptions) {
137-
const tracker = track(safeOptions)
138-
let value = tracker.move('[^')
139-
const exit = context.enter('footnoteReference')
140-
const subexit = context.enter('reference')
141-
value += tracker.move(
142-
safe(context, association(node), {
143-
...tracker.current(),
144-
before: value,
145-
after: ']'
146-
})
147-
)
148-
subexit()
149-
exit()
150-
value += tracker.move(']')
151-
return value
152-
}
166+
/** @type {ToMarkdownHandle} */
167+
function footnoteReferencePeek() {
168+
return '['
169+
}
153170

154-
/** @type {ToMarkdownHandle} */
155-
function footnoteReferencePeek() {
156-
return '['
157-
}
171+
/**
172+
* @type {ToMarkdownHandle}
173+
* @param {FootnoteDefinition} node
174+
*/
175+
function footnoteDefinition(node, _, context, safeOptions) {
176+
const tracker = track(safeOptions)
177+
let value = tracker.move('[^')
178+
const exit = context.enter('footnoteDefinition')
179+
const subexit = context.enter('label')
180+
value += tracker.move(
181+
safe(context, association(node), {
182+
...tracker.current(),
183+
before: value,
184+
after: ']'
185+
})
186+
)
187+
subexit()
188+
value += tracker.move(
189+
']:' + (node.children && node.children.length > 0 ? ' ' : '')
190+
)
191+
tracker.shift(4)
192+
value += tracker.move(
193+
indentLines(containerFlow(node, context, tracker.current()), map)
194+
)
195+
exit()
196+
197+
return value
198+
}
158199

159-
/**
160-
* @type {ToMarkdownHandle}
161-
* @param {FootnoteDefinition} node
162-
*/
163-
function footnoteDefinition(node, _, context, safeOptions) {
164-
const tracker = track(safeOptions)
165-
let value = tracker.move('[^')
166-
const exit = context.enter('footnoteDefinition')
167-
const subexit = context.enter('label')
168-
value += tracker.move(
169-
safe(context, association(node), {
170-
...tracker.current(),
171-
before: value,
172-
after: ']'
173-
})
174-
)
175-
subexit()
176-
value += tracker.move(
177-
']:' + (node.children && node.children.length > 0 ? ' ' : '')
178-
)
179-
tracker.shift(4)
180-
value += tracker.move(
181-
indentLines(containerFlow(node, context, tracker.current()), map)
182-
)
183-
exit()
184-
185-
return value
186-
187-
/** @type {Map} */
188-
function map(line, index, blank) {
189-
if (index) {
190-
return (blank ? '' : ' ') + line
191-
}
192-
193-
return line
194-
}
200+
/** @type {Map} */
201+
function map(line, index, blank) {
202+
if (index === 0) {
203+
return line
195204
}
205+
206+
return (blank ? '' : ' ') + line
196207
}

0 commit comments

Comments
 (0)