From 8eb7d8f707458d6c03952591a1c4c60596aef4f7 Mon Sep 17 00:00:00 2001 From: balpha Date: Sun, 3 Feb 2013 10:34:04 +0100 Subject: [PATCH] add several advanced plugin hooks to the converter --- Markdown.Converter.js | 44 ++++++++++++++++++++++++++++++++++++++---- demo/browser/demo.css | 6 ++++++ demo/browser/demo.html | 18 +++++++++++++++++ 3 files changed, 64 insertions(+), 4 deletions(-) diff --git a/Markdown.Converter.js b/Markdown.Converter.js index b37fdcc..e8d9a1e 100644 --- a/Markdown.Converter.js +++ b/Markdown.Converter.js @@ -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]) @@ -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: @@ -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); @@ -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: @@ -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, @@ -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); @@ -433,6 +467,8 @@ else // Do hard breaks: text = text.replace(/ +\n/g, "
\n"); + + text = pluginHooks.postSpanGamut(text); return text; } diff --git a/demo/browser/demo.css b/demo/browser/demo.css index acf4f46..d39a1db 100644 --- a/demo/browser/demo.css +++ b/demo/browser/demo.css @@ -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%; diff --git a/demo/browser/demo.html b/demo/browser/demo.html index 51cdcf6..93cb1af 100644 --- a/demo/browser/demo.html +++ b/demo/browser/demo.html @@ -22,6 +22,16 @@ Just plain **Markdown**, except that the input is sanitized: I'm the ghost from the past! + +and that it implements "fenced blockquotes" via a plugin: + +""" +Do it like this: + +1. Have idea. +2. ??? +3. Profit! +"""
@@ -56,7 +66,15 @@