Skip to content

Commit

Permalink
Add support for lists of extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jun 20, 2021
1 parent 5886d5d commit 46f6d7f
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 5 deletions.
18 changes: 13 additions & 5 deletions dev/lib/index.js
Expand Up @@ -68,7 +68,7 @@
* @property {NormalizedExtension} config
* Configuration.
*
* @typedef {{mdastExtensions?: Array.<Extension>}} FromMarkdownOptions
* @typedef {{mdastExtensions?: Array.<Extension|Array.<Extension>>}} FromMarkdownOptions
* @typedef {ParseOptions & FromMarkdownOptions} Options
*/

Expand Down Expand Up @@ -1048,14 +1048,20 @@ function compiler(options = {}) {

/**
* @param {Extension} combined
* @param {Array.<Extension>} extensions
* @param {Array.<Extension|Array.<Extension>>} extensions
* @returns {Extension}
*/
function configure(combined, extensions) {
let index = -1

while (++index < extensions.length) {
extension(combined, extensions[index])
const value = extensions[index]

if (Array.isArray(value)) {
configure(combined, value)
} else {
extension(combined, value)
}
}

return combined
Expand All @@ -1072,12 +1078,14 @@ function extension(combined, extension) {

for (key in extension) {
if (own.call(extension, key)) {
const list = key === 'canContainEols' || key === 'transforms'
const maybe = own.call(combined, key) ? combined[key] : undefined
const left = maybe || (combined[key] = {})
/* c8 ignore next */
const left = maybe || (combined[key] = list ? [] : {})
const right = extension[key]

if (right) {
if (key === 'canContainEols' || key === 'transforms') {
if (list) {
// @ts-expect-error: `left` is an array.
combined[key] = [...left, ...right]
} else {
Expand Down
49 changes: 49 additions & 0 deletions test/index.js
Expand Up @@ -127,6 +127,55 @@ test('mdast-util-from-markdown', (t) => {
'should support extensions'
)

t.deepEqual(
fromMarkdown('a\nb', {
mdastExtensions: [
[
{
enter: {
lineEnding(token) {
this.enter({type: 'break'}, token)
}
}
},
{
exit: {
lineEnding(token) {
this.exit(token)
}
}
}
]
]
}).children[0].children,
[
{
type: 'text',
value: 'a',
position: {
start: {line: 1, column: 1, offset: 0},
end: {line: 1, column: 2, offset: 1}
}
},
{
type: 'break',
position: {
start: {line: 1, column: 2, offset: 1},
end: {line: 2, column: 1, offset: 2}
}
},
{
type: 'text',
value: 'b',
position: {
start: {line: 2, column: 1, offset: 2},
end: {line: 2, column: 2, offset: 3}
}
}
],
'should support multiple extensions'
)

t.deepEqual(
fromMarkdown('*a*', {
mdastExtensions: [
Expand Down

0 comments on commit 46f6d7f

Please sign in to comment.