Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4618706
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:18:18+00:00 2026-05-22T02:18:18+00:00

On a website I’m creating I’m using Python-Markdown to format news posts. To avoid

  • 0

On a website I’m creating I’m using Python-Markdown to format news posts. To avoid issues with dead links and HTTP-content-on-HTTPS-page problems I’m requiring editors to upload all images to the site and then embed them (I’m using a markdown editor which I’ve patched to allow easy embedding of those images using standard markdown syntax).

However, I’d like to enforce the no-external-images policy in my code.

One way would be writing a regex to extract image URLs from the markdown sourcecode or even run it through the markdown renderer and use a DOM parser to extract all src attributes from img tags.

However, I’m curious if there’s some way to hook into Python-Markdown to extract all image links or execute custom code (e.g. raising an exception if the link is external) during parsing.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T02:18:18+00:00Added an answer on May 22, 2026 at 2:18 am

    One approach would be to intercept the <img> node at a lower level just after Markdown parses and constructs it:

    import re
    from markdown import Markdown
    from markdown.inlinepatterns import ImagePattern, IMAGE_LINK_RE
    
    RE_REMOTEIMG = re.compile('^(http|https):.+')
    
    class CheckImagePattern(ImagePattern):
    
        def handleMatch(self, m):
            node = ImagePattern.handleMatch(self, m)
            # check 'src' to ensure it is local
            src = node.attrib.get('src')
            if src and RE_REMOTEIMG.match(src):
                print 'ILLEGAL:', m.group(9)
                # or alternately you could raise an error immediately
                # raise ValueError("illegal remote url: %s" % m.group(9))
            return node
    
    DATA = '''
    ![Alt text](/path/to/img.jpg)
    ![Alt text](http://remote.com/path/to/img.jpg)
    '''
    
    mk = Markdown()
    # patch in the customized image pattern matcher with url checking
    mk.inlinePatterns['image_link'] = CheckImagePattern(IMAGE_LINK_RE, mk)
    result = mk.convert(DATA)
    print result
    

    Output:

    ILLEGAL: http://remote.com/path/to/img.jpg
    <p><img alt="Alt text" src="/path/to/img.jpg" />
    <img alt="Alt text" src="http://remote.com/path/to/img.jpg" /></p>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My website is in the root directory, which is /www.lebmotor.com/web/content/ . I am using
Website in reference: http://www.imvu-e.com/products/ht/clients/test/ Notice on the tables how I have a newline separating
My website's root page automatically redirects all users to some subpage via http header.
The website I am developing uses jQuery UI tabs to load content via AJAX
Website: http://ghostpool.com/wordpress/slide/ If you click the + button in the bottom left corner to
Website using .NET Framework v3.5, SQL Server 2008, written in C# I have a
My website has varaible content length, and this sometime means that the footer does
Our website was created with Google Apps. My website URL is http://mydomain.com , which
My website ( http://www.webbuddies.co.za ) works perfectly, but when viewed at 1280x1024 resolution, there's
The website I am creating has a fairly large title text for its banner.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.