import re


def name_key_function(name: str) -> str:
    """Order files that start with digits in numeric order so 2 is before 11.

    Replace a run of digit characters at the start of the filename with a
    5 character string padded out with leading zeros.
    Also replace the same if immediately after a slash.
    """
    leading_digits = r"^([0-9]+)[^0-9]*"
    match1 = re.search(pattern=leading_digits, string=name)
    if match1 is not None:
        digits1 = match1.group(1)
        prefix1 = format(int(digits1), "05d")
        name = name.replace(digits1, prefix1)

    slash_leading_digits = r"/([0-9]+)[^0-9]*"
    match2 = re.search(pattern=slash_leading_digits, string=name)
    if match2 is not None:
        digits2 = match2.group(1)
        prefix2 = format(int(digits2), "05d")
        name = name.replace(digits2, prefix2)
    return name