阻止 Umbraco/TinyMce 将绝对 URL 转换为相对 URL [英] Stop Umbraco/TinyMce from converting absolute URLs to relative URLs

查看:28
本文介绍了阻止 Umbraco/TinyMce 将绝对 URL 转换为相对 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Umbraco/TinyMce 都喜欢从编辑器中的任何绝对 URL 中剥离基本域.这是有问题的,因为我有一个 RSS 提要可以抓取我的帖子,并每周通过电子邮件将它们发送给订阅者.问题是,如果它们的 URL 是相对的,图像(和链接,但我稍后会担心)显然将不起作用.

Both Umbraco/TinyMce both like to strip the base domain from any absolute URLs in the editor. This is problematic as I have an RSS feed that scrapes my posts, and emails them weekly to subscribers. The issue is, images (and links, but I'll worry about that later) obviously won't work if their URL is relative.

到目前为止,我已经完成了以下工作:

So far I've done the following:

1) 添加到 tiny_mce_src.js:

1) Added to tiny_mce_src.js:

convert_urls : false,
relative_urls : false,
remove_script_host : false,   

与外面的情况相反,对于 Umbraco,convert_urls 必须设置为 false.

Contrary to what's out there, for Umbraco convert_urls must be set to false.

使用媒体内容选择器(或内置 HTML 编辑器),这将保留输入的内容(直到保存/发布..).否则,它会在关闭媒体内容选择器或内置 HTML 编辑器时删除基本域.

Using the media content picker (or built in HTML editor), this will preserve what is typed (until saving/publishing..). Otherwise it strips out the base domain upon closing the media content picker or built in HTML editor.

2) 下一个问题是内容创建者不想弄乱 HTML,所以我需要媒体选择器在选择图片时自动拥有基本域.我在 insertImage.js 中对此进行了硬编码:

2) The next issue is that content creators will not want to mess around with HTML, so I needed the media picker to automatically have the base domain when picking a picture. I hardcoded this in here in insertImage.js:

        if (src.substring(0, umbracoPath.length) == umbracoPath) {
            // if the path contains a reference to the umbraco path, it also contains a reference to go up one level (../)
            src = src.substring(umbracoPath.length + 3, src.length);
        }

        var formObj = document.forms[0];
        formObj.src.value = 'https://example.com' + src;

这会修复所有内容,直到发布和保存.基本上,在这个框架的丛林健身房的某个地方,它会获取当前域和子域(如果适用),并且如果该域在富内容编辑器框中(例如在链接或图像 href/src 中),它将..将其删除.

This fixes everything up until publishing and saving. Basically, somewhere in this jungle gym of a framework it grabs the current domain and subdomain if applicable, and if that domain is in the Rich Content Editor box (such as in a link or image href/src) it will.. strip it out.

解决此问题的方法是在上面的 insertImage.js 代码中使用 https://www.example.com/,并确保您仅通过 https://example.com.

A work around for this to use https://www.example.com/ in the insertImage.js code above, and make sure you're accessing the back office via just https://example.com.

但是,这将不起作用,因为我将有一些内容创建者使用 www,而有些则不会.此外,即使我没有硬编码,而是说如果 www 在当前域中,那么在 insertImage.js 中不是 www(反之亦然),如果员工 A(使用 www)转到员工 B 的旧帖子,最终会导致问题没有(不使用 www)并重新保存 - 它会将其推回相对 URL.

This, however, will not work as I will have some content creators using www and some that will not be. Also even if I didn't hardcode and instead said if www is in current domain then not www (and vice versa) in insertImage.js, it would eventually cause issues if Employee A (using www) went to an old post that Employee B did (not using www) and resaved- It would push it back to a relative URL.

我的问题是:我可以在 Umbraco 框架中哪里查看直接编辑此问题的来源?

My question is: Where can I look in the Umbraco framework to edit the source of this issue directly?

推荐答案

另一种方法是使用解析器、正则表达式或子字符串方法.我也不必担心维护我对 Umbraco 框架所做的任何更改.

An alternative is to use a parser, regex, or substring methods. I also don't have to worry about maintaining any changes I would have made to the Umbraco framework.

在我为 RSS 提要调用此信息的那一刻,我只使用了一个简单的子字符串:

I ended just using a simple substring at the very moment I'm calling this information for the RSS feed:

      var bt = node.GetProperty("bodyText").Value
               .Replace("src=\"/","src=\"https://example.com/")
               .Replace("href=\"/","href=\"https://example.com/");                                   
      @Html.Raw(bt);

任何以正斜杠作为第一个字符的 src 和 href 属性将被替换为基本域名.这非常适合我的设置,但例如,如果您在博客文章中显示代码,则可能会导致问题.

Any src and href attributes with a forward slash as the first character will get replaced with the base domain name. This works perfectly for my setup, but might cause issues if you were displaying code in a blog post, for example.

HTML Agility Pack 之类的解析器包更好,但如果可以,我不需要它用 1 行新代码在技术上解决我的问题.

A parser such as HTML Agility Pack pack is better, but I don't need it if I can solve my issue with technically 1 new line of code.

这篇关于阻止 Umbraco/TinyMce 将绝对 URL 转换为相对 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆