Repository navigation
feat(blog): nested categories via parentSlug - #1668
Closed
aka-sacci-ccr wants to merge 1 commit into
Closed
aka-sacci-ccr wants to merge 1 commit into
aka-sacci-ccr wants to merge 1 commit into
Conversation
Categories were flat: a `Category` had no way to say it belongs under
another one, so a site could not express "Recipes > Desserts" and the
listing filter was an exact slug match.
Adds `Category.parentSlug` and a single graph module,
`blog/core/categoryTree.ts`, that every loader shares. Records come from a
decofile a human edits, so a `parentSlug` may point at nothing, at itself,
or around a cycle; every walk is bounded twice, by a visited set and by
MAX_CATEGORY_DEPTH, and truncates instead of throwing or hanging.
- BlogpostListing/BlogpostList accept a category path ("parent/child") and
expand it into the whole subtree, so a parent lists its children's posts.
- BlogpostListing returns `categoryPath` and BlogPostPage returns
`categories`, both the ancestor chain, root first.
- Canonical URLs are rewritten with the real chain. A request that reaches a
child through a stale or wrong path still renders and points at the one
canonical URL, so Google consolidates instead of 404ing or seeing
duplicates. The BreadcrumbList builder is unchanged: it already derives
crumbs from the pathname and resolves names by slug.
- GetCategories gains `parentSlug` to list direct children.
filterPostsByCategory now takes a slug or a list of them, which makes
filterRelatedPosts an alias and removes the Array branch of filterPosts —
the list case gains term filtering for free.
Sites must route the listing as a catch-all (/blog/:category*) for
subcategory URLs to match; mod.ts documents it.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Contributor
Tagging OptionsShould a new tag be published when this PR is merged?
|
Contributor
|
Navigate logical layers of code changes, visualize relationships, and explore their blast radius. Note Currently processing new changes in this PR. This may take a few minutes, please wait... ⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Advanced Run ID: 📒 Files selected for processing (12)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Context
Blog categories were flat.
Categoryhad no way to say it belongs under another one, so a site could not express Recipes > Desserts, and the listing filter was an exact slug match.Model
Category.parentSlug?: string— one field, no new records, no nested object in the decofile (which would allow literal recursion in the JSON).Chosen over a
children: string[]array because the breadcrumb needs a parent walk either way, and only a parent ref makes the trail unambiguous: a child has exactly one parent, and editing a child never touches the parent's record.Guards
Categories come from a decofile a human edits, so
parentSlugmay point at nothing, at itself, or around a cycle. All graph logic lives in one module,blog/core/categoryTree.ts, and every walk is bounded twice — by a visitedSetand byMAX_CATEGORY_DEPTH(4). A malformed record truncates the chain instead of throwing or hanging a request.Covered by
blog/tests/categoryTree.test.ts: cycle, self-reference, dangling parent, over-deep chain, duplicated slugs, descendant walk over a cycle.Behaviour
BlogpostListing/BlogpostListaccept a category path ("parent/child") and expand the leaf into its whole subtree, so a parent lists its children's posts.BlogpostListingreturnscategoryPath,BlogPostPagereturnscategories— both the ancestor chain, root first, for the UI breadcrumb.GetCategoriesgainsparentSlugto list direct children (menus).SEO
blog/utils/jsonLD.tsis unchanged. It already derives crumbs from the pathname and resolves names by slug; now that the canonical carries the full chain andSeoBlogPostpasses the ancestor chain, the breadcrumb comes outBlog > Parent > Child > Postwith sequentialpositionand only the last item withoutitem.articleSectionuntouched.Incidental cleanup
filterPostsByCategorynow takes a slug or a list of them. That makesfilterRelatedPostsan alias and removes theArray.isArraybranch offilterPosts— net negative diff, and the list case gains term filtering for free.loadCategoryBySlug, which re-read the whole collection a second time, is gone.Required on the site side
Route the listing as a catch-all (
/blog/:category*, post/blog/:category*/:slug) for subcategory URLs to match.mod.tsdocuments it andTemplate.tsxalready builds the preview URL with the full path.Verification
deno test -A blog/tests/— 44 passed.deno task checkclean.End to end: create
parentandchild(parentSlug: "parent"), publish a post inchild, then check (a)/blog/parentlists the child's post, (b)/blog/parent/childhas its own canonical, (c) the post's JSON-LDBreadcrumbListhas 4 items in order.🤖 Generated with Claude Code
Summary by cubic
Adds
Category.parentSlugso blog categories can nest ("Recipes > Desserts" instead of flat), and makes category listings accept a full path (parent/child) and expand into the subtree. All category-graph logic is in one module,blog/core/categoryTree.ts, with walks bounded by a visited set and a depth ceiling — cycles, self-references, and dangling parents truncate instead of hanging.Behavior
BlogpostListingandBlogpostListexpand a category path into the leaf's whole subtree, so a parent lists its children's posts.BlogpostListingreturnscategoryPathandBlogPostPagereturnscategories(the ancestor chain, root first) for breadcrumbs; JSON-LD breadcrumbs are unaffected.GetCategoriesgainsparentSlugto list direct children.filterPostsByCategorynow accepts a slug or slug list;filterRelatedPostsbecomes an alias and list filtering gains term filtering for free.Migration
/blog/:category*and/blog/:category*/:slug.Written for commit 2cc3c46. Summary will update on new commits.