Hashes an <a> link or an <img> link.

    This function only converts reference link styles:

        [text here][ref id]
        ![alt text here][ref id]

    For inline style links, see hash_inline_links.

    Reference ids can be defined anywhere in the Markdown text.
    Reference ids can also be omitted, in which case te text in the
    first box is used as the reference id:

        [ref id][]

    This is known as an "implicit link" reference.

def hash_reference_links(text, hashes, markdown_obj):
    """Hashes an <a> link or an <img> link.

    This function only converts reference link styles:

        [text here][ref id]
        ![alt text here][ref id]

    For inline style links, see hash_inline_links.

    Reference ids can be defined anywhere in the Markdown text.
    Reference ids can also be omitted, in which case te text in the
    first box is used as the reference id:

        [ref id][]

    This is known as an "implicit link" reference.
    """
    def sub(match):
        is_img = match.group(1) != ''
        content = match.group(2)
        ref = match.group(3).strip().lower()
        if not ref:
            ref = content.strip().lower()
        ref = ref.replace('\n', ' ')
        if ref not in markdown_obj.references:
            link, title = '', ''
        else:
            link, title = markdown_obj.references[ref]
        if title:
            title = ' title="{0}"'.format(title)
        if is_img:
            result = '<img src="{0}" alt="{1}"{2}>'.format(
                    link, content, title)
        else:
            result = '<a href="{0}"{2}>{1}</a>'.format(link,
                    markdown_obj.convert(content).replace('<p>', '').replace('</p>', '').strip(),
                    title)
        hashed = hash_text(result, 'link')
        hashes[hashed] = result
        return hashed
    return re_reference_link.sub(sub, text)