Skip to content
This repository has been archived by the owner on Oct 1, 2020. It is now read-only.

Commit

Permalink
add several advanced plugin hooks to the converter
Browse files Browse the repository at this point in the history
  • Loading branch information
balpha committed Feb 3, 2013
1 parent 0fa8d7a commit 8eb7d8f
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 4 deletions.
44 changes: 40 additions & 4 deletions Markdown.Converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ else
if (original === identity)
this[hookname] = func;
else
this[hookname] = function (x) { return func(original(x)); }
this[hookname] = function (text) {
var args = Array.prototype.slice.call(arguments, 0);
args[0] = original.apply(null, args);
return func.apply(null, args);
};
},
set: function (hookname, func) {
if (!this[hookname])
Expand Down Expand Up @@ -103,9 +107,28 @@ else

Markdown.Converter = function () {
var pluginHooks = this.hooks = new HookCollection();
pluginHooks.addNoop("plainLinkText"); // given a URL that was encountered by itself (without markup), should return the link text that's to be given to this link
pluginHooks.addNoop("preConversion"); // called with the orignal text as given to makeHtml. The result of this plugin hook is the actual markdown source that will be cooked
pluginHooks.addNoop("postConversion"); // called with the final cooked HTML code. The result of this plugin hook is the actual output of makeHtml

// given a URL that was encountered by itself (without markup), should return the link text that's to be given to this link
pluginHooks.addNoop("plainLinkText");

// called with the orignal text as given to makeHtml. The result of this plugin hook is the actual markdown source that will be cooked
pluginHooks.addNoop("preConversion");

// called with the text once all normalizations have been completed (tabs to spaces, line endings, etc.), but before any conversions have
pluginHooks.addNoop("postNormalization");

// Called with the text before / after creating block elements like code blocks and lists. Note that this is called recursively
// with inner content, e.g. it's called with the full text, and then only with the content of a blockquote. The inner
// call will receive outdented text.
pluginHooks.addNoop("preBlockGamut");
pluginHooks.addNoop("postBlockGamut");

// called with the text of a single block element before / after the span-level conversions (bold, code spans, etc.) have been made
pluginHooks.addNoop("preSpanGamut");
pluginHooks.addNoop("postSpanGamut");

// called with the final cooked HTML code. The result of this plugin hook is the actual output of makeHtml
pluginHooks.addNoop("postConversion");

//
// Private state of the converter instance:
Expand Down Expand Up @@ -168,6 +191,8 @@ else
// match consecutive blank lines with /\n+/ instead of something
// contorted like /[ \t]*\n+/ .
text = text.replace(/^[ \t]+$/mg, "");

text = pluginHooks.postNormalization(text);

// Turn block-level HTML blocks into hash entries
text = _HashHTMLBlocks(text);
Expand Down Expand Up @@ -378,12 +403,17 @@ else

return blockText;
}

var blockGamutHookCallback = function (t) { return _RunBlockGamut(t); }

function _RunBlockGamut(text, doNotUnhash) {
//
// These are all the transformations that form block-level
// tags like paragraphs, headers, and list items.
//

text = pluginHooks.preBlockGamut(text, blockGamutHookCallback);

text = _DoHeaders(text);

// Do Horizontal Rules:
Expand All @@ -395,6 +425,8 @@ else
text = _DoLists(text);
text = _DoCodeBlocks(text);
text = _DoBlockQuotes(text);

text = pluginHooks.postBlockGamut(text, blockGamutHookCallback);

// We already ran _HashHTMLBlocks() before, in Markdown(), but that
// was to escape raw HTML in the original Markdown source. This time,
Expand All @@ -412,6 +444,8 @@ else
// tags like paragraphs, headers, and list items.
//

text = pluginHooks.preSpanGamut(text);

text = _DoCodeSpans(text);
text = _EscapeSpecialCharsWithinTagAttributes(text);
text = _EncodeBackslashEscapes(text);
Expand All @@ -433,6 +467,8 @@ else

// Do hard breaks:
text = text.replace(/ +\n/g, " <br>\n");

text = pluginHooks.postSpanGamut(text);

return text;
}
Expand Down
6 changes: 6 additions & 0 deletions demo/browser/demo.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ body
font-family: sans-serif;
}

blockquote {
border-left: 2px dotted #888;
padding-left: 5px;
background: #d0f0ff;
}

.wmd-panel
{
margin-left: 25%;
Expand Down
18 changes: 18 additions & 0 deletions demo/browser/demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@
Just plain **Markdown**, except that the input is sanitized:

<marquee>I'm the ghost from the past!</marquee>

and that it implements "fenced blockquotes" via a plugin:

"""
Do it like this:

1. Have idea.
2. ???
3. Profit!
"""
</textarea>
</div>
<div id="wmd-preview" class="wmd-panel wmd-preview"></div>
Expand Down Expand Up @@ -56,7 +66,15 @@
<script type="text/javascript">
(function () {
var converter1 = Markdown.getSanitizingConverter();

converter1.hooks.chain("preBlockGamut", function (text, rbg) {
return text.replace(/^ {0,3}""" *\n((?:.*?\n)+?) {0,3}""" *$/gm, function (whole, inner) {
return "<blockquote>" + rbg(inner) + "</blockquote>\n";
});
});

var editor1 = new Markdown.Editor(converter1);

editor1.run();

var converter2 = new Markdown.Converter();
Expand Down

0 comments on commit 8eb7d8f

Please sign in to comment.