Skip to content

Reduce risk of publishing posts with wrong date #1581

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,23 @@ jobs:
with:
path: public

pub_date:
name: Check publication date for placeholder
if: ${{ github.ref == 'refs/heads/master' }}

runs-on: ubuntu-latest
steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4

- run: rustup override set ${{ env.RUST_VERSION }}
- uses: Swatinem/rust-cache@9d47c6ad4b02e050fd481d890b2ea34778fd09d6 # v2.7.8

- run: cargo test -p front_matter -- --include-ignored date_is_set

deploy:
if: ${{ github.ref == 'refs/heads/master' }}

needs: build
needs: [pub_date, build]

permissions:
pages: write
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,14 @@ You can store your main blog post in `content/<some-slug>/index.md`.
Images go into the same directory: `content/<some-slug>/my_image.png`.
Now you can reference that image with a simple relative path: `![alt text](my_image.png)`.

A post's date of publication is embedded in the `path` key of the front matter.
Unless the exact date is known in advance, keep the placeholder (`9999/12/31`) until the post is about to be published.
Don't worry, there's a CI check to prevent a post with a placeholder date from being deployed.

Here is an example of the front matter format:
```md
+++
path = "2015/03/15/some-slug"
path = "9999/12/31/some-slug"
title = "Title of the blog post"
authors = ["Blog post author (or on behalf of which team)"]
description = "(optional)"
Expand Down
42 changes: 33 additions & 9 deletions front_matter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,7 @@ mod tests {

#[test]
fn front_matter_is_normalized() {
let repo_root = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("..");

let posts = fs::read_dir(repo_root.join("content"))
.unwrap()
.chain(fs::read_dir(repo_root.join("content/inside-rust")).unwrap())
.map(|p| p.unwrap().path())
.filter(|p| p.is_file() && p.file_name() != Some("_index.md".as_ref()));

for post in posts {
for post in all_posts() {
let slug = post.file_stem().unwrap().to_str().unwrap();

let inside_rust = post
Expand Down Expand Up @@ -212,4 +204,36 @@ The post {post} has abnormal front matter.
};
}
}

/// This test is run by the merge queue check to make sure a blog post isn't
/// merged before its date of publication is set. The date of a blog post
/// is usually a placeholder (path = "9999/12/31/...") until shortly before
/// it's published.
#[test]
#[ignore]
fn date_is_set() {
for post in all_posts() {
let content = fs::read_to_string(&post).unwrap();
let (front_matter, _) = parse(&content).unwrap();

if front_matter.path.starts_with("9999/12/31") {
panic!(
"\n\
The post {slug} has a placeholder publication date.\n\
If you're about to publish it, please set it to today.\n\
",
slug = post.file_stem().unwrap().to_str().unwrap(),
);
}
}
}

fn all_posts() -> impl Iterator<Item = PathBuf> {
let repo_root = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("..");
fs::read_dir(repo_root.join("content"))
.unwrap()
.chain(fs::read_dir(repo_root.join("content/inside-rust")).unwrap())
.map(|p| p.unwrap().path())
.filter(|p| p.is_file() && p.file_name() != Some("_index.md".as_ref()))
}
}