3

I'm attempting to duplicate the original img tag's functionality in custom img tag that will be added to the pagedown converter.

e.g I'm copy the original behavior:

![image_url][1] [1]: http://lolink.com gives <img src="http://lolink.com">

into a custom one:

?[image_url][1] [1]: http://lolink.com gives <img class="lol" src="http://lolink.com">

Looking at the docs the only way to do this is through using the preblockgamut hook and then adding another "block level structure." I attempted doing this and got an Uncaught Error: Recursive call to converter.makeHtml

here's the code of me messing around with it:

    converter.hooks.chain("preBlockGamut", function (text, dosomething) {
        return text.replace(/(\?\[(.*?)\][ ]?(?:\n[ ]*)?\[(.*?)\])()()()()/g, function (whole, inner) {
            return "<img src=" + dosomething(inner) + ">";
        });
    });

I'm not very experienced with hooks and everything so what would I do to fix it? Thanks.

UPDATE: found out that _DoImages runs after prespangamut, will use that instead of preblockgamut

bnynn
  • 501
  • 1
  • 6
  • 20

3 Answers3

2

Figured it out! The solution is very clunky and involves editing the source code because I am very bad at regex and the _DoImage() function uses a lot of internal functions only in the source.

solution:

All edits will be made to the markdown.converter file.

do a ctrl+f for the _DoImage function, you will find that it is named in two places, one in the RunSpanGamut and one defining the function. The solution is simple, copy over the DoImage function and related stuff to a new one in order to mimic the original function and edit it to taste.

next to DoImage function add:

function _DoPotatoImages(text) {
    text = text.replace(/(\?\[(.*?)\][ ]?(?:\n[ ]*)?\[(.*?)\])()()()()/g, writePotatoImageTag);
    text = text.replace(/(\?\[(.*?)\]\s?\([ \t]*()<?(\S+?)>?[ \t]*((['"])(.*?)\6[ \t]*)?\))/g, writePotatoImageTag);
    return text;
}

function writePotatoImageTag(wholeMatch, m1, m2, m3, m4, m5, m6, m7) {
    var whole_match = m1;
    var alt_text = m2;
    var link_id = m3.toLowerCase();
    var url = m4;
    var title = m7;

    if (!title) title = "";

    if (url == "") {
        if (link_id == "") {
            link_id = alt_text.toLowerCase().replace(/ ?\n/g, " ");
        }
        url = "#" + link_id;

        if (g_urls.get(link_id) != undefined) {
            url = g_urls.get(link_id);
            if (g_titles.get(link_id) != undefined) {
                title = g_titles.get(link_id);
            }
        }
        else {
            return whole_match;
        }
    }

    alt_text = escapeCharacters(attributeEncode(alt_text), "*_[]()");
    url = escapeCharacters(url, "*_");
    var result = "<img src=\"" + url + "\" alt=\"" + alt_text + "\"";

    title = attributeEncode(title);
    title = escapeCharacters(title, "*_");
    result += " title=\"" + title + "\"";

    result += " class=\"p\" />";

    return result;
}   

if you look at the difference between the new _DoPotatoImages() function and the original _DoImages(), you will notice I edited the regex to have an escaped question mark \? instead of the normal exclamation mark !

Also notice how the writePotatoImageTag calls g_urls and g_titles which are some of the internal functions that are called.

After that, add your text = _DoPotatoImages(text); to runSpanGamut function (MAKE SURE YOU ADD IT BEFORE THE text = _DoAnchors(text); LINE BECAUSE THAT FUNCTION WILL OVERRIDE IMAGE TAGS) and now you should be able to write ?[image desc](url) along with ![image desc](url)

done.

bnynn
  • 501
  • 1
  • 6
  • 20
0

Thanks for the edit to the main post.

I see what you mean now.

It is a bit weird how it uses empty capture groups to specify tags, but if it works, it works.

It looks like you would need to add on an extra () onto the regex string, then specify m8 as a new extra variable to be passed into the function, and then specify it as class = m8; like the other variables at the top of the function.

Then where it says var result =, instead of class =\"p\" you would just put class + title=\"" + .......

Vasili Syrakis
  • 9,321
  • 1
  • 39
  • 56
0

The full line (not only the regex) in Markdown.Converter.js goes like this:

text = text.replace(/(!\[(.*?)\][ ]?(?:\n[ ]*)?\[(.*?)\])()()()()/g, writeImageTag);

so check the function writeImageTag. There you can see how the regex matching text is replaced with a full img tag.

You can change the almost-last line before its return from

 result += " />";

to

 result += ' class="lol" />';
Jongware
  • 22,200
  • 8
  • 54
  • 100
  • yes but I would like to keep the original image conversion, how would I make a new function that converts markdown into an image tag with a class? – bnynn Dec 30 '13 at 00:55
  • @bnynn: umm, you gonna need to explain that a bit clearer. With my modification the original code does exactly that. – Jongware Dec 30 '13 at 00:57
  • Sorry for misinforming you, what I meant was that I would like to duplicate the `img` tag functionality into another function that uses a different markdown tag. e.g: the original function turns something like `![image][1]` into `` I would like to duplicate that in order to turn something like `?[image][1]` into `` – bnynn Dec 30 '13 at 01:06
  • 1
    @bnynn: then change the first `!` in the regex to `\?`, that's all. – Jongware Dec 30 '13 at 01:09
  • sigh it doesn't seem like I am being clear enough. I will have to rewrite this post. Thank you for your time. – bnynn Dec 30 '13 at 01:15
  • @bnynn: ah -- I think I got it. You need to add a *new* `text.replace` below the current one, with the adjusted regex and calling your modified function. Then your md will accept *both* notations. – Jongware Dec 30 '13 at 01:17
  • yes! thank you, I will make another post however, as this one is riddled with confusion. sorry haha. – bnynn Dec 30 '13 at 01:21
  • Did you end up making a new post? How did you end up solving this issue? – Jeremy Lynch Jan 14 '14 at 08:28
  • Unfortunately I have still not found a solution. I made two new posts, one got two votes and then buried and the [other one](http://stackoverflow.com/questions/21009576/how-to-create-a-custom-img-tag-extension-for-pagedown) is in pretty much the same condition. It seems this is a tough one for everyone! If I do find a solution I will definitely post it. – bnynn Jan 14 '14 at 17:00
  • @Jeremy Richards I just figured it out! currently updating this post with the solution – bnynn Jan 14 '14 at 20:17