Skip to content

Commit 5caa789

Browse files
authored
Reduce risk of publishing posts with wrong date (#1581)
The date of publication often has to be adjusted at the last minute. This can easily be forgotten. In order to reduce the risk of posts with an incorrect date being published, this commit encourages authors to keep a placeholder date until shortly before publication. A CI check prevents the deployment if a placeholder date is detected. The placeholder date "9999/12/31" is chosen because it shows up at the top of the list of posts during development and it's clearly "invalid".
1 parent 85f525f commit 5caa789

File tree

3 files changed

+52
-11
lines changed

3 files changed

+52
-11
lines changed

Diff for: .github/workflows/main.yml

+14-1
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,23 @@ jobs:
4242
with:
4343
path: public
4444

45+
pub_date:
46+
name: Check publication date for placeholder
47+
if: ${{ github.ref == 'refs/heads/master' }}
48+
49+
runs-on: ubuntu-latest
50+
steps:
51+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
52+
53+
- run: rustup override set ${{ env.RUST_VERSION }}
54+
- uses: Swatinem/rust-cache@9d47c6ad4b02e050fd481d890b2ea34778fd09d6 # v2.7.8
55+
56+
- run: cargo test -p front_matter -- --include-ignored date_is_set
57+
4558
deploy:
4659
if: ${{ github.ref == 'refs/heads/master' }}
4760

48-
needs: build
61+
needs: [pub_date, build]
4962

5063
permissions:
5164
pages: write

Diff for: README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,14 @@ You can store your main blog post in `content/<some-slug>/index.md`.
3333
Images go into the same directory: `content/<some-slug>/my_image.png`.
3434
Now you can reference that image with a simple relative path: `![alt text](my_image.png)`.
3535

36+
A post's date of publication is embedded in the `path` key of the front matter.
37+
Unless the exact date is known in advance, keep the placeholder (`9999/12/31`) until the post is about to be published.
38+
Don't worry, there's a CI check to prevent a post with a placeholder date from being deployed.
39+
3640
Here is an example of the front matter format:
3741
```md
3842
+++
39-
path = "2015/03/15/some-slug"
43+
path = "9999/12/31/some-slug"
4044
title = "Title of the blog post"
4145
authors = ["Blog post author (or on behalf of which team)"]
4246
description = "(optional)"

Diff for: front_matter/src/lib.rs

+33-9
Original file line numberDiff line numberDiff line change
@@ -141,15 +141,7 @@ mod tests {
141141

142142
#[test]
143143
fn front_matter_is_normalized() {
144-
let repo_root = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("..");
145-
146-
let posts = fs::read_dir(repo_root.join("content"))
147-
.unwrap()
148-
.chain(fs::read_dir(repo_root.join("content/inside-rust")).unwrap())
149-
.map(|p| p.unwrap().path())
150-
.filter(|p| p.is_file() && p.file_name() != Some("_index.md".as_ref()));
151-
152-
for post in posts {
144+
for post in all_posts() {
153145
let slug = post.file_stem().unwrap().to_str().unwrap();
154146

155147
let inside_rust = post
@@ -212,4 +204,36 @@ The post {post} has abnormal front matter.
212204
};
213205
}
214206
}
207+
208+
/// This test is run by the merge queue check to make sure a blog post isn't
209+
/// merged before its date of publication is set. The date of a blog post
210+
/// is usually a placeholder (path = "9999/12/31/...") until shortly before
211+
/// it's published.
212+
#[test]
213+
#[ignore]
214+
fn date_is_set() {
215+
for post in all_posts() {
216+
let content = fs::read_to_string(&post).unwrap();
217+
let (front_matter, _) = parse(&content).unwrap();
218+
219+
if front_matter.path.starts_with("9999/12/31") {
220+
panic!(
221+
"\n\
222+
The post {slug} has a placeholder publication date.\n\
223+
If you're about to publish it, please set it to today.\n\
224+
",
225+
slug = post.file_stem().unwrap().to_str().unwrap(),
226+
);
227+
}
228+
}
229+
}
230+
231+
fn all_posts() -> impl Iterator<Item = PathBuf> {
232+
let repo_root = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("..");
233+
fs::read_dir(repo_root.join("content"))
234+
.unwrap()
235+
.chain(fs::read_dir(repo_root.join("content/inside-rust")).unwrap())
236+
.map(|p| p.unwrap().path())
237+
.filter(|p| p.is_file() && p.file_name() != Some("_index.md".as_ref()))
238+
}
215239
}

0 commit comments

Comments
 (0)