Skip to content

Renderer.html() is not invoked if HTML is not preceded by a newline #1601

Closed
@octogonz

Description

@octogonz

Describe the bug

The renderer option allows a custom renderer to intercept various markdown elements before the output is written. However, these events do not work correctly for HTML markdown expressions:

If HTML markdown is not immediately preceded by a blank line, then the Renderer.html() event is skipped -- even though the content is ultimately rendered as HTML. This is a bug.

To Reproduce

Example input with CORRECT output:

const marked = require('marked');

class CustomRenderer extends marked.Renderer {
  constructor(options) { super(options); }
    html(text) {
      console.log(`html(): ` + JSON.stringify(text));
      return text;
    }
    text(text) {
      console.log(`text(): ` + JSON.stringify(text));
      return text;
    }
}

const input1 = `
HTML Image:

<img alt="MY IMAGE" src="example.png" />
`;

marked(input1, { renderer: new CustomRenderer() });

// OUTPUT:
//
// text(): "HTML Image:"
// html(): "<img alt=\"MY IMAGE\" src=\"example.png\" />\n"

Example input with INCORRECT output:

const marked = require('marked');

class CustomRenderer extends marked.Renderer {
  constructor(options) { super(options); }
    html(text) {
      console.log(`html(): ` + JSON.stringify(text));
      return text;
    }
    text(text) {
      console.log(`text(): ` + JSON.stringify(text));
      return text;
    }
}

// NOTE THERE IS MISSING NEWLINE BEFORE THE <IMG> TAG:
const input2 = `
HTML Image:
<img alt="MY IMAGE" src="example.png" />
`;

marked(input2, { renderer: new CustomRenderer() });

// OUTPUT:
//
// text(): "HTML Image:\n"

Expected behavior

The Renderer.html() event SHOULD be invoked for both of the above inputs. The newline should not matter, because the markup is rendered as HTML either way.

Metadata

Metadata

Assignees

No one assigned

    Labels

    L1 - brokenValid usage causes incorrect output OR a crash AND there is no known workaround for the issuecategory: inline elements

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions