Skip to content

[ACTV-280] Improve deck media detection#1252

Draft
abdnh wants to merge 2 commits intomainfrom
ACTV-280
Draft

[ACTV-280] Improve deck media detection#1252
abdnh wants to merge 2 commits intomainfrom
ACTV-280

Conversation

@abdnh
Copy link
Contributor

@abdnh abdnh commented Feb 27, 2026

Related issues

https://ankihub.atlassian.net/browse/ACTV-280

Proposed changes

This updates media extraction methods to match Anki's logic. The most notable improvement is that template scripts are now detected.

How to reproduce

TODO

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly overhauls the system's media detection capabilities to more closely mirror Anki's internal logic. The changes aim to provide a more robust and comprehensive identification of local media files across various components of a deck, including note fields, CSS, and card templates. This ensures that all relevant media assets are correctly recognized and handled, improving the overall reliability of media synchronization and management.

Highlights

  • Improved Media Detection Logic: The core media extraction methods have been updated to align with Anki's native logic, ensuring more accurate detection of local media files within notes and note types.
  • Template Script Detection: A significant enhancement is the ability to now detect media references embedded within template scripts, which was previously overlooked.
  • Refactored Media Utility Functions: The common_utils.py module has been completely rewritten with new, granular functions for extracting media from HTML, AV tags, LaTeX, and underscored references in CSS and templates.
  • Consistent Note Type Context: Media extraction functions now consistently receive the note_type object, allowing for context-aware media detection, such as determining LaTeX SVG preference.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • ankihub/ankihub_client/models.py
    • Updated imports to use new gather_media_names_from_note_field and gather_media_names_from_note_type functions.
    • Modified get_media_names_from_notes_data to pass note_type to get_media_names_from_note_info.
    • Updated get_media_names_from_suggestions to accept and utilize a get_notetype_for_suggestion callable.
    • Refactored get_media_names_from_suggestion to pass note_type to gather_media_names_from_note_field.
    • Replaced get_media_names_from_notetype with a new get_media_names_from_note_type that calls gather_media_names_from_note_type.
    • Introduced get_media_names_from_from_field to wrap gather_media_names_from_note_field.
    • Modified get_media_names_from_note_info to accept and pass note_type to gather_media_names_from_note_field.
    • Removed the internal _get_media_names_from_field function.
  • ankihub/common_utils.py
    • Removed the old local_media_names_from_html function and its associated regexes.
    • Added new imports for hashlib, html, anki.models.NotetypeDict, and anki.utils.strip_html.
    • Introduced a comprehensive set of new regex patterns (HTML_MEDIA_TAGS, AV_TAGS, LATEX, LATEX_NEWLINES, REMOTE_FILENAME, UNDERSCORED_CSS_IMPORTS, UNDERSCORED_REFERENCES) for detailed media detection.
    • Implemented helper functions _decode_entities, _is_local_filename, _extract_html_media_refs, _extract_av_tags, _extract_latex, _strip_html_for_latex, _fname_for_latex, _extract_underscored_css_imports, _extract_underscored_references, and _prefers_svg_latex.
    • Created gather_media_names_from_note_type to extract underscored media filenames from note type CSS and templates.
    • Created gather_media_names_from_note_field to extract local media filenames from field content, including HTML, AV tags, and LaTeX.
  • ankihub/db/db.py
    • Updated import to use gather_media_names_from_note_field instead of local_media_names_from_html.
    • Modified media_names_for_ankihub_deck to select anki_note_type_id and pass the corresponding note type to gather_media_names_from_note_field.
  • ankihub/gui/decks_dialog.py
    • Updated import to use get_media_names_from_note_type.
    • Replaced calls to get_media_names_from_notetype with get_media_names_from_note_type in on_note_type_selected.
  • ankihub/gui/media_sync.py
    • Updated imports to include get_media_names_from_from_field and get_media_names_from_note_type.
    • Refactored _media_referenced_by_notes to iterate through note fields and use get_media_names_from_from_field with the note's type, and to use get_media_names_from_note_type for note type media.
  • ankihub/gui/suggestion_dialog.py
    • Modified _added_new_media to pass note.note_type() to get_media_names_from_note_info when comparing media names.
  • ankihub/main/suggestions.py
    • Updated import to include NotetypeDict.
    • Modified _rename_and_upload_media_for_suggestions to provide a suggestion_note_type_getter callable to get_media_names_from_suggestions for accurate note type context.
  • tests/addon/test_integration.py
    • Updated import to use gather_media_names_from_note_field.
    • Replaced calls to local_media_names_from_html with gather_media_names_from_note_field in media assertion tests, passing a Mock() for the note type.
  • tests/addon/test_unit.py
    • Updated imports to remove NotetypeId from anki.models.
    • Modified test_extracts_media_from_note_fields to pass note_type directly to get_media_names_from_notes_data.
    • Refactored test_extracts_media_from_note_type_templates into test_extracts_underscored_images_and_scripts_from_templates to test the new underscored media detection logic in templates and CSS.
    • Added new test cases for test_extracts_unquoted_attributes, test_decodes_html_entities_in_filenames, and test_extracts_latex_from_note_fields to validate the new media extraction utilities.
  • tests/client/test_client.py
    • Modified test_upload_media_for_suggestion to pass a Mock() object as the note_type argument to get_media_names_from_suggestion.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant