Skip to content

Commit 46f6d7f

Browse files
committed
Add support for lists of extensions
1 parent 5886d5d commit 46f6d7f

File tree

2 files changed

+62
-5
lines changed

2 files changed

+62
-5
lines changed

dev/lib/index.js

+13-5
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
* @property {NormalizedExtension} config
6969
* Configuration.
7070
*
71-
* @typedef {{mdastExtensions?: Array.<Extension>}} FromMarkdownOptions
71+
* @typedef {{mdastExtensions?: Array.<Extension|Array.<Extension>>}} FromMarkdownOptions
7272
* @typedef {ParseOptions & FromMarkdownOptions} Options
7373
*/
7474

@@ -1048,14 +1048,20 @@ function compiler(options = {}) {
10481048

10491049
/**
10501050
* @param {Extension} combined
1051-
* @param {Array.<Extension>} extensions
1051+
* @param {Array.<Extension|Array.<Extension>>} extensions
10521052
* @returns {Extension}
10531053
*/
10541054
function configure(combined, extensions) {
10551055
let index = -1
10561056

10571057
while (++index < extensions.length) {
1058-
extension(combined, extensions[index])
1058+
const value = extensions[index]
1059+
1060+
if (Array.isArray(value)) {
1061+
configure(combined, value)
1062+
} else {
1063+
extension(combined, value)
1064+
}
10591065
}
10601066

10611067
return combined
@@ -1072,12 +1078,14 @@ function extension(combined, extension) {
10721078

10731079
for (key in extension) {
10741080
if (own.call(extension, key)) {
1081+
const list = key === 'canContainEols' || key === 'transforms'
10751082
const maybe = own.call(combined, key) ? combined[key] : undefined
1076-
const left = maybe || (combined[key] = {})
1083+
/* c8 ignore next */
1084+
const left = maybe || (combined[key] = list ? [] : {})
10771085
const right = extension[key]
10781086

10791087
if (right) {
1080-
if (key === 'canContainEols' || key === 'transforms') {
1088+
if (list) {
10811089
// @ts-expect-error: `left` is an array.
10821090
combined[key] = [...left, ...right]
10831091
} else {

test/index.js

+49
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,55 @@ test('mdast-util-from-markdown', (t) => {
127127
'should support extensions'
128128
)
129129

130+
t.deepEqual(
131+
fromMarkdown('a\nb', {
132+
mdastExtensions: [
133+
[
134+
{
135+
enter: {
136+
lineEnding(token) {
137+
this.enter({type: 'break'}, token)
138+
}
139+
}
140+
},
141+
{
142+
exit: {
143+
lineEnding(token) {
144+
this.exit(token)
145+
}
146+
}
147+
}
148+
]
149+
]
150+
}).children[0].children,
151+
[
152+
{
153+
type: 'text',
154+
value: 'a',
155+
position: {
156+
start: {line: 1, column: 1, offset: 0},
157+
end: {line: 1, column: 2, offset: 1}
158+
}
159+
},
160+
{
161+
type: 'break',
162+
position: {
163+
start: {line: 1, column: 2, offset: 1},
164+
end: {line: 2, column: 1, offset: 2}
165+
}
166+
},
167+
{
168+
type: 'text',
169+
value: 'b',
170+
position: {
171+
start: {line: 2, column: 1, offset: 2},
172+
end: {line: 2, column: 2, offset: 3}
173+
}
174+
}
175+
],
176+
'should support multiple extensions'
177+
)
178+
130179
t.deepEqual(
131180
fromMarkdown('*a*', {
132181
mdastExtensions: [

0 commit comments

Comments
 (0)