简体   繁体   中英

TinyMCE: How do you keep relative URLs when uploading images BUT use absolute URL when using “insert link” option?

TinyMCE version: 4.3.10
Plugin for uploading images: jbimages

I need to store the absolute path of both images and any links added to the form so that the message can be emailed. However, toggling "relative_urls" messes one of those up.

For example:

relative_urls: true
This setting results in proper links. However, any images uploaded via jbimages result in relative links, which is no good for emails. The image gets stored as "images/21312.png". That won't open in an email, since it's missing the domain prefix.

relative_urls: false
This setting results in proper image URLs when uploading via jbimages. However, all other links get prefixed with the Document Root URL. In other words, a link added as "example.org" turns into "www.example.com/example.org", where example.com is my domain root.

The ideal result is for links to be saved as absolute and not prefixed, while uploaded images get prefixed with the domain's URL.

If there is no setting to accommodate the above, are there any other plugins that allow image uploading into the body? I tried drag and drop, but that converts the image into base64 and doesn't always work correctly.

Thanks

I would look at this additional parameter:

https://www.tinymce.com/docs/configure/url-handling/#remove_script_host

Per the docs:

If this option is enabled the protocol and host part of the URLs returned will be removed. This option is only used if the relative_urls option is set to false

If disabled, URLs will be returned in this format: http://www.example.com/somedir/somefile.htm instead of the default format: /somedir/somefile.htm .

The entire topic of URL manipulation is reviewed here: https://www.tinymce.com/docs/configure/url-handling/

If everything is working as you like other than the JBImages plugin I would then ask the plugin creator if there are any settings that can be changed there to address this issue.

With the help of @Michael Fromin, I solved it with a combination of his answer/comment:

I needed the following to be set:

relative_urls: true
remove_script_host: false
urlconverter_callback : 'customURLConverter'

For the customURLConverter function, I used this:

function customURLConverter(url, node, on_save, name)
{
    var checkURL;
    var urlPrefix;

    // Get the first 7 characters of the string
    checkURL = url.substring(0, 7);

    // Determine if those characters are coming from the image uploader
    if(checkURL === "/system")
    {
        // prefix the incoming URL with my domain
        urlPrefix = 'https://www.example.com';
        url = urlPrefix.concat(url);
    }

    // Return URL
    return url;
}

In my use case, the URL from my upload image using jbimages is predictable, so I can check the first X characters from it. There's no reason anyone (in my site) would be linking to a URL that begins with "/system", so I can safely always assume that any URL coming in from TinyMCE that starts with "/system" is an uploaded image.

尝试在配置tinyMCE中添加:

convert_urls : 0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM