Skip to content

Commit

Permalink
Editing the title of a blog post should not make it 404
Browse files Browse the repository at this point in the history
We found a problem with the site which occurs in the following
situation:

 - Someone creates a new post in prose.io. (That creates a file
   in the repository with a filename baseed on slug of the post
   which is based on its title. The slug is stored in the post's
   YAML frontmatter as well.)

 - Someone edits the title of the post in prose.io. This changes
   the slug which is stored in the YAML frontmatter, but not the
   filename in the repository.)

In our code what was happening is that any list of multiple posts
(e.g. on the front page) would generate links based on the slug
in the YAML frontmatter. However, when looking up an individual
post for display, the slug in the URL is used to form a glob
pattern to find in the appropriate directory in the repository.

This means that changes in the title of a blog post in prose.io
can cause broken links on the site (and we currently don't
automatically deploy if any such broken links are found).

To fix this, this commit changes the slug which is returned by a
MarkdownWithFrontmatter document to be based on its filename, not
the slug found in its frontmatter.

Fixes #166
  • Loading branch information
mhl committed Jun 28, 2017
1 parent d7d9988 commit ce9d5bd
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 4 deletions.
3 changes: 1 addition & 2 deletions lib/document/markdown_with_frontmatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ def body
end

def slug
slug = frontmatter.slug
slug.empty? ? rawname : slug
rawname
end

private
Expand Down
3 changes: 2 additions & 1 deletion tests/document/finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
slug: a-slug
---'
Dir.stub :glob, [new_tempfile(contents, filename)] do
finder.find_single.url.must_equal('/path/a-slug')
expected_slug = File.basename(Dir.glob[0], '.*')
finder.find_single.url.must_equal("/path/#{expected_slug}")
end
end

Expand Down
3 changes: 2 additions & 1 deletion tests/document/markdown_with_frontmatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
end

it 'has a url' do
document.url.must_equal('/events/a-slug')
expected_slug = File.basename(document.send(:filename), '.*').gsub(/^1000-10-01-/, '')
document.url.must_equal("/events/#{expected_slug}")
end

it 'has a published field' do
Expand Down

0 comments on commit ce9d5bd

Please sign in to comment.