Skip to content

Commit dea5336

Browse files
committed
Add support for position tracking
1 parent 171bcf4 commit dea5336

File tree

2 files changed

+44
-49
lines changed

2 files changed

+44
-49
lines changed

index.js

+43-48
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {visitParents} from 'unist-util-visit-parents'
2121
import {containerFlow} from 'mdast-util-to-markdown/lib/util/container-flow.js'
2222
import {containerPhrasing} from 'mdast-util-to-markdown/lib/util/container-phrasing.js'
2323
import {checkQuote} from 'mdast-util-to-markdown/lib/util/check-quote.js'
24+
import {track} from 'mdast-util-to-markdown/lib/util/track.js'
2425

2526
const own = {}.hasOwnProperty
2627

@@ -217,51 +218,60 @@ function exit(token) {
217218
* @type {ToMarkdownHandle}
218219
* @param {Directive} node
219220
*/
220-
function handleDirective(node, _, context) {
221-
const prefix = fence(node)
221+
function handleDirective(node, _, context, safeOptions) {
222+
const tracker = track(safeOptions)
223+
const sequence = fence(node)
222224
const exit = context.enter(node.type)
223-
let value =
224-
prefix +
225-
(node.name || '') +
226-
label(node, context) +
227-
attributes(node, context)
225+
let value = tracker.move(sequence + (node.name || ''))
226+
/** @type {Directive|Paragraph|undefined} */
227+
let label = node
228228

229229
if (node.type === 'containerDirective') {
230-
const subvalue = content(node, context)
231-
if (subvalue) value += '\n' + subvalue
232-
value += '\n' + prefix
230+
const head = (node.children || [])[0]
231+
label = inlineDirectiveLabel(head) ? head : undefined
233232
}
234233

235-
exit()
236-
return value
237-
}
238-
239-
/** @type {ToMarkdownHandle} */
240-
function peekDirective() {
241-
return ':'
242-
}
234+
if (label && label.children && label.children.length > 0) {
235+
const exit = context.enter('label')
236+
const subexit = context.enter(node.type + 'Label')
237+
value += tracker.move('[')
238+
value += tracker.move(
239+
containerPhrasing(label, context, {
240+
...tracker.current(),
241+
before: value,
242+
after: ']'
243+
})
244+
)
245+
value += tracker.move(']')
246+
subexit()
247+
exit()
248+
}
243249

244-
/**
245-
* @param {Directive} node
246-
* @param {Context} context
247-
* @returns {string}
248-
*/
249-
function label(node, context) {
250-
/** @type {Directive|Paragraph} */
251-
let label = node
250+
value += tracker.move(attributes(node, context))
252251

253252
if (node.type === 'containerDirective') {
254253
const head = (node.children || [])[0]
255-
if (!inlineDirectiveLabel(head)) return ''
256-
label = head
254+
let shallow = node
255+
256+
if (inlineDirectiveLabel(head)) {
257+
shallow = Object.assign({}, node, {children: node.children.slice(1)})
258+
}
259+
260+
if (shallow && shallow.children && shallow.children.length > 0) {
261+
value += tracker.move('\n')
262+
value += tracker.move(containerFlow(shallow, context, tracker.current()))
263+
}
264+
265+
value += tracker.move('\n' + sequence)
257266
}
258267

259-
const exit = context.enter('label')
260-
const subexit = context.enter(node.type + 'Label')
261-
const value = containerPhrasing(label, context, {before: '[', after: ']'})
262-
subexit()
263268
exit()
264-
return value ? '[' + value + ']' : ''
269+
return value
270+
}
271+
272+
/** @type {ToMarkdownHandle} */
273+
function peekDirective() {
274+
return ':'
265275
}
266276

267277
/**
@@ -348,21 +358,6 @@ function attributes(node, context) {
348358
}
349359
}
350360

351-
/**
352-
* @param {ContainerDirective} node
353-
* @param {Context} context
354-
* @returns {string}
355-
*/
356-
function content(node, context) {
357-
const head = (node.children || [])[0]
358-
359-
if (inlineDirectiveLabel(head)) {
360-
node = Object.assign({}, node, {children: node.children.slice(1)})
361-
}
362-
363-
return containerFlow(node, context)
364-
}
365-
366361
/**
367362
* @param {BlockContent} node
368363
* @returns {node is Paragraph & {data: {directiveLabel: boolean}}}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"dependencies": {
3939
"@types/mdast": "^3.0.0",
4040
"@types/unist": "^2.0.0",
41-
"mdast-util-to-markdown": "^1.0.0",
41+
"mdast-util-to-markdown": "^1.3.0",
4242
"parse-entities": "^4.0.0",
4343
"stringify-entities": "^4.0.0",
4444
"unist-util-visit-parents": "^5.0.0"

0 commit comments

Comments
 (0)