Skip to content
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

Incorrect path generated for index.md files #1485

Closed
iwfor opened this issue Feb 11, 2020 · 4 comments · Fixed by #1490
Closed

Incorrect path generated for index.md files #1485

iwfor opened this issue Feb 11, 2020 · 4 comments · Fixed by #1490

Comments

@iwfor
Copy link

iwfor commented Feb 11, 2020

After enabling markdown in Rules, path/index.md files are incorrectly generated to path/index/index.html path instead of index.html. This is especially problematic when using index.md for the root page of a site.

This patch updates the template for Rules:

--- a/nanoc-cli/lib/nanoc/cli/commands/create-site.rb
+++ b/nanoc-cli/lib/nanoc/cli/commands/create-site.rb
@@ -52,7 +52,7 @@ module Nanoc::CLI::Commands
       #compile '/**/*.md' do
       #  filter :kramdown
       #  layout '/default.*'
-      #  write item.identifier.without_ext + '/index.html'
+      #  write (item.identifier.without_ext + '/index.html').sub(/index\\\/index.html/, 'index.html')
       #end
 
       compile '/**/*' do
@denisdefreyne
Copy link
Member

Yep — this is buggy.

Alternatively (and more verbosely, though arguably easier to read):

if item.identifier =~ '/index.*'
  write ext: 'html'
else
  # We write all items into its own directory with an index.html file,
  # except the root index.* file, which needs to remain at the root –
  # otherwise, /index.md would end up as /index/index.html.
  write item.identifier.without_ext + '/index.html'
end

I added a comment, because I feel that it’s necessary to explain this in more detail. I’ll give this some more thought.

@iwfor
Copy link
Author

iwfor commented Feb 13, 2020

That alternative definitely reads easier than my band-aid, but the condition didn't match. This condition worked:

  if item.identifier.match?(/\/index\..*/)

It looks like this bug also pops up if I put an index.html in a sub-directory (e.g., /images/index.html), and I'm not sure of a clean way to handle both html and md blocks.

The first compile block in Rules handles the case of index.html at the root of the site, so clearly someone noticed this problem before.

@koitsu
Copy link

koitsu commented Jul 13, 2020

@ddfreyne Does this change actually work correctly with Markdown (.md) files? I just began trying out nanoc today (version 4.11.18) and after uncommenting the example Rules entry for Markdown, the result is as follows -- note the file extension in output:

~/nanoc/blog $ ls -l content
total 8
-rw-r--r-- 1 jdc jdc  881 Jul 12 18:01 index.md
-rw-r--r-- 1 jdc jdc 1006 Jul 12 17:56 stylesheet.css
~/nanoc/blog $ nanoc nanoc
Loading site… done
Compiling site…
      create  [0.08s]  output/index.md
      create  [0.00s]  output/stylesheet.css

Site compiled in 0.10s.

If I rename index.md to index.html (ignore the fact it's a Markdown file), the output filename is correct.

@koitsu
Copy link

koitsu commented Jul 13, 2020

What I found worked correctly is below; it's the write ext: 'html' that did it.

compile '/**/*.md' do
  filter :kramdown
  layout '/default.*'

  if item.identifier =~ '**/index.*'
    write ext: 'html'
  else
    write item.identifier.without_ext + '/index.html'
  end
end

Directory layouts and results:

~/nanoc/blog $ find content/ -ls
 25561166      4 drwxr-xr-x   4 jdc      jdc          4096 Jul 12 18:34 content/
 25692253      4 drwxr-xr-x   2 jdc      jdc          4096 Jul 12 18:35 content/bar
 25692254      4 -rw-r--r--   1 jdc      jdc            65 Jul 12 18:35 content/bar/index.html
 25561186      4 -rw-r--r--   1 jdc      jdc           883 Jul 12 18:23 content/index.md
 25692248      4 drwxr-xr-x   2 jdc      jdc          4096 Jul 12 18:23 content/foo
 25692249      4 -rw-r--r--   1 jdc      jdc            52 Jul 12 18:23 content/foo/index.md
 25561174      4 -rw-r--r--   1 jdc      jdc          1006 Jul 12 17:56 content/stylesheet.css
~/nanoc/blog $ rm -fr output
~/nanoc/blog $ nanoc nanoc
Loading site… done
Compiling site…
      create  [0.01s]  output/bar/index.html
      create  [0.08s]  output/index.html
      create  [0.00s]  output/foo/index.html
      create  [0.00s]  output/stylesheet.css

Site compiled in 0.10s.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants