Skip to content

Commit

Permalink
Add extension point for nodes containing eols
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Sep 13, 2020
1 parent 4565651 commit 91c4fd5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 22 deletions.
45 changes: 23 additions & 22 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,15 @@ function fromMarkdown(value, encoding, options) {
// Note this compiler only understand complete buffering, not streaming.
function compiler(options) {
var settings = options || {}

var handlers = configure(
var config = configure(
{
canContainEols: [
'emphasis',
'fragment',
'heading',
'paragraph',
'strong'
],
enter: {
autolink: opener(link),
autolinkProtocol: onenterdata,
Expand Down Expand Up @@ -170,13 +176,13 @@ function compiler(options) {
length = events.length - 1

while (++index < length) {
handler = handlers[events[index][0]]
handler = config[events[index][0]]

if (own.call(handler, events[index][1].type)) {
handler[events[index][1].type].call(
{
stack: stack,
handlers: handlers,
config: config,
enter: enter,
exit: exit,
buffer: buffer,
Expand Down Expand Up @@ -497,13 +503,7 @@ function compiler(options) {
return
}

if (
context.type === 'emphasis' ||
context.type === 'fragment' ||
context.type === 'heading' ||
context.type === 'paragraph' ||
context.type === 'strong'
) {
if (config.canContainEols.indexOf(context.type) !== -1) {
onenterdata.call(this, token)
onexitdata.call(this, token)
}
Expand Down Expand Up @@ -739,29 +739,30 @@ function compiler(options) {
}
}

function configure(handlers, extensions) {
function configure(config, extensions) {
var length = extensions.length
var index = -1

while (++index < length) {
extension(handlers, extensions[index])
extension(config, extensions[index])
}

return handlers
return config
}

function extension(handlers, extension) {
var hook
function extension(config, extension) {
var key
var left
var right
var type

for (hook in extension) {
left = own.call(handlers, hook) ? handlers[hook] : (handlers[hook] = {})
right = extension[hook]
for (key in extension) {
left = own.call(config, key) ? config[key] : (config[key] = {})
right = extension[key]

for (type in right) {
left[type] = right[type]
if (key === 'canContainEols') {
config[key] = [].concat(left, right)
} else {
Object.assign(left, right)
}
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
"complexity": "off",
"guard-for-in": "off",
"unicorn/no-fn-reference-in-iterator": "off",
"unicorn/prefer-includes": "off",
"unicorn/prefer-number-properties": "off",
"unicorn/prefer-optional-catch-binding": "off",
"unicorn/prefer-set-has": "off"
Expand Down
2 changes: 2 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ test('mdast-util-from-markdown', function (t) {
{
// Unknown objects are used, but have no effect.
unknown: undefined,
// `canContainEols` is an array.
canContainEols: 'someType',
enter: {lineEnding: lineEndingAsHardBreakEnter},
exit: {lineEnding: lineEndingAsHardBreakExit}
}
Expand Down

0 comments on commit 91c4fd5

Please sign in to comment.