from bs4 import BeautifulSoup


def select_tidref(hard_ref: BeautifulSoup, tiddler_name: str):
    """
    Given the hard-reference BS node coming from an HTML snippet and the name
    of the tiddler the HTML snippet was rendered within, return the string to
    be used as a tiddler reference. This will be the hard-reference if the
    tr-reference div exists and is non-empty, otherwise the tiddler name.
    """
    if hard_ref is not None and hard_ref.get_text().strip():
        return hard_ref.get_text().strip()
    else:
        return tiddler_name