不要在 python markdown 中渲染来自未列出主机的图像

Do not render image from unlisted hosts in python markdown

我使用 Python-Markdown 呈现用户生成的内容。我想将来自外部来源的图片更改为链接。

所以我有一个存储列表:

storages = ['foo.com', 'bar.net']

我需要更换

![](http://external.com/image.png)

类似于:

[http://external.com/image.png](http://external.com/image.png)

如果主机不在存储中。

我尝试在保存到数据库之前编辑降价文本,但这不是一个好的解决方案,因为用户可能想要编辑他的数据并发现数据已被修改。所以我想在渲染时进行替换。

this tutorial:

中演示了您问题的一个解决方案
from markdown.treeprocessors import Treeprocessor
from markdown.extensions import Extension
from urllib.parse import urlparse


class InlineImageProcessor(Treeprocessor):
    def __init__(self, md, hosts):
        self.md = md
        self.hosts = hosts

    def is_unknown_host(self, url):
        url = urlparse(url)
        return url.netloc and url.netloc not in self.hosts

    def run(self, root):
        for element in root.iter('img'):
            attrib = element.attrib
            if self.is_unknown_host(attrib['src']):
                tail = element.tail
                element.clear()
                element.tag = 'a'
                element.set('href', attrib.pop('src'))
                element.text = attrib.pop('alt')
                element.tail = tail
                for k, v in attrib.items():
                    element.set(k, v)


class ImageExtension(Extension):
    def __init__(self, **kwargs):
        self.config = {'hosts' : [[], 'List of approved hosts']}
        super(ImageExtension, self).__init__(**kwargs)

    def extendMarkdown(self, md):
        md.treeprocessors.register(
            InlineImageProcessor(md, hosts=self.getConfig('hosts')),
           'inlineimageprocessor',
           15
        )

正在测试:

>>> import markdown
>>> from image-extension import ImageExtension
>>> input = """
... ![a local image](/path/to/image.jpg)
... 
... ![a remote image](http://example.com/image.jpg)
... 
... ![an excluded remote image](http://exclude.com/image.jpg)
... """
>>> print(markdown.markdown(input, extensions=[ImageExtension(hosts=['example.com'])]))
<p><img alt="a local image" src="/path/to/image.jpg"/></p>
<p><img alt="a remote image" src="http://example.com/image.jpg"/></p>
<p><a href="http://exclude.com/image.jpg">an excluded remote image</a></p>

完全披露:我是 Python-Markdown 的首席开发人员。我们需要另一个教程来演示扩展 API 的一些附加功能。我看到了这个问题并认为它会成为一个很好的候选人。因此,我编写了教程,它逐步完成了开发过程,最终得到了上面的结果。谢谢你的启发。