Skip to content

Optimize Markdown ToC checker script #886

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

ReNothingg
Copy link

Optimize Markdown ToC checker script

  • Consolidate regex patterns into constants
  • Streamline heading parsing and slug generation
  • Reduce code duplication and simplify logic
  • Enhance comments for clarity
  • Maintain original functionality (check and optional --fix)

Copy link

github-actions bot commented May 10, 2025

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@ReNothingg
Copy link
Author

I have read the CLA Document and I hereby sign the CLA

@ReNothingg
Copy link
Author

recheck

github-actions bot added a commit that referenced this pull request May 10, 2025
@ReNothingg
Copy link
Author

@maintainers Could you please review and approve this PR? The CLA has been signed, and all checks have passed. Thank you!

return 0


def main():
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please reorganize main() as I had it before:

  • Top of file so reader does not have to dig to the end of the file to figure out what arguments this script accepts.
  • As good practice, main() should not call sys.exit() itself. That behavior should be restricted to if __name__ == "__main__"

slug = text.lower()
# normalize spaces and dashes
slug = slug.replace("\u00a0", " ")
level, title = len(m.group(1)), m.group(2).strip()
Copy link
Collaborator

Choose a reason for hiding this comment

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

please keep these as separate lines as before

print(line)
# Show diff
print("ERROR: ToC out of date. Diff:")
for ln in unified_diff(current, expected, fromfile="existing ToC", tofile="generated ToC", lineterm=""):
Copy link
Collaborator

Choose a reason for hiding this comment

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

prefer line to ln

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