Skip to content

Conversation

@devin-ai-integration
Copy link

feat: Add comprehensive cross-reference resolution system to FernRenderer

Summary

This PR implements a complete cross-reference resolution system for the FernRenderer to achieve parity with the MyST renderer's cross-referencing capabilities. The implementation adds automatic link generation for code references in docstrings, Anchor components for non-heading items, and infrastructure for deep linking support in code blocks.

Key Changes:

  1. Reference Discovery Phase: Added _build_reference_map() that runs during initialization to build a complete mapping of all documented items to their slugs and anchors, enabling efficient cross-reference lookups.

  2. Cross-Reference Resolution: Implemented _resolve_cross_reference() with multiple fallback strategies (exact match, child, sibling, module child) to resolve backtick-enclosed identifiers like `ClassName` or `function_name()` to proper Fern links.

  3. Docstring Processing: Integrated _process_docstring_references() into the docstring formatting pipeline to automatically convert code references in descriptions, parameter docs, return values, and exception descriptions into clickable links.

  4. Anchor Components: Added <Anchor id="..."> components to function, class, property, and data rendering methods to enable proper cross-referencing for items that don't have their own headings.

  5. Deep Linking Infrastructure: Implemented _extract_code_references() to extract identifiers from code blocks and map them to documentation URLs (for Fern's deep linking feature).

Note: The deep linking method (_extract_code_references()) is implemented but not yet integrated into the rendering pipeline - this will need to be added in a follow-up if needed.

Review & Testing Checklist for Human

⚠️ Critical - This PR has not been tested with actual documentation generation. Lint checks pass, but end-to-end functionality needs verification:

  • Test cross-reference resolution: Generate docs from a real Python package and verify that docstring references like `ClassName` actually become clickable links to the correct documentation pages
  • Verify Anchor component syntax: Confirm that Fern supports <Anchor id="..."> syntax and that it creates properly linkable anchors (check Fern docs at https://buildwithfern.com/learn/docs/writing-content/components/anchor)
  • Test link formats: Verify that same-page links use #anchor format and cross-page links use /slug#anchor format correctly
  • Check for broken references: Test with a codebase that has various reference patterns (parent.Child, sibling references, etc.) to ensure resolution logic handles all cases
  • Performance testing: Run on a large codebase (1000+ documented items) to verify that the initialization-time reference map building doesn't cause significant slowdowns

Test Plan Recommendation

  1. Generate docs for a real Python package (e.g., a small internal package or open source project)
  2. Check generated .md files for:
    • Proper <Anchor> components at the top of function/class/property definitions
    • Converted links in docstrings (search for [ `ClassName ] patterns)
    • Verify links point to correct slugs and anchors
  3. Build the Fern documentation and verify links are clickable and navigate correctly
  4. Test edge cases: references to undefined items should remain as plain code

Notes

…erer

Implements a complete cross-reference resolution system to achieve parity with MyST renderer:

1. Reference Mapping/Discovery Phase:
   - Added _build_reference_map() to build a mapping of all documented items to their slugs and anchors
   - Added _page_map to track which page each item belongs to for same-page vs cross-page link determination
   - Runs during initialization to enable efficient cross-reference lookups

2. Cross-Reference Resolution:
   - Added _resolve_cross_reference() to resolve references to documented items
   - Added _create_link_to_item() to generate proper Fern links (same-page anchors or cross-page slug+anchor)
   - Supports exact matches, relative names, children, siblings, and module-level resolution

3. Docstring Reference Processing:
   - Added _process_docstring_references() to convert backtick-enclosed identifiers to Fern links
   - Integrated into _format_docstring_sections() for description, parameters, returns, and raises sections
   - Automatically converts references like `ClassName` or `function_name()` to clickable links

4. Deep Linking Support:
   - Added _extract_code_references() to extract identifiers from code blocks
   - Returns a dict suitable for Fern's deep linking feature: {identifier: url}
   - Filters out Python keywords and builtins

5. Anchor Component Support:
   - Added <Anchor id="..."> components to all render methods (function, class, property, data)
   - Enables proper cross-referencing for items without their own headings
   - Uses consistent anchor generation via _create_anchor()

This implementation follows the strategy discussed in the thread to achieve full parity with MyST renderer's cross-reference capabilities while using Fern's native linking mechanisms (slugs, anchors, deep linking).

Co-Authored-By: [email protected] <[email protected]>
@devin-ai-integration
Copy link
Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

@Ryan-Amirthan Ryan-Amirthan deleted the devin/1762123459-cross-reference-resolution branch November 4, 2025 14:55
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.

2 participants