Comments (7)
Yeah, I was using metalsmith-filenames
, but it seems that the layout module overwrites the filename attribute. I didn't want to use metalsmith-in-place
because we have dozens of markdown bio files and I want to keep application logic out of those files.
I did figure it out though! My solution was to create my own plugin that takes the file's name (minus the extension) and writes it to an attribute called slug
. In case anyone is in this situation, here's what I did:
First, I created a plugin at /metalsmith/slugs.js
var path = require('path');
module.exports = function() {
return function(files, metalsmith, callback) {
Object.keys(files).forEach(function(filename){
files[filename].slug = path.parse(filename).name;
});
callback();
};
};
Then, I included it in the CLI object. I'm using metalsmith-grunt
, so this is part of my Gruntfile.js
:
plugins: {
'../../../metalsmith/slugs.js': {},
// ... other plugins
}
@ismay, Thanks for all the help with this. I appreciate the encouragement.
Cheers,
— Braden
from layouts.
Hmm, I see. Just so I have some context, what's the problem you're trying to solve? There might be an easier way.
from layouts.
Sure!
I'm working on a team page for a website. I'm trying to render a folder of personal bios written in markdown format into html pages on the final site. So, for example, there's a file called team/braden-kowitz.md
that needs to be rendered by the layout layouts/layout-bio.html
.
In that layout file, I use frontmatter from the markdown file to pull in info about the person, for example {{name}}
and {{title}}
. I also want to include a photo of the person, which shares the same slug from the filename. So if the file is braden-kowitz.md
, I'd like to point to an image file like /img/team/braden-kowitz.jpg
.
So I need access to the filename in order to link to the profile image correctly. Does that makes sense?
Thanks for the help!
- Braden
from layouts.
So I need access to the filename in order to link to the profile image correctly. Does that makes sense?
Ok I see, that makes sense. I think the easiest thing to do is use metalsmith-collections. I use it myself here and then loop through the generated collection here. I think that should work similar to what you're trying to do, if I'm not mistaken.
It'll probably be easier to do it that way. I am using metalsmith-in-place though (which is pretty similar), so you'll might have to adapt some things if you want to keep using layouts.
Hope that helps!
from layouts.
I'm not sure that metalsmith-collections will help actually. I don't need to render an index page listing the team members. I just need each markdown filename to be accessible while the layout is rendering.
For example, I have a jane-smith.md
file that looks something like this:
---
name: Jane Smith
title: President
---
Jane Smith started her career as a javascript developer.
Now she's president of ACME Corp.
When this markdown file is run through the layout, I'd like to have access to the name jane-smith.md
, but it seems to be overwritten by the name of the layout file. I'm not sure what the proper behavior is here, but it seems like a bug. Shouldn't {{filename}}
match to the file being rendered, not the layout?
Thanks for the help here! The rest of my experience with Metalsmith has been amazing. This is just the one little hiccup that I'm trying to figure out still.
Cheers,
— Braden
from layouts.
I'm not sure that metalsmith-collections will help actually. I don't need to render an index page listing the team members. I just need each markdown filename to be accessible while the layout is rendering.
Yeah ok, so you just want the filename of the markdown file that's currently being rendered into a template. And I assume that you're using metalsmith-filenames
?
Metalsmith filenames makes the filename of each file available as a variable on the object associated with that file. What you're running into is a limitation of metalsmith-layouts
, which only processes variables in the layout, and not from the contents being passed to the layout (the markdown file). Hence, the filename isn't available.
You could use metalsmith-in-place
, and apply templates by extending them, but I don't think you'd be able to use markdown. If I were you I'd switch to metalsmith-in-place
, and use it without the markdown plugin. That way you'll be able to do what you're trying to do here.
There are probably other ways as well, but they'll be a lot more cumbersome and difficult to implement. Hope that helps. If not, an example repo might help illustrate what you're trying to do. Let me know if you have any questions!
from layouts.
@kowitz No problem. happy to help! Thanks for posting the solution 👍
from layouts.
Related Issues (20)
- Cannot include a handlebars layout with a partial HOT 5
- Make errors more descriptive
- Add debug logging
- Update readme and package info
- Version 10 of node.js has been released
- Update after move
- Add suppressNoFilesError flag
- Enable greenkeeper HOT 4
- Improve documentation
- Not working with ECT templates after upgrading from 1.8.1 HOT 3
- Supporting async-only jstransformers HOT 2
- error message missing current file name HOT 1
- From v2.0.0 the metalsmith example static-site is not working HOT 3
- templates should have access to the raw "contents" of other files HOT 5
- Ignoring the output format specified in the jstransformer HOT 5
- Loosen dependency on jstransformers HOT 2
- Align with core plugin setup
- Use metalsmith.match instead of multimatch
- v2.5.0 breaks integration with metalsmith-watch HOT 6
- `pattern` option is not passed in `engineOptions`
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from layouts.