The Wayback Machine - https://web.archive.org/web/20211228085801/https://github.com/markedjs/marked/issues/1336
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

Catch-all method for Renderer #1336

Open
hansy opened this issue Sep 15, 2018 · 5 comments
Open

Catch-all method for Renderer #1336

hansy opened this issue Sep 15, 2018 · 5 comments

Comments

@hansy
Copy link

@hansy hansy commented Sep 15, 2018

I couldn't find this in the docs, but I was wondering if there was a way to set up some sort of catch-all method for HTML tags that don't match any overwritten renderer methods? For example:

const md = require('marked');
const renderer = new md.Renderer();

renderer.heading = (text, level) => // do something with headings;
renderer.paragraph = text => // do something with paragraphs;

renderer.all = htmlString => // do something with any element not a header or paragraph

md(input, { renderer }, (err, result) => console.log(result);
@hansy hansy changed the title Catch-all method for extesions Catch-all method for extensions Sep 15, 2018
@UziTech
Copy link
Member

@UziTech UziTech commented Sep 16, 2018

No, any method that is not overwritten is assumed to be using the default implementation

@UziTech
Copy link
Member

@UziTech UziTech commented Sep 19, 2018

Perhaps we could add a function that does that. Something like:

const md = require('marked');
const renderer = new md.Renderer();

renderer.all(htmlString => /* set all methods to use this default function */);

// change the methods you want to be different
renderer.heading = (text, level) => // do something with headings;
renderer.paragraph = text => // do something with paragraphs;

md(input, { renderer }, (err, result) => console.log(result);

I would be open to a pull request like that.

@styfle styfle changed the title Catch-all method for extensions Catch-all method for Renderer Sep 19, 2018
@styfle
Copy link
Member

@styfle styfle commented Sep 19, 2018

It's pretty easy to achieve today without an additional feature.

const marked = require('marked');
const renderer = new marked.Renderer();

// Do something for all methods
const all = (str) => { console.log('all', str) };

// Magic to wire-up all methods on the renderer
Object.keys(renderer.__proto__).forEach(p => renderer[p] = all);

// Change the methods you want to be different
renderer.heading = (text, level) => { console.log('heading', text, level) }
renderer.paragraph = text => { console.log('paragraph', text) }

marked(input, { renderer }, (err, result) => console.log(result);

@UziTech UziTech removed the question label Aug 20, 2019
@marco-silva0000
Copy link

@marco-silva0000 marco-silva0000 commented Jul 15, 2020

is this still the best way to implement a catch-all?

@UziTech
Copy link
Member

@UziTech UziTech commented Jul 15, 2020

@UziTech UziTech added this to To Do in vNext via automation Dec 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
vNext
To Do
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants