Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: added script to shorten documentation links Fixes #6195 #6265

Open
wants to merge 15 commits into
base: master
Choose a base branch
from

Conversation

vjgaur
Copy link
Contributor

@vjgaur vjgaur commented Oct 28, 2024

Description

Added a script to automatically shorten documentation links and apply it to improve readability of the Polkadot SDK documentation. The script converts long-form documentation links to a shorter format while maintaining the full references at the bottom of each file.

Fixes #6195

Integration

The script is added to substrate/.maintain/ and can be used as follows:

Preview changes:

python3 substrate/.maintain/link-shortener.py --dry-run

Apply changes:

python3 substrate/.maintain/link-shortener.py

// Before:
[crate::reference_docs::blockchain_state_machines]

// After:
blockchain_state_machines

Review Notes

Detailed Implementation Overview

Approach

The script uses a systematic approach to transform documentation links:

  1. File Processing

    • Recursively finds all .rs files in docs/sdk/src
    • Checks each file for presence of crate:: to optimize processing
    • Only modifies files that contain documentation links
  2. Link Pattern Recognition
    PATTERNS = [ r'\[crate::reference_docs::[^]+\]', r'\[crate::polkadot_sdk::[^]`

These patterns target specific documentation sections while avoiding false positives.
3. Link Transformation Process
def replacer(match): # Extract full path: [crate::reference_docs::item] full_path = match.group(0) # Remove brackets: crate::reference_docs::item path_without_brackets = full_path[2:-2] # Get item name: item short_name = path_without_brackets.split("::")[-1] # Create new format return f'[{short_name}]({path_without_brackets})'

  1. Reference Management
    Collects all unique references during processing
    Sorts references alphabetically
    Appends them at the end of the file in a dedicated section
    Maintains original paths for documentation linking

  2. Safety Features
    Dry Run Mode: --dry-run flag shows changes without applying them
    Error Handling: Gracefully handles file reading/writing errors
    Progress Reporting: Shows which files are being processed
    Statistics: Reports number of files processed and modified

  3. Testing
    The script can be tested in multiple ways.
    Dry run on all docs:
    python3 substrate/.maintain/link-shortener.py --dry-run

Test specific directory:
python3 substrate/.maintain/link-shortener.py --path docs/sdk/src/guides --dry-run

Results from Testing

Total files processed: 57
Files modified: 22
Common patterns found and transformed correctly
All references maintained and properly linked
Documentation functionality preserved while improving readability

Implementation details:

Script identifies documentation links using regex patterns
Processes .rs files in docs/sdk/src by default
Maintains all original references while improving readability
Includes dry-run mode for safe testing
Files processed: 57
Files modified: 22
No TODOs remaining.

Checklist

My PR includes a detailed description as outlined in the "Description" and its two subsections above.
My PR follows the labelling requirements (awaiting maintainer labels)
I have made corresponding changes to the documentation (this PR is documentation related)
I have added tests (script includes --dry-run option for testing)

Polkadot Address 👍

polkadot address: 14anwfyV1RYtHZjirHpfjAGaSQTirpYagGi8JaXCyy8fr3So

@@ -135,10 +135,14 @@
//! ```
//! Note: for a parachain which measures time in terms of its own block number, changing block
//! time may cause complications, requiring additional changes. See here more information:
//! [`crate::guides::async_backing_guide#timing-by-block-number`].
//! [`async_backing_guide#timing-by-block-number`](crate::guides::async_backing_guide#timing-by-block-number).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
//! [`async_backing_guide#timing-by-block-number`](crate::guides::async_backing_guide#timing-by-block-number).
//! [`async_backing_guide#timing-by-block-number`].

I think for these final display should be like this, not containing the header.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your suggestion.
I have incorporated those changes.
Let me know if thats fine

@@ -12,7 +12,7 @@
//!
//! > By this step, you have already launched a full Polkadot-SDK-based blockchain!
//!
//! Once done, feel free to step up into one of our templates: [`crate::polkadot_sdk::templates`].
//! Once done, feel free to step up into one of our templates: [`templates`](crate::polkadot_sdk::templates).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
//! Once done, feel free to step up into one of our templates: [`templates`](crate::polkadot_sdk::templates).
//! Once done, feel free to step up into one of our templates: [`templates`](crate::polkadot_sdk::templates).
Suggested change
//! Once done, feel free to step up into one of our templates: [`templates`](crate::polkadot_sdk::templates).
//! Once done, feel free to step up into one of our templates: [`templates`].

So in all of them, you should now be able to remove the part that is inside the ().

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kianenigma
Thanks for your suggestion.
I have incorporated these changes.
Let me know if thats fine or anything else needs to be changed further

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @kianenigma I am trying to get through pr checks for prdoc
I am not sure what should go in crates because this script isn't impacting any crates but the documentation only. Initially I added substrate since it was giving me error later removed it than it asks for crates to be added , Kindly suggest what crates to be added in prdoc
Required { path: "/crates", },

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with the label I have added, it should be good.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @kianenigma

Copy link
Contributor

@kianenigma kianenigma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks neat overall! I've spotted two small issues in the first few ones, will do a final review once they are also fixed.

Please provide your polkadot address for a tip.


// Link References
// [`templates`]: crate::polkadot_sdk::templates
//! Link References
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
//! Link References

And this line doesn't have to be here, because what comes after is not rendered.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done fixed it.

@kianenigma kianenigma added the R0-silent Changes should not be mentioned in any release notes label Nov 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
R0-silent Changes should not be mentioned in any release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

polkadot-sdk-docs: Script to shorten all links.
2 participants