from pathlib import Path


def href_to_path(found_in: Path, base_dir: Path, href: str) -> Path:
    """Converts:
       - from a link target (aka href) as specified in markdown
       - to an absolute Path on disk.

    found_in: the Path of the file containing the link.
    base_dir: the Path of the base directory of the markdown file tree.
    href: the target href as specified in the markdown link.

    Absolute hrefs (/absolute/path.md) are resolved w.r.t. the base directory.
    Relative hrefs (relative/path.md) are resolved w.r.t. the containing file.
    Empty hrefs are considered the same target as the containing file itself.
    """

    if not href:
        # Markdown link was in the form: [text](#anchor)
        ret = found_in
    else:
        # There may be a better check for absolute href
        abs_ = (href[0:1] == '/')
        if abs_:
            # Link is in the form of [text](/absolute/path.md)
            ret = base_dir.joinpath(href[1:]).resolve()
        else:
            # Link is in the form of [text](relative/path.md)
            ret = found_in.parent.joinpath(href).resolve()

    return ret