Skip to content

Question about custom renderer #743

Closed

Description

@paulocheque

I would like to add an attribute data-id to EVERY html node generated by marked. Is that possible?

Maybe with var renderer = new marked.Renderer();, but I did not find the way to get ALL nodes.

Activity

  1. paulocheque commented on Apr 20, 2016

    @paulocheque
    Author

    Maybe something like:

    renderer.node = function(nodeAsString) {
       // suppose nodeAsString = '<p>some text <strong data-id="previously defined id">here with children</strong></p>'
       var ID = 1111;
       return '<p data-id="1111">some text <strong data-id="previously defined id">here with children</strong></p>'
    }
    

    ps: check that we would need to add data-ids to all nodes, includings children nodes.

  2. paulocheque commented on Apr 20, 2016

    @paulocheque
    Author

    If I am able to include the data-id attr only for the parent nodes I guess it will be useful too.

  3. paulocheque commented on Apr 21, 2016

    @paulocheque
    Author

    I was able to add this extra attribute but I had to override all methods for each node type.

    If you guys think this feature will be useful for the core, I can create a PR for that.

  4. KojiIzawa commented on Dec 3, 2017

    @KojiIzawa

    Hi @paulocheque,
    I have the same request what you have solved. Could you provide the code in question if available?
    Thanks in advance.

  5. joshbruce commented on Dec 3, 2017

    @joshbruce
    Member

    Hello. See #956

    The current vision for Marked as I see it: A high-speed, low-level library for converting Markdown to HTML and back again.

    Admittedly I don't know enough about how Marked works to explain things in the literal; so, hopefully, this makes sense.

    var marked = require();
    
    var markdown = 'Hello, World!';
    
    var html = marked.toHtml(markdown);
    // result: <p>Hello, World!</p>
    
    var markdown = marked.toMarkdown(html);
    // result: Hello, World!

    According to the Markdown spec, we should be able to process strings containing Markdown and HTML. For security, this should be turned off by default. Therefore, out of the box, any non-markdown would be stripped; which would include <script>.

    Therefore, you could opt-in, and take responsibility for:

    marked.allowMarkup = true;
    
    var mixed = '<p><span data-id="something">Hello</span>, World!</p>\n\nHello again!';
    
    var html = marked.toHtml(mixed);
    // result: <p><span data-id="something">Hello</span>, World!</p><p>Hello again!</p>
    
    var markdown = marked.toMarkdown(html);
    // result: <p><span data-id="something">Hello</span>, World!</p>\n\nHello again!

    Is this an accurate representation of expectation and sticking to the vision statement? Even if the code is wholly incorrect.

  6. joshbruce commented on Apr 13, 2018

    @joshbruce
    Member

    Closing as no follow-up conversation of discussion as of today.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions