Question

I am using pagedown as a text editor, but I don't want users to be able to embed images in their content (links are ok).

How can I prevent images from displaying? Preferably I don't want pagedown to generate the html for images at all.

Was it helpful?

Solution

To remove the ability to add images with a button, you can comment out a couple sections of Markdown.Editor.js:

To make sure the keyboard shortcut is disabled, comment out the case "g" section below:

...
case "k":
    doClick(buttons.code);
    break;
/*
case "g":
    doClick(buttons.image);
    break;
*/
case "o":
    doClick(buttons.olist);
    break;
...

To eliminate the image button, comment out that section:

...
buttons.code = makeButton("wmd-code-button", getString("code"), "-80px", bindCommand("doCode"));
/* 
buttons.image = makeButton("wmd-image-button", getString("image"), "-100px", bindCommand(function (chunk, postProcessing) {
return this.doLinkOrImage(chunk, postProcessing, true);
}));
*/
makeSpacer(2);
...

A saavy markdown user might know how to enter an image without using the button, however. To catch this, you could do something like the following in Markdown.Converter.js:

Go to the _DoImages function, and replace this line:
text = text.replace(/(!\[(.*?)\][ ]?(?:\n[ ]*)?\[(.*?)\])()()()()/g, writeImageTag);

with something like this one:
text = text.replace(/(!\[(.*?)\][ ]?(?:\n[ ]*)?\[(.*?)\])()()()()/g, "IMAGES NOT SUPPORTED");

And again, this line:
text = text.replace(/(!\[(.*?)\]\s?\([ \t]*()<?(\S+?)>?[ \t]*((['"])(.*?)\6[ \t]*)?\))/g, writeImageTag);

with this:
text = text.replace(/(!\[(.*?)\]\s?\([ \t]*()<?(\S+?)>?[ \t]*((['"])(.*?)\6[ \t]*)?\))/g, "IMAGES NOT SUPPORTED");

Now whenever the converter finds markdown for an image, it'll replace it with "IMAGES NOT SUPPORTED", including in the preview window. This may or may not be exactly what you want, but you could also replace with an empty string, other tag, ...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top