Skip to content

Commit 8e0233e

Browse files
committed
Update @types/mdast, mdast utilities
1 parent 66fb90e commit 8e0233e

File tree

4 files changed

+102
-63
lines changed

4 files changed

+102
-63
lines changed

index.d.ts

+38-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,27 @@ declare module 'mdast-util-to-markdown' {
159159
// Add nodes to content.
160160
declare module 'mdast' {
161161
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
162-
interface StaticPhrasingContentMap {
162+
interface RootContentMap {
163+
/**
164+
* Directive in flow content (such as in the root document, or block
165+
* quotes), which contains further flow content.
166+
*/
167+
containerDirective: ContainerDirective
168+
169+
/**
170+
* Directive in flow content (such as in the root document, or block
171+
* quotes), which contains nothing.
172+
*/
173+
leafDirective: LeafDirective
174+
175+
/**
176+
* Directive in phrasing content (such as in paragraphs, headings).
177+
*/
178+
textDirective: TextDirective
179+
}
180+
181+
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
182+
interface PhrasingContentMap {
163183
/**
164184
* Directive in phrasing content (such as in paragraphs, headings).
165185
*/
@@ -180,4 +200,21 @@ declare module 'mdast' {
180200
*/
181201
leafDirective: LeafDirective
182202
}
203+
204+
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
205+
interface ParagraphData {
206+
/**
207+
* Field set on the first paragraph which is a child of a container
208+
* directive.
209+
* When this is `true`, that means the paragraph represents the *label*:
210+
*
211+
* ```markdown
212+
* :::a[This is the label]
213+
* This is further things.
214+
* :::
215+
* ```
216+
*/
217+
// eslint-disable-next-line @typescript-eslint/ban-types
218+
directiveLabel?: boolean | null | undefined
219+
}
183220
}

lib/index.js

+16-18
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,7 @@
2121
import {parseEntities} from 'parse-entities'
2222
import {stringifyEntitiesLight} from 'stringify-entities'
2323
import {visitParents} from 'unist-util-visit-parents'
24-
import {containerFlow} from 'mdast-util-to-markdown/lib/util/container-flow.js'
25-
import {containerPhrasing} from 'mdast-util-to-markdown/lib/util/container-phrasing.js'
26-
import {checkQuote} from 'mdast-util-to-markdown/lib/util/check-quote.js'
27-
import {track} from 'mdast-util-to-markdown/lib/util/track.js'
2824

29-
// To do: next major: replace `containerFlow`, `containerPhrasing`, `track`
30-
// with `state` methods.
3125
// To do: next major: expose functions.
3226

3327
const own = {}.hasOwnProperty
@@ -178,7 +172,7 @@ function exitContainerLabel(token) {
178172
* @type {FromMarkdownHandle}
179173
*/
180174
function enterAttributes() {
181-
this.setData('directiveAttributes', [])
175+
this.data.directiveAttributes = []
182176
this.buffer() // Capture EOLs
183177
}
184178

@@ -188,7 +182,7 @@ function enterAttributes() {
188182
*/
189183
function exitAttributeIdValue(token) {
190184
const list = /** @type {Array<[string, string]>} */ (
191-
this.getData('directiveAttributes')
185+
this.data.directiveAttributes
192186
)
193187
list.push([
194188
'id',
@@ -204,7 +198,7 @@ function exitAttributeIdValue(token) {
204198
*/
205199
function exitAttributeClassValue(token) {
206200
const list = /** @type {Array<[string, string]>} */ (
207-
this.getData('directiveAttributes')
201+
this.data.directiveAttributes
208202
)
209203
list.push([
210204
'class',
@@ -220,7 +214,7 @@ function exitAttributeClassValue(token) {
220214
*/
221215
function exitAttributeValue(token) {
222216
const list = /** @type {Array<[string, string]>} */ (
223-
this.getData('directiveAttributes')
217+
this.data.directiveAttributes
224218
)
225219
list[list.length - 1][1] = parseEntities(this.sliceSerialize(token), {
226220
attribute: true
@@ -233,7 +227,7 @@ function exitAttributeValue(token) {
233227
*/
234228
function exitAttributeName(token) {
235229
const list = /** @type {Array<[string, string]>} */ (
236-
this.getData('directiveAttributes')
230+
this.data.directiveAttributes
237231
)
238232

239233
// Attribute names in CommonMark are significantly limited, so character
@@ -247,7 +241,7 @@ function exitAttributeName(token) {
247241
*/
248242
function exitAttributes() {
249243
const list = /** @type {Array<[string, string]>} */ (
250-
this.getData('directiveAttributes')
244+
this.data.directiveAttributes
251245
)
252246
/** @type {Record<string, string>} */
253247
const cleaned = {}
@@ -263,7 +257,7 @@ function exitAttributes() {
263257
}
264258
}
265259

266-
this.setData('directiveAttributes')
260+
this.data.directiveAttributes = undefined
267261
this.resume() // Drop EOLs
268262
const node = /** @type {Directive} */ (this.stack[this.stack.length - 1])
269263
node.attributes = cleaned
@@ -282,7 +276,7 @@ function exit(token) {
282276
* @param {Directive} node
283277
*/
284278
function handleDirective(node, _, state, safeOptions) {
285-
const tracker = track(safeOptions)
279+
const tracker = state.createTracker(safeOptions)
286280
const sequence = fence(node)
287281
const exit = state.enter(node.type)
288282
let value = tracker.move(sequence + (node.name || ''))
@@ -303,7 +297,11 @@ function handleDirective(node, _, state, safeOptions) {
303297
const subexit = state.enter(labelType)
304298
value += tracker.move('[')
305299
value += tracker.move(
306-
containerPhrasing(label, state, {
300+
// @ts-expect-error: `containerPhrasing` is typed correctly, but TS
301+
// generates *hardcoded* types, which means that our dynamically added
302+
// directives are not present.
303+
// At some point, TS should fix that, and `from-markdown` should be fine.
304+
state.containerPhrasing(label, {
307305
...tracker.current(),
308306
before: value,
309307
after: ']'
@@ -326,7 +324,7 @@ function handleDirective(node, _, state, safeOptions) {
326324

327325
if (shallow && shallow.children && shallow.children.length > 0) {
328326
value += tracker.move('\n')
329-
value += tracker.move(containerFlow(shallow, state, tracker.current()))
327+
value += tracker.move(state.containerFlow(shallow, tracker.current()))
330328
}
331329

332330
value += tracker.move('\n' + sequence)
@@ -347,7 +345,7 @@ function peekDirective() {
347345
* @returns {string}
348346
*/
349347
function attributes(node, state) {
350-
const quote = checkQuote(state)
348+
const quote = state.options.quote || '"'
351349
const subset = node.type === 'textDirective' ? [quote] : [quote, '\n', '\r']
352350
const attrs = node.attributes || {}
353351
/** @type {Array<string>} */
@@ -427,7 +425,7 @@ function attributes(node, state) {
427425

428426
/**
429427
* @param {BlockContent | DefinitionContent} node
430-
* @returns {node is Paragraph & {data: {directiveLabel: boolean}}}
428+
* @returns {node is Paragraph & {data: {directiveLabel: true}}}
431429
*/
432430
function inlineDirectiveLabel(node) {
433431
return Boolean(

package.json

+8-9
Original file line numberDiff line numberDiff line change
@@ -37,25 +37,24 @@
3737
"index.js"
3838
],
3939
"dependencies": {
40-
"@types/mdast": "^3.0.0",
41-
"@types/unist": "^2.0.0",
42-
"mdast-util-from-markdown": "^1.3.0",
43-
"mdast-util-to-markdown": "^1.5.0",
40+
"@types/mdast": "^4.0.0",
41+
"@types/unist": "^3.0.0",
42+
"mdast-util-from-markdown": "^2.0.0",
43+
"mdast-util-to-markdown": "^2.0.0",
4444
"parse-entities": "^4.0.0",
4545
"stringify-entities": "^4.0.0",
46-
"unist-util-visit-parents": "^5.0.0"
46+
"unist-util-visit-parents": "^6.0.0"
4747
},
4848
"devDependencies": {
4949
"@types/node": "^20.0.0",
50-
"c8": "^7.0.0",
51-
"mdast-util-from-markdown": "^1.0.0",
52-
"micromark-extension-directive": "^2.0.0",
50+
"c8": "^8.0.0",
51+
"micromark-extension-directive": "^3.0.0",
5352
"prettier": "^2.0.0",
5453
"remark-cli": "^11.0.0",
5554
"remark-preset-wooorm": "^9.0.0",
5655
"type-coverage": "^2.0.0",
5756
"typescript": "^5.0.0",
58-
"unist-util-remove-position": "^4.0.0",
57+
"unist-util-remove-position": "^5.0.0",
5958
"xo": "^0.54.0"
6059
},
6160
"scripts": {

test.js

+40-35
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,15 @@ test('directiveFromMarkdown', () => {
149149
'should support directives (container)'
150150
)
151151

152+
let tree = fromMarkdown(':a[b *c*\nd]', {
153+
extensions: [directive()],
154+
mdastExtensions: [directiveFromMarkdown]
155+
})
156+
157+
removePosition(tree, {force: true})
158+
152159
assert.deepEqual(
153-
removePosition(
154-
fromMarkdown(':a[b *c*\nd]', {
155-
extensions: [directive()],
156-
mdastExtensions: [directiveFromMarkdown]
157-
}),
158-
true
159-
),
160+
tree,
160161
{
161162
type: 'root',
162163
children: [
@@ -180,14 +181,15 @@ test('directiveFromMarkdown', () => {
180181
'should support content in a label'
181182
)
182183

184+
tree = fromMarkdown(':a{#b.c.d e=f g="h&amp;i&unknown;j"}', {
185+
extensions: [directive()],
186+
mdastExtensions: [directiveFromMarkdown]
187+
})
188+
189+
removePosition(tree, {force: true})
190+
183191
assert.deepEqual(
184-
removePosition(
185-
fromMarkdown(':a{#b.c.d e=f g="h&amp;i&unknown;j"}', {
186-
extensions: [directive()],
187-
mdastExtensions: [directiveFromMarkdown]
188-
}),
189-
true
190-
),
192+
tree,
191193
{
192194
type: 'root',
193195
children: [
@@ -207,14 +209,15 @@ test('directiveFromMarkdown', () => {
207209
'should support attributes'
208210
)
209211

212+
tree = fromMarkdown(':a{b=&param c="&param" d=\'&param\'}', {
213+
extensions: [directive()],
214+
mdastExtensions: [directiveFromMarkdown]
215+
})
216+
217+
removePosition(tree, {force: true})
218+
210219
assert.deepEqual(
211-
removePosition(
212-
fromMarkdown(':a{b=&param c="&param" d=\'&param\'}', {
213-
extensions: [directive()],
214-
mdastExtensions: [directiveFromMarkdown]
215-
}),
216-
true
217-
),
220+
tree,
218221
{
219222
type: 'root',
220223
children: [
@@ -234,14 +237,15 @@ test('directiveFromMarkdown', () => {
234237
'should not support non-terminated character references'
235238
)
236239

240+
tree = fromMarkdown(':a{b\nc="d\ne"}', {
241+
extensions: [directive()],
242+
mdastExtensions: [directiveFromMarkdown]
243+
})
244+
245+
removePosition(tree, {force: true})
246+
237247
assert.deepEqual(
238-
removePosition(
239-
fromMarkdown(':a{b\nc="d\ne"}', {
240-
extensions: [directive()],
241-
mdastExtensions: [directiveFromMarkdown]
242-
}),
243-
true
244-
),
248+
tree,
245249
{
246250
type: 'root',
247251
children: [
@@ -261,14 +265,15 @@ test('directiveFromMarkdown', () => {
261265
'should support EOLs in attributes'
262266
)
263267

268+
tree = fromMarkdown('::::a\n:::b\n:c\n:::\n::::', {
269+
extensions: [directive()],
270+
mdastExtensions: [directiveFromMarkdown]
271+
})
272+
273+
removePosition(tree, {force: true})
274+
264275
assert.deepEqual(
265-
removePosition(
266-
fromMarkdown('::::a\n:::b\n:c\n:::\n::::', {
267-
extensions: [directive()],
268-
mdastExtensions: [directiveFromMarkdown]
269-
}),
270-
true
271-
),
276+
tree,
272277
{
273278
type: 'root',
274279
children: [

0 commit comments

Comments
 (0)