Skip to content

Commit 29748a5

Browse files
committed
Refactor code-style
1 parent 2828e2f commit 29748a5

File tree

2 files changed

+83
-91
lines changed

2 files changed

+83
-91
lines changed

lib/index.js

Lines changed: 81 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -18,142 +18,106 @@ import {normalizeIdentifier} from 'micromark-util-normalize-identifier'
1818
footnoteReference.peek = footnoteReferencePeek
1919

2020
/**
21-
* Create an extension for `mdast-util-from-markdown` to enable GFM footnotes
22-
* in markdown.
23-
*
24-
* @returns {FromMarkdownExtension}
25-
* Extension for `mdast-util-from-markdown`.
21+
* @this {CompileContext}
22+
* @type {FromMarkdownHandle}
2623
*/
27-
export function gfmFootnoteFromMarkdown() {
28-
return {
29-
enter: {
30-
gfmFootnoteDefinition: enterFootnoteDefinition,
31-
gfmFootnoteDefinitionLabelString: enterFootnoteDefinitionLabelString,
32-
gfmFootnoteCall: enterFootnoteCall,
33-
gfmFootnoteCallString: enterFootnoteCallString
34-
},
35-
exit: {
36-
gfmFootnoteDefinition: exitFootnoteDefinition,
37-
gfmFootnoteDefinitionLabelString: exitFootnoteDefinitionLabelString,
38-
gfmFootnoteCall: exitFootnoteCall,
39-
gfmFootnoteCallString: exitFootnoteCallString
40-
}
41-
}
24+
function enterFootnoteCallString() {
25+
this.buffer()
4226
}
4327

4428
/**
45-
* Create an extension for `mdast-util-to-markdown` to enable GFM footnotes
46-
* in markdown.
47-
*
48-
* @returns {ToMarkdownExtension}
49-
* Extension for `mdast-util-to-markdown`.
29+
* @this {CompileContext}
30+
* @type {FromMarkdownHandle}
5031
*/
51-
export function gfmFootnoteToMarkdown() {
52-
return {
53-
// This is on by default already.
54-
unsafe: [{character: '[', inConstruct: ['phrasing', 'label', 'reference']}],
55-
handlers: {footnoteDefinition, footnoteReference}
56-
}
32+
function enterFootnoteCall(token) {
33+
this.enter({type: 'footnoteReference', identifier: '', label: ''}, token)
5734
}
5835

5936
/**
6037
* @this {CompileContext}
6138
* @type {FromMarkdownHandle}
6239
*/
63-
function enterFootnoteDefinition(token) {
64-
this.enter(
65-
{type: 'footnoteDefinition', identifier: '', label: '', children: []},
66-
token
67-
)
40+
function enterFootnoteDefinitionLabelString() {
41+
this.buffer()
6842
}
6943

7044
/**
7145
* @this {CompileContext}
7246
* @type {FromMarkdownHandle}
7347
*/
74-
function enterFootnoteDefinitionLabelString() {
75-
this.buffer()
48+
function enterFootnoteDefinition(token) {
49+
this.enter(
50+
{type: 'footnoteDefinition', identifier: '', label: '', children: []},
51+
token
52+
)
7653
}
7754

7855
/**
7956
* @this {CompileContext}
8057
* @type {FromMarkdownHandle}
8158
*/
82-
function exitFootnoteDefinitionLabelString(token) {
59+
function exitFootnoteCallString(token) {
8360
const label = this.resume()
8461
const node = this.stack[this.stack.length - 1]
85-
assert(node.type === 'footnoteDefinition')
86-
node.label = label
62+
assert(node.type === 'footnoteReference')
8763
node.identifier = normalizeIdentifier(
8864
this.sliceSerialize(token)
8965
).toLowerCase()
66+
node.label = label
9067
}
9168

9269
/**
9370
* @this {CompileContext}
9471
* @type {FromMarkdownHandle}
9572
*/
96-
function exitFootnoteDefinition(token) {
73+
function exitFootnoteCall(token) {
9774
this.exit(token)
9875
}
9976

10077
/**
10178
* @this {CompileContext}
10279
* @type {FromMarkdownHandle}
10380
*/
104-
function enterFootnoteCall(token) {
105-
this.enter({type: 'footnoteReference', identifier: '', label: ''}, token)
106-
}
107-
108-
/**
109-
* @this {CompileContext}
110-
* @type {FromMarkdownHandle}
111-
*/
112-
function enterFootnoteCallString() {
113-
this.buffer()
114-
}
115-
116-
/**
117-
* @this {CompileContext}
118-
* @type {FromMarkdownHandle}
119-
*/
120-
function exitFootnoteCallString(token) {
81+
function exitFootnoteDefinitionLabelString(token) {
12182
const label = this.resume()
12283
const node = this.stack[this.stack.length - 1]
123-
assert(node.type === 'footnoteReference')
124-
node.label = label
84+
assert(node.type === 'footnoteDefinition')
12585
node.identifier = normalizeIdentifier(
12686
this.sliceSerialize(token)
12787
).toLowerCase()
88+
node.label = label
12889
}
12990

13091
/**
13192
* @this {CompileContext}
13293
* @type {FromMarkdownHandle}
13394
*/
134-
function exitFootnoteCall(token) {
95+
function exitFootnoteDefinition(token) {
13596
this.exit(token)
13697
}
13798

13899
/**
139100
* @type {ToMarkdownHandle}
140-
* @param {FootnoteReference} node
101+
* @param {FootnoteDefinition} node
141102
*/
142-
function footnoteReference(node, _, state, info) {
103+
function footnoteDefinition(node, _, state, info) {
143104
const tracker = state.createTracker(info)
144105
let value = tracker.move('[^')
145-
const exit = state.enter('footnoteReference')
146-
const subexit = state.enter('reference')
106+
const exit = state.enter('footnoteDefinition')
107+
const subexit = state.enter('label')
147108
value += tracker.move(
148-
state.safe(state.associationId(node), {
149-
...tracker.current(),
150-
before: value,
151-
after: ']'
152-
})
109+
state.safe(state.associationId(node), {before: value, after: ']'})
153110
)
154111
subexit()
112+
value += tracker.move(
113+
']:' + (node.children && node.children.length > 0 ? ' ' : '')
114+
)
115+
tracker.shift(4)
116+
value += tracker.move(
117+
state.indentLines(state.containerFlow(node, tracker.current()), map)
118+
)
155119
exit()
156-
value += tracker.move(']')
120+
157121
return value
158122
}
159123

@@ -164,33 +128,61 @@ function footnoteReferencePeek() {
164128

165129
/**
166130
* @type {ToMarkdownHandle}
167-
* @param {FootnoteDefinition} node
131+
* @param {FootnoteReference} node
168132
*/
169-
function footnoteDefinition(node, _, state, info) {
133+
function footnoteReference(node, _, state, info) {
170134
const tracker = state.createTracker(info)
171135
let value = tracker.move('[^')
172-
const exit = state.enter('footnoteDefinition')
173-
const subexit = state.enter('label')
136+
const exit = state.enter('footnoteReference')
137+
const subexit = state.enter('reference')
174138
value += tracker.move(
175-
state.safe(state.associationId(node), {
176-
...tracker.current(),
177-
before: value,
178-
after: ']'
179-
})
139+
state.safe(state.associationId(node), {after: ']', before: value})
180140
)
181141
subexit()
182-
value += tracker.move(
183-
']:' + (node.children && node.children.length > 0 ? ' ' : '')
184-
)
185-
tracker.shift(4)
186-
value += tracker.move(
187-
state.indentLines(state.containerFlow(node, tracker.current()), map)
188-
)
189142
exit()
190-
143+
value += tracker.move(']')
191144
return value
192145
}
193146

147+
/**
148+
* Create an extension for `mdast-util-from-markdown` to enable GFM footnotes
149+
* in markdown.
150+
*
151+
* @returns {FromMarkdownExtension}
152+
* Extension for `mdast-util-from-markdown`.
153+
*/
154+
export function gfmFootnoteFromMarkdown() {
155+
return {
156+
enter: {
157+
gfmFootnoteCallString: enterFootnoteCallString,
158+
gfmFootnoteCall: enterFootnoteCall,
159+
gfmFootnoteDefinitionLabelString: enterFootnoteDefinitionLabelString,
160+
gfmFootnoteDefinition: enterFootnoteDefinition
161+
},
162+
exit: {
163+
gfmFootnoteCallString: exitFootnoteCallString,
164+
gfmFootnoteCall: exitFootnoteCall,
165+
gfmFootnoteDefinitionLabelString: exitFootnoteDefinitionLabelString,
166+
gfmFootnoteDefinition: exitFootnoteDefinition
167+
}
168+
}
169+
}
170+
171+
/**
172+
* Create an extension for `mdast-util-to-markdown` to enable GFM footnotes
173+
* in markdown.
174+
*
175+
* @returns {ToMarkdownExtension}
176+
* Extension for `mdast-util-to-markdown`.
177+
*/
178+
export function gfmFootnoteToMarkdown() {
179+
return {
180+
handlers: {footnoteDefinition, footnoteReference},
181+
// This is on by default already.
182+
unsafe: [{character: '[', inConstruct: ['label', 'phrasing', 'reference']}]
183+
}
184+
}
185+
194186
/** @type {Map} */
195187
function map(line, index, blank) {
196188
if (index === 0) {

test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import assert from 'node:assert/strict'
22
import test from 'node:test'
33
import {fromMarkdown} from 'mdast-util-from-markdown'
4-
import {toMarkdown} from 'mdast-util-to-markdown'
5-
import {gfmFootnote} from 'micromark-extension-gfm-footnote'
64
import {
75
gfmFootnoteFromMarkdown,
86
gfmFootnoteToMarkdown
97
} from 'mdast-util-gfm-footnote'
8+
import {toMarkdown} from 'mdast-util-to-markdown'
9+
import {gfmFootnote} from 'micromark-extension-gfm-footnote'
1010

1111
test('core', async function (t) {
1212
await t.test('should expose the public api', async function () {

0 commit comments

Comments
 (0)