Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question about custom renderer #743

Closed
paulocheque opened this issue Apr 20, 2016 · 6 comments
Closed

Question about custom renderer #743

paulocheque opened this issue Apr 20, 2016 · 6 comments

Comments

@paulocheque
Copy link

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.

@paulocheque
Copy link
Author

paulocheque commented Apr 20, 2016

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.

@paulocheque
Copy link
Author

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

@paulocheque
Copy link
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.

@KojiIzawa
Copy link

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

@joshbruce
Copy link
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.

@joshbruce
Copy link
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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants