blog_post_dir.ts

A blog's post directories, src/routes/<dirname>/<id>-<slug>: their name grammar, parsing and formatting, validation of a blog's directory listing, lookup by a query, and the next id. Pure, so the build helpers and tests share it.

view source

Declarations
#

7 declarations

BLOG_POST_SLUG_PATTERN
#

blog_post_dir.ts view source

RegExp import {BLOG_POST_SLUG_PATTERN} from '@fuzdev/fuz_blog/blog_post_dir.js';

Matches a post slug: lowercase ASCII letters and digits in hyphen-separated runs.

BlogPostDir
#

find_blog_post_dir
#

blog_post_dir.ts view source

(post_dirs: BlogPostDir[], query: string, blog_dirname: string): BlogPostDir import {find_blog_post_dir} from '@fuzdev/fuz_blog/blog_post_dir.js';

Finds a blog's post by a query naming it: a bare id like 1 matches by id, an <id>-<slug> directory name like 1-hello-world matches that directory exactly, and anything else matching BLOG_POST_SLUG_PATTERN matches by slug. A directory name that matches no post falls back to matching by slug, since a slug may itself look like <id>-<slug> (5-1-x has the slug 1-x). An existing directory name wins over an equal slug of another post. Slugs are not unique across ids, so a slug matching more than one post throws - query by id or directory name instead.

post_dirs

the blog's posts, from resolve_blog_post_dirs

query

the post's id, directory name, or slug

type string

blog_dirname

the blog's dirname, for error messages

type string

returns

BlogPostDir

throws

  • Error - when `query` is none of the three forms or matches no post or more than one

parse_blog_post_dirname
#

blog_post_dir.ts view source

(name: string): BlogPostDir | null import {parse_blog_post_dirname} from '@fuzdev/fuz_blog/blog_post_dir.js';

Parses a post directory name like 1-hello-world into its id and slug. The slug may itself start with digits (1-2024-recap is id 1, slug 2024-recap).

name

type string

returns

BlogPostDir | null

the id and slug, or null when name is not a valid post directory name or its id is beyond the safe integer range

resolve_blog_post_dirs
#

blog_post_dir.ts view source

(names: string[], blog_dirname: string): BlogPostDir[] import {resolve_blog_post_dirs} from '@fuzdev/fuz_blog/blog_post_dir.js';

Validates a blog's post directory names, returning the posts sorted by id. A name starting with a digit claims to be a post and must parse with parse_blog_post_dirname; other names ([post=blog_post_id], a consumer's own routes) are skipped.

names

the names of the subdirectories of a blog's route directory

type string[]

blog_dirname

the blog's dirname, for error messages

type string

returns

BlogPostDir[]

throws

  • Error - on an invalid post directory name or a duplicate id

to_blog_post_dirname
#

to_next_blog_post_id
#

blog_post_dir.ts view source

(post_dirs: BlogPostDir[]): BlogPostId import {to_next_blog_post_id} from '@fuzdev/fuz_blog/blog_post_dir.js';

The id for a new post, one more than the highest existing id, so a deleted post's id is not reused unless it was the highest.

post_dirs

returns

BlogPostId

Depends on
#

Imported by
#