Marked: I hope marked can support customing tag,meaning we can make our new lexer rules

Created on 22 May 2014  路  14Comments  路  Source: markedjs/marked

@chjj I search the issues in marked,and found the same quetion like #379 #383 etc
Is that true we can't add new lexer rules and do something we want using marked?But I think it is very important.
I wanna write something like {{{test.html}}} in markdown file,and do something especial about these pattern,is there any way to do this?
I don't know whether am I misunderstanding the issues I mentioned before.If I am right,can marked support this?thanks a lot!

All 14 comments

I would like to do this as well.
Another use case is to find all "at" symbols and link them to the user name like GitHub does.

Example Input:

@chjj is a cool cat

Example output:

<a href="https://example.com/user/chjj">@chjj</a> is a cool cat

Adding the ability for the dev to customize these rules will make the marked plugin very powerful.

@styfle Yeah!It's very important!

@xingqiba0418 @styfle you can make it in this way https://github.com/chjj/marked#overriding-renderer-methods

A rough example:

var rndr = new marked.Renderer();
rndr.paragraph = function(text){
    text = text.replace(/@(\w+)\b/g, function(s, m){
        // console.log(s + " " + m);
        return '<a href="https://example.com/user/' + m + '">' + s + '</a>';
    })
    return marked.Renderer.prototype.paragraph.apply(this, arguments);
}

@t09def Thanks! I didn't think to override paragraph but that makes sense because we don't want to match @name if it is in a code block, only in the paragraph block.

It still seems odd to me that you have to use the prototype to get the renderer to work properly.

the original paragraph function of the prototype is (see https://github.com/chjj/marked/blob/master/lib/marked.js#L814)

Renderer.prototype.paragraph = function(text) {
      return '<p>' + text + '</p>\n';
};

so you can just return '<p>' + text + '</p>' in the end of rndr.paragraph.

since i often code in java, i am accustomed to write super() or ...... apply the prototype function in javascript :smile:

Can i add some class to paragraph, if it contains only one image? It's need to styling images in 100% width on my project.

I still think it would be sensible to have access to the rule blocks for those instance where you want to take precedence on the current parser. For instance, creating new block types.

+1

+1

+1

+1

+1

Closing as out of scope for now. See #956

Was this page helpful?
0 / 5 - 0 ratings

Related issues

james4388 picture james4388  路  3Comments

amejiarosario picture amejiarosario  路  3Comments

FireflyAndStars picture FireflyAndStars  路  3Comments

elennaro picture elennaro  路  4Comments

pigtooter picture pigtooter  路  4Comments