markedjs / marked

A markdown parser and compiler. Built for speed.
https://marked.js.org
Other
32.81k stars 3.38k forks source link

Need advice on extending marked with custom markup #57

Closed puzrin closed 9 years ago

puzrin commented 12 years ago

We need to extend marked with [[reference]] syntax, that woud work in the same way, like highlight (via external function call)

https://github.com/nodeca/ndoc/blob/master/syntax.md#short-links

Could you explain, how to monkeypatch marked, to add this inline tag?

madmuffin1 commented 10 years ago

I am also looking into extending marked.

I am trying to implement a new inline rule, which is supposed to translate anything like [A]something into <span class="chord">A</span>something and came up with this:

marked.InlineLexer.rules.chord = /^\[([^\n\]]+)\]/;
marked.Renderer.prototype.chord = function(text) {
  return '<span class="chord">' + text + '</span>';
};

So I am injecting a new rule into the lexer and prototyped the replace function I am looking for. Unfortunately, this seems not to be enough. What am I missing? Would be grateful for any hints.

EDIT: I haven't thoroughly looked into the code when I posted this, I suspected the rules to act like a chain, but it seems they don't. In order to make this work, some lines need to be added to the InlineLexer.prototype.output function, which is not actually possible without modifying the code. Though I might look into this more later, I want to just note down one finding: the function InlineLexer contains a line this.rules = inline.normal;. I suggest changing it to this.rules = InlineLexer.rules || inline.normal;, so changes made trough the exposed rules are accessible from within marked (as mentioned above, this does not help much though).

octalmage commented 10 years ago

In my app I ended up pre-parsing the markdown using regex to add custom syntax, but I'd love to have this built in!

elennaro commented 9 years ago

+1 very needed feature!

puzrin commented 9 years ago

Closed - not actual anymore. We developped https://github.com/jonschlinkert/remarkable with pluggable syntax.