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
Relative Assets with Directory Indexes #818
Relative Assets with Directory Indexes #818
Comments
Hi @geoffreywiseman, would you mind telling us what stuff you've tried? For example, I would expect that |
Sure. My initial test for this were on a blog page:
If I added markdown for the image on the page:
The image would point to image_tagAfter trying the markdown approach, I thought I'd see if it was different using ERB, so I did:
This was still an absolute path to image_tag, relativeAfter looking over the documentation some more, I tried an .html.erb again, this time with:
No effect. After the next session, I could see this wasn't being checked. debuggingThen I ran a debug session using Ruby 1.9 and the debugger gem, using an .html.erb (since I think of an easy way to invoke the debugger from within a markdown page) and traced through the code to see if I could see any way of doing relative links. Although that did show me that url_for existed, it showed me that image_tag didn't use it, and that markdown images go through image_tag. I didn't then try building the image tag myself using url_for, although it's possible that as you're suggesting, it would work. That said, unless I can get that to work in markdown (and I don't see how, unless I do .html.md.erb), then it doesn't change anything for me, I'll just end up doing markdown with absolute links. |
Hm. For reasons I haven't quite yet grokked, url_for seems unavailable to me: == Request: /blog/2013/03/12/debug/index.html
error build/blog/2013/03/12/debug/index.html
undefined method `url_for' for #<Middleman::Application:0x70134897338440>
/Users/geoffrey/<path>/middleman/source/blog/2013-03-12-debug.html.erb:2:in `evaluate_source'
/Users/geoffrey/.rvm/gems/ruby-1.9.3-p392/gems/tilt-1.3.3/lib/tilt/template.rb:209:in `instance_eval'
/Users/geoffrey/.rvm/gems/ruby-1.9.3-p392/gems/tilt-1.3.3/lib/tilt/template.rb:209:in `evaluate_source'
/Users/geoffrey/.rvm/gems/ruby-1.9.3-p392/gems/tilt-1.3.3/lib/tilt/template.rb:144:in `cached_evaluate'
/Users/geoffrey/.rvm/gems/ruby-1.9.3-p392/gems/tilt-1.3.3/lib/tilt/template.rb:127:in `evaluate'/ I can see that it's defined on the 3.0-stable tag, so it must be something to do with how the helper methods are bound and available during the instance_eval, but I don't know that part of the middleman codebase well. |
Hm, are you sure you're using the latest Middleman release? The problem you're having is that
You can see that The problem with just using Does that at least make sense of the current behavior? We always use source paths for helpers and things, because extensions can modify the output paths willy-nilly. By always using source paths, you can do things like decide to start or stop using |
It makes perfect sense. Using Markdown, though, I have no idea how to use relative paths for images.  Again, not ideal, but it works. |
Ah, yes, I was using Middleman 3.0.11 and middleman-blog 3.1.1; updated and url_for works. That presumably doesn't help me with markdown, though. It doesn't look like middleman-blog will let me do /blog/2013-03-12-debug/index.html.md either:
So, if you're using markdown and middleman-blog with article subdirectories, I guess we just have to use absolute URLs? Source PathsI see what you're saying about source paths, which is why it occurred to me to try turning the blog article into an index.html.md version instead. However, you say that url_for needs to use source paths, and yet, I can use "plate.jpg" in url_for and it seems to work? |
Sorry, actually, my solution does not work, and I may have found a bug. As mentionned above, I'm trying to include an image which lies in an article subdirectory:
The problem is that the following HTML code is generated: <img alt="Some picture" width="1039" height="577" src="/posts/some-interesting-post"> Note that the When building with
This error seems pretty logic, as it tries to build all assets in an article subdirectory to the same filename. If it helps, here is my # Automatic image dimensions on image_tag helper
activate :automatic_image_sizes
activate :blog do |blog|
blog.prefix = 'posts'
blog.permalink = ':title'
blog.default_extension = '.md'
blog.layout = 'post'
blog.paginate = true
end
# Directory Indexes
activate :directory_indexes
Time.zone = 'Paris'
# Methods defined in the helpers block are available in templates
# helpers do
# def some_helper
# "Helping"
# end
# end
set :css_dir, 'css'
set :js_dir, 'js'
set :images_dir, 'img'
set :markdown_engine, :redcarpet
set :markdown,
fenced_code_blocks: true,
autolink: true,
smartypants: true,
gh_blockcode: true,
lax_spacing: true
activate :rouge_syntax
# Build-specific configuration
configure :build do
# For example, change the Compass output style for deployment
activate :minify_css
# Minify Javascript on build
activate :minify_javascript
# Minify HTML
activate :minify_html
# Enable cache buster
# activate :cache_buster
# Use relative URLs
activate :relative_assets
# Add asset fingerprinting to avoid cache issues
activate :asset_hash
# Compress PNGs after build
# First: gem install middleman-smusher
# require "middleman-smusher"
# activate :smusher
# Or use a different image path
# set :http_path, "/Content/images/"
end
activate :deploy do |deploy|
deploy.method = :git
deploy.remote = 'gh-pages'
deploy.branch = 'master'
end
activate :livereload I tried to activate Smusher (you never know), but it did not help. Any idea ? Smells like a bug, doesn't it ? |
I don't have that problem, although I don't have the identical configuration to you -- I might try adding a few things that you have in your file to see if I can reproduce (e.g. automatic image sizes). |
Hi there! I have the same issue here (#issuecomment-14838455 for sure). I need to produce something like <dl class="explained">
<dt><img src="image.jpg"/></dt>
<dd>This is a cool picture</dd>
</dl> So I use helper like that: helpers do
def explained_img img, explanation
<<-eos
<dl class="explained">
<dt>#{image_tag(img)}</dt>
<dd>#{explanation}</dd>
</dl>
eos
end
end But it's necessary to call it with full paths, I'm calling it from a markdown source of a blog post: <%= explained_img '/posts/2013-07-16-forest/squirrel.jpeg', 'Fig. 1: Running squirrels' %> My humble solutionUse
The simplest way to get the desired string is '/posts/' + caller[0].split(':').shift().split('/').pop().split('.').shift()
#=> "/posts/2013-07-16-forest" So my helper is def explained_img img, explanation
prefix = '/posts/' + caller[0].split(':').shift().split('/').pop().split('.').shift() + '/'
<<-eos
<dl class="explained">
<dt>#{image_tag(prefix + img)}</dt>
<dd>#{explanation}</dd>
</dl>
eos
end One could think about regexp, paths parsers, path builders, some verifications and etc. Good luck. |
I have this problem too. My workaround is to run my markdown through ERB first: In
It's not ideal but it's better than a poke with a stick. |
Pretty sure In all seriousness, I'll look into writing tests to provide us with a target to fix. |
… paths as well as their source paths. This would fix middleman#818.
This is really not easy to fix. I came up with a patch to At this point I think we should just document that you should link to images by repeating the blog post name (like |
Yeah, I end up repeating the blog post name each time. Do you need a PR with an updated doc ? |
That'd be great. -Ben Sent from my phone On Sep 23, 2013, at 10:06 AM, Michael Baudino notifications@github.com Yeah, I end up repeating the blog post name each time. Do you need a PR — |
…irectory indexes. Fixes middleman/middleman#818
… paths as well as their source paths. This would fix middleman#818.
… paths as well as their source paths. This would fix #818.
The middleman homepage still warns users to use the article subdirectory (Blogging > Article Subdirectory) in relative links, but #1048 seems to fix it — is the warning still required? /ed: Ah, yes — |
After moving the image to the article subdirectory I get an extra (empty)
Anyone else having this problem? Make sure you are looking in the raw page response because the empty alt attribute is removed in the element browser of both Safari and Chrome. |
@erikdahlstrand Looks like a bug. Please file a separate issue. Ideally, include a link to a Github repo with a minimal Middleman project that demonstrates the issue. |
@erikdahlstrand Are you using |
@tdreyno Yes I do. |
geoffreywiseman commentedMar 13, 2013
After doing a bunch of poking around Padrino, asset_url, etc, I have come to the possibly erroneous conclusion that although you can group assets with blog posts using directory indexes (
activate :directory_indexes
), you can't refer to the assets from the blog post without an absolute URL. I find that irritating, although obviously I can make it work.If I'm wrong, let me know how I can do it so I can test it and maybe contribute a documentation patch. If I'm right, I'm not sure what approach you'd like to take -- maybe connecting asset_path to url_for or something else of that nature. In any case, if this isn't currently possible, then I'd like to request it.
The text was updated successfully, but these errors were encountered: