Skip to main content

inject_permalink_if_missing

Function inject_permalink_if_missing 

Source
pub fn inject_permalink_if_missing(body: &str, permalink: &str) -> String
Expand description

Injects permalink: "<permalink>" as the first key of the YAML frontmatter block when the block exists and declares neither permalink nor url (spec A2/B1, plan §2 item 1.2, issue #586).

Author-specified values always win: a file with either key passes through byte-for-byte. Files without a frontmatter fence are left untouched — the stager’s structural line-scan (shared with the template-default shim) only operates on YAML --- fenced blocks, and staticdatagen extracts no metadata from fence-less files anyway.

Idempotent: a second pass over previously-staged content returns the input unchanged (the injected permalink: is detected as an existing key).

§Examples

use ssg::content_stager::inject_permalink_if_missing;

// Missing both keys — derived permalink lands as the first key.
let out = inject_permalink_if_missing(
    "---\ntitle: T\n---\nbody",
    "https://example.com/t/",
);
assert!(out.contains("permalink: \"https://example.com/t/\""));

// Author-specified permalink wins verbatim.
let with_permalink = "---\npermalink: /mine/\ntitle: T\n---\nbody";
assert_eq!(
    inject_permalink_if_missing(with_permalink, "https://x/"),
    with_permalink
);

// `url` counts as author-specified too.
let with_url = "---\nurl: /u/\ntitle: T\n---\nbody";
assert_eq!(inject_permalink_if_missing(with_url, "https://x/"), with_url);