Skip to content

Commit 91c4fd5

Browse files
committed
Add extension point for nodes containing eols
1 parent 4565651 commit 91c4fd5

File tree

3 files changed

+26
-22
lines changed

3 files changed

+26
-22
lines changed

index.js

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,15 @@ function fromMarkdown(value, encoding, options) {
3434
// Note this compiler only understand complete buffering, not streaming.
3535
function compiler(options) {
3636
var settings = options || {}
37-
38-
var handlers = configure(
37+
var config = configure(
3938
{
39+
canContainEols: [
40+
'emphasis',
41+
'fragment',
42+
'heading',
43+
'paragraph',
44+
'strong'
45+
],
4046
enter: {
4147
autolink: opener(link),
4248
autolinkProtocol: onenterdata,
@@ -170,13 +176,13 @@ function compiler(options) {
170176
length = events.length - 1
171177

172178
while (++index < length) {
173-
handler = handlers[events[index][0]]
179+
handler = config[events[index][0]]
174180

175181
if (own.call(handler, events[index][1].type)) {
176182
handler[events[index][1].type].call(
177183
{
178184
stack: stack,
179-
handlers: handlers,
185+
config: config,
180186
enter: enter,
181187
exit: exit,
182188
buffer: buffer,
@@ -497,13 +503,7 @@ function compiler(options) {
497503
return
498504
}
499505

500-
if (
501-
context.type === 'emphasis' ||
502-
context.type === 'fragment' ||
503-
context.type === 'heading' ||
504-
context.type === 'paragraph' ||
505-
context.type === 'strong'
506-
) {
506+
if (config.canContainEols.indexOf(context.type) !== -1) {
507507
onenterdata.call(this, token)
508508
onexitdata.call(this, token)
509509
}
@@ -739,29 +739,30 @@ function compiler(options) {
739739
}
740740
}
741741

742-
function configure(handlers, extensions) {
742+
function configure(config, extensions) {
743743
var length = extensions.length
744744
var index = -1
745745

746746
while (++index < length) {
747-
extension(handlers, extensions[index])
747+
extension(config, extensions[index])
748748
}
749749

750-
return handlers
750+
return config
751751
}
752752

753-
function extension(handlers, extension) {
754-
var hook
753+
function extension(config, extension) {
754+
var key
755755
var left
756756
var right
757-
var type
758757

759-
for (hook in extension) {
760-
left = own.call(handlers, hook) ? handlers[hook] : (handlers[hook] = {})
761-
right = extension[hook]
758+
for (key in extension) {
759+
left = own.call(config, key) ? config[key] : (config[key] = {})
760+
right = extension[key]
762761

763-
for (type in right) {
764-
left[type] = right[type]
762+
if (key === 'canContainEols') {
763+
config[key] = [].concat(left, right)
764+
} else {
765+
Object.assign(left, right)
765766
}
766767
}
767768
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
"complexity": "off",
8181
"guard-for-in": "off",
8282
"unicorn/no-fn-reference-in-iterator": "off",
83+
"unicorn/prefer-includes": "off",
8384
"unicorn/prefer-number-properties": "off",
8485
"unicorn/prefer-optional-catch-binding": "off",
8586
"unicorn/prefer-set-has": "off"

test/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ test('mdast-util-from-markdown', function (t) {
8282
{
8383
// Unknown objects are used, but have no effect.
8484
unknown: undefined,
85+
// `canContainEols` is an array.
86+
canContainEols: 'someType',
8587
enter: {lineEnding: lineEndingAsHardBreakEnter},
8688
exit: {lineEnding: lineEndingAsHardBreakExit}
8789
}

0 commit comments

Comments
 (0)