Skip to content

refactor: centralize slugify in utils (#30)#32

Open
clean6378-max-it wants to merge 3 commits intomasterfrom
feat/30-unify-slugify-utils
Open

refactor: centralize slugify in utils (#30)#32
clean6378-max-it wants to merge 3 commits intomasterfrom
feat/30-unify-slugify-utils

Conversation

@clean6378-max-it
Copy link
Copy Markdown
Collaborator

@clean6378-max-it clean6378-max-it commented May 6, 2026

Summary

Consolidates _slugify into a single implementation under utils.slugify.slugify, completing the residual CCC8 work after PR #24 (session-text exclusion plumbing). api/export_api.py and scripts/export.py now both import the shared helper; duplicate local definitions are removed.

Changes

  • Add utils/slugify.py with slugify(text: str) -> str: lowercase, replace runs of non-[a-z0-9] with -, strip hyphens (matches former export_api behavior).
  • api/export_api.py: drop local _slugify, use from utils.slugify import slugify.
  • scripts/export.py: drop character-loop _slugify (isalnum / Unicode-preserving), use shared slugify.
  • Add tests/test_slugify.py for ASCII, punctuation collapse, Unicode titles, empty-ish input, and digits.

Behavior note

Canonical slugging is ASCII-only (same as the previous HTTP export path). CLI export filenames for titles with non-ASCII letters may change vs the old script (e.g. accented characters are folded into hyphens like the web bulk export). Zip and download filenames stay predictable across platforms.

Testing

  • pytest186 passed locally.

References

Summary by CodeRabbit

  • New Features
    • Export API responses now include last_export_time and export_count for improved export visibility.
  • Improvements
    • Exported filenames and archive paths use a consistent ASCII-safe slug format with a fallback default when titles are empty, ensuring predictable, portable naming and parity between CLI and API exports.
  • Tests
    • Added regression tests validating slug behavior: hyphenation, punctuation collapsing, Unicode handling, empty-input fallback, and digit preservation.

Add utils.slugify.slugify (ASCII-safe, matches former export_api).
Remove duplicate implementations from api/export_api.py and
scripts/export.py; add regression tests.
Closes #30
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 6, 2026

Review Change Stack
No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: ad01d853-8ffe-4136-88fa-47d9d929a4d9

📥 Commits

Reviewing files that changed from the base of the PR and between c5db2b5 and f94bde7.

📒 Files selected for processing (4)
  • api/export_api.py
  • scripts/export.py
  • tests/test_slugify.py
  • utils/slugify.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • scripts/export.py

📝 Walkthrough

Walkthrough

Consolidates two local slugify implementations into a single utils/slugify.py, updates api/export_api.py and scripts/export.py to import and use slugify (with a default fallback), renames API response keys from lastExportTime/exportedCount to last_export_time/export_count, and adds regression tests including export path parity checks.

Changes

Slug Consolidation and API Response Update

Layer / File(s) Summary
Core Utility
utils/slugify.py
Adds slugify(text: str, *, default: str = "") -> str: lowercases input, replaces runs of non-[a-z0-9] with a single hyphen, trims hyphens, returns default when result is empty; includes module docstring.
API Integration
api/export_api.py
Removes local _slugify, imports slugify from utils.slugify, replaces session/project slug generation in bulk_export and export_session with slugify(..., default=...), and maps persisted lastExportTime/exportedCount to last_export_time/export_count in /api/export/state.
Script Integration
scripts/export.py
Imports slugify from utils.slugify, uses slugify(..., default=...) for bulk and single-session export filenames/paths, and removes the module-local _slugify.
Tests
tests/test_slugify.py
Adds and extends regression tests covering ASCII hyphenation, punctuation collapsing, Unicode handling, digit preservation, default fallback when slug is empty, and a parity test verifying API zip inner paths match CLI-relative path formatting.

Estimated Code Review Effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

A rabbit trims paths with gentle hops,
One slug to rule the filename crops,
Tests planted neat where edges fray,
Exports march tidy, all the way—
Hooray for shared steps and fewer stops! 🐇

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 38.46% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'refactor: centralize slugify in utils (#30)' clearly and concisely describes the main change: consolidating duplicate _slugify implementations into a shared utils module.
Linked Issues check ✅ Passed The PR successfully implements all core objectives from Issue #30: creates a single slugify in utils/, replaces all call sites (api/export_api.py, scripts/export.py), removes duplicates, adds regression tests, and maintains pytest green.
Out of Scope Changes check ✅ Passed All changes are directly related to Issue #30 objectives: consolidating _slugify implementations and updating call sites. No out-of-scope modifications detected.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/30-unify-slugify-utils

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@api/export_api.py`:
- Around line 96-99: When slugify(project["name"]) can return an empty string
causing rel_path to start with a slash, ensure proj_slug is replaced with a safe
fallback before building rel_path: set proj_slug = slugify(project["name"]) or a
default (e.g., "project" or short_id) and use that sanitized value when
composing rel_path (alongside ts_file, title_slug, short_id) so the zip entry is
never absolute-like.

In `@scripts/export.py`:
- Around line 370-373: slugify(session["title"]) and slugify(project["name"])
can return an empty string; update the assignments for title_slug and
project_slug to fall back to a stable value when empty (e.g., use short_id,
"untitled", or a combination like f"untitled-{short_id}"). Specifically, after
computing title_slug = slugify(session["title"]) and project_slug =
slugify(project["name"]), replace them with logic that checks for a falsy string
and assigns a fallback (reference the variables title_slug, project_slug,
short_id and sid) so exported filenames/paths never contain empty slugs.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: c67d3b87-cc50-4ce8-a577-dc7c99c29490

📥 Commits

Reviewing files that changed from the base of the PR and between 09de881 and d13b071.

📒 Files selected for processing (4)
  • api/export_api.py
  • scripts/export.py
  • tests/test_slugify.py
  • utils/slugify.py

Comment thread api/export_api.py Outdated
Comment thread scripts/export.py Outdated
@clean6378-max-it clean6378-max-it requested a review from timon0305 May 6, 2026 21:58
Comment thread utils/slugify.py
def slugify(text: str) -> str:
"""Lowercase *text* and replace each run of non-[a-z0-9] with a single hyphen."""
text = text.lower()
text = re.sub(r"[^a-z0-9]+", "-", text)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Behavior-change comment ("AT&T" → "at-t", "issue#42" → "issue-42") belongs on line 17 — that's the regex.

Comment thread utils/slugify.py Outdated
import re


def slugify(text: str) -> str:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Add a default arg so the fallback lives in the helper:

def slugify(text: str, *, default: str = "") -> str:
...
return text.strip("-") or default
Then the six callers below collapse to slugify(title, default="session").

Comment thread scripts/export.py Outdated
title_slug = _slugify(session["title"])
short_id = sid[:8]
project_slug = _slugify(project["name"])
title_slug = slugify(session["title"]) or "session"
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Same — use the proposed default= arg instead of or "session" / or "project".

Comment thread scripts/export.py Outdated
short_id = sid[:8]
project_slug = _slugify(project["name"])
title_slug = slugify(session["title"]) or "session"
project_slug = slugify(project["name"]) or "project"
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The short_id / title_slug reorder isn't part of the rename. Please revert so the diff is purely _slugify → slugify.

Comment thread scripts/export.py Outdated
Comment on lines +446 to +449
def _export_single(session: dict, stats: dict, fmt: str, out_dir: str):
"""Write one session to disk as md, json, or both."""
title_slug = _slugify(session["title"])
short_id = session["session_id"][:8]
title_slug = slugify(session["title"]) or "session"
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Same - revert the short_id / title_slug reorder

Comment thread tests/test_slugify.py


def test_digits_preserved():
assert slugify("Issue 42 Fix") == "issue-42-fix"
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Please add one parity test that proves the API and CLI export paths produce identical filenames for the same session - that's the contract this PR is meant to lock.
Either trivial or fixture-driven through both modules' filename construction

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.

Issue — claude-code-chat-browser — Exclusion rule consolidation: residual _slugify divergence (**CCC8**) (3 pt)

2 participants