Skip to content

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

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

refactor: centralize slugify in utils (#30)#32
clean6378-max-it wants to merge 2 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
    • Export filenames now use a consistent, ASCII-safe slug format to ensure predictable naming.
  • Tests
    • Added regression tests validating slug behavior (hyphenation, punctuation, Unicode handling, empty inputs, digits).

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

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: b99aab7a-1e6f-4890-bfa1-91cea5ba61e7

📥 Commits

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

📒 Files selected for processing (2)
  • api/export_api.py
  • 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, and renames API response keys from lastExportTime/exportedCount to last_export_time/export_count. Adds regression tests for slug behavior.

Changes

Slug Consolidation and API Response Update

Layer / File(s) Summary
Core Utility
utils/slugify.py
Adds slugify(text: str) -> str: lowercases input, replaces runs of non-[a-z0-9] characters with a single hyphen, trims leading/trailing hyphens, ASCII-only behavior.
API Integration
api/export_api.py
Removes local _slugify, imports slugify from utils.slugify, updates bulk_export and export_session to use it; updates get_export_state response keys to last_export_time and export_count.
Script Integration
scripts/export.py
Imports slugify from utils.slugify, replaces local _slugify usages in bulk and single-session export paths, and removes the internal _slugify helper.
Tests
tests/test_slugify.py
Adds regression tests: ASCII word hyphenation, punctuation collapse, Unicode-to-ASCII-safe handling, empty-after-strip behavior, and digit preservation.

Estimated Code Review Effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

A rabbit hops through code at night,
Two slugs conformed to one delight,
Tests planted in a tidy row,
Filenames now sing as they go—
Hooray for shared paths, soft and light! 🐇✨

🚥 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)' directly and concisely summarizes the primary change: consolidating duplicate _slugify implementations into a single utilities module.
Linked Issues check ✅ Passed The PR successfully fulfills all coding requirements from issue #30: consolidates two _slugify implementations into utils/slugify.py, updates all call sites in api/export_api.py and scripts/export.py, adds regression tests in tests/test_slugify.py, and maintains backward compatibility.
Out of Scope Changes check ✅ Passed All changes are directly aligned with issue #30 scope: consolidating _slugify, updating call sites, and adding regression tests. The change to get_export_state response keys (exposing last_export_time and export_count) represents straightforward refactoring without semantic changes to core functionality.

✏️ 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
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
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
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
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