fix: Store#save writes non-default language posts into lang subdirectory
default_path_for now routes posts whose lang differs from default_lang into content_dir/<lang>/<slug>.md, matching the documented multi-language layout and preventing same-slug posts in different languages from overwriting each other.
This commit is contained in:
@@ -97,7 +97,9 @@ module Volumen
|
||||
end
|
||||
|
||||
def default_path_for(post)
|
||||
target = File.join(@content_dir, "#{post.slug}.md")
|
||||
dir = @content_dir
|
||||
dir = File.join(dir, post.lang) if post.lang && post.lang != @default_lang
|
||||
target = File.join(dir, "#{post.slug}.md")
|
||||
unless File.expand_path(target).start_with?(File.expand_path(@content_dir) + File::SEPARATOR)
|
||||
raise ArgumentError, "slug escapes content directory"
|
||||
end
|
||||
|
||||
@@ -55,4 +55,19 @@ class StoreTest < Minitest::Test
|
||||
post = Volumen::Post.new(metadata: { "slug" => "../../etc/passwd" }, body: "x")
|
||||
assert_raises(ArgumentError, "slug escapes content directory") { store.save(post) }
|
||||
end
|
||||
|
||||
def test_save_uses_lang_subdirectory_for_non_default_language
|
||||
store = Volumen::Store.new(@dir, default_lang: "en")
|
||||
post = Volumen::Post.new(metadata: { "slug" => "bonjour", "lang" => "fr" }, body: "x")
|
||||
store.save(post)
|
||||
assert File.exist?(File.join(@dir, "fr", "bonjour.md"))
|
||||
end
|
||||
|
||||
def test_save_uses_root_for_default_language
|
||||
store = Volumen::Store.new(@dir, default_lang: "en")
|
||||
post = Volumen::Post.new(metadata: { "slug" => "hello-new", "lang" => "en" }, body: "x")
|
||||
store.save(post)
|
||||
assert File.exist?(File.join(@dir, "hello-new.md"))
|
||||
refute File.exist?(File.join(@dir, "en", "hello-new.md"))
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user