Append the star count to bullet entry lines whose first GitHub link has known star data. `format_stars` controls the parenthesized text. Defaults to "{N} GitHub stars". Pass `str` for a bare number.
(
markdown: str,
stars_data: dict[str, dict],
*,
format_stars=None,
)
| 399 | |
| 400 | |
| 401 | def annotate_entries_with_stars( |
| 402 | markdown: str, |
| 403 | stars_data: dict[str, dict], |
| 404 | *, |
| 405 | format_stars=None, |
| 406 | ) -> str: |
| 407 | """Append the star count to bullet entry lines whose first GitHub link has known star data. |
| 408 | |
| 409 | `format_stars` controls the parenthesized text. Defaults to "{N} GitHub stars". |
| 410 | Pass `str` for a bare number. |
| 411 | """ |
| 412 | if format_stars is None: |
| 413 | format_stars = lambda n: f"{n} GitHub stars" # noqa: E731 lambda-assignment |
| 414 | lines = markdown.splitlines(keepends=True) |
| 415 | out: list[str] = [] |
| 416 | for line in lines: |
| 417 | if not BULLET_LINE_RE.match(line): |
| 418 | out.append(line) |
| 419 | continue |
| 420 | annotated = line |
| 421 | for match in MARKDOWN_LINK_RE.finditer(line): |
| 422 | repo_key = extract_github_repo(match.group(1)) |
| 423 | if not repo_key: |
| 424 | continue |
| 425 | entry = stars_data.get(repo_key) |
| 426 | if not entry or "stars" not in entry: |
| 427 | continue |
| 428 | stripped = line.rstrip("\n") |
| 429 | ending = line[len(stripped) :] |
| 430 | annotated = f"{stripped} ({format_stars(entry['stars'])}){ending}" |
| 431 | break |
| 432 | out.append(annotated) |
| 433 | return "".join(out) |
| 434 | |
| 435 | |
| 436 | def remove_sponsors_section(markdown: str) -> str: |