Skip to content

Enhancement: Context Enrichment Patch for KOReader Anki Plugin #89

@mallomar

Description

@mallomar

Hi @nairyosangha! I've developed a patch for the KOReader Anki plugin that significantly enhances the context information included with vocabulary cards. The patch addresses several limitations in the current implementation and provides users with much richer context for their language learning.

Current vs Enhanced Behavior

Current behavior: Cards include only the surrounding sentence context.

Enhanced behavior: Cards now include:

  • Full book title (properly extracted from document metadata)
  • Current chapter title (mapped from table of contents)
  • Correct reference page number (matching what appears in margins, not display page)

Example output:

"Yesterday she hid her sister's favorite lovie." - Good Inside: A Guide to Becoming the Parent You Want to Be, Chapter 8: Reduce Shame, Increase Connection, Page 84
Image

Key Features

1. Proper Reference Page Extraction

The patch correctly maps KOReader's internal display pages to actual book reference pages using document:getPageMap(). This ensures the page numbers match what users see in margins and correspond to physical book pagination.

2. Accurate Book Title Extraction

Uses multiple fallback methods to get complete book titles:

  • Document metadata properties (document.props.title)
  • Reader document properties (reader_ui.doc_props.title)
  • Intelligent filename parsing with proper cleanup

3. Dynamic Chapter Detection

Leverages KOReader's table of contents system to map current reading position to the appropriate chapter title using position-based matching algorithms.

4. Robust Error Handling

Includes comprehensive fallback mechanisms and detailed logging for troubleshooting, with graceful degradation when certain data isn't available.

Implementation Approach

The patch monkey-patches the AnkiNote:get_word_context() method to intercept and enhance context generation while preserving all existing functionality. It uses KOReader's internal APIs:

  • document:getPageMap() for reference page mapping
  • reader_ui.toc.toc for chapter information
  • UIManager system for accessing active document state

Benefits for Users

  • Better study context: Users can easily identify which book and chapter their vocabulary came from
  • Accurate references: Page numbers match physical books and other reading apps
  • Improved organization: Cards are more meaningful when reviewing outside the original reading context
  • Seamless integration: No changes to existing workflow or card templates needed

The patch has been tested extensively and maintains full backward compatibility with existing Anki card generation while adding these enhancements transparently.

I posted the complete patch code on GitHub Gist. Would you be interested in incorporating these features into the main Anki plugin? I'm happy to collaborate on integration or provide any additional technical details about the implementation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions