Flat_Slug.Py

The articles slug is what determines the url to your page. It should be sanitized of special characters that do not work in the browser.

Explicit Slug in Frontmatter

If you explicitly set the slug in the frontmatter of a post, markata will not overwrite it.


---
title: My First Post
slug: /my-post

---

This is my first post it will be at `<markata.config.url>/my-post/`
reguardless of filename.

Automatic Slug Based on Filename

By default the flat_slug plugin will use the stem of your filename, which is the filename without the extension, unless you explicitly set your slug in frontmatter.

  • /pages/my-post.md becomes <markata.config.url>/my-post/
  • /pages/blog/a-blog-post.md becomes <markata.config.url>/a-blog-post/

!! class

FlatSlugConfig class

FlatSlugConfig source


        class FlatSlugConfig(pydantic.BaseModel):
            slugify: bool = True

!! class

Config class

Config source


        class Config(pydantic.BaseModel):
            flat_slug: FlatSlugConfig = FlatSlugConfig()

!! class

FlatSlugPost class

FlatSlugPost source


        class FlatSlugPost(pydantic.BaseModel):
            should_slugify: Optional[bool] = None

            @pydantic.validator("should_slugify", pre=True, always=True)
            def default_slugify(cls: "FlatSlugPost", v: bool, *, values: Dict) -> bool:
                if not v:
                    return cls.markata.config.flat_slug.slugify
                return v

!! function

config_model function

config_model source


        def config_model(markata: Markata) -> None:
            markata.config_models.append(Config)

!! function

post_model function

post_model source


        def post_model(markata: "Markata") -> None:
            markata.post_models.append(FlatSlugPost)

!! function

pre_render function

Sets the article slug if one is not already set in the frontmatter.

pre_render source


        def pre_render(markata: "Markata") -> None:
            """
            Sets the article slug if one is not already set in the frontmatter.
            """
            for article in markata.iter_articles(description="creating slugs"):
                stem = article.get(
                    "slug",
                    Path(article.get("path", article.get("title", ""))).stem,
                )
                if article.should_slugify:
                    article.slug = "/".join([slugify(s) for s in stem.split("/")])
                else:
                    article.slug = stem

!! method

default_slugify method

default_slugify source


        def default_slugify(cls: "FlatSlugPost", v: bool, *, values: Dict) -> bool:
                if not v:
                    return cls.markata.config.flat_slug.slugify
                return v