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

[Question] - Blog Recipe - Slugs / Urls #612

Closed
dazinator opened this issue Jan 22, 2018 · 9 comments

Comments

Projects
None yet
2 participants
@dazinator
Copy link

commented Jan 22, 2018

I am new to Wyam, I have read the docs (thanks for those!)

I am using the blogs recipe.

I am migrating my existing blog site (hugo based) and would like to keep the same URL's for my posts.

Wyam is currently generating the following format url for my posts:

/posts/YYYY/MM/[file name]

I would like to generate the following to keep compatibility:

/blog/YYYY/MM/DD/[slug OR filename]

[slug] is something I specify in the front matter of a post:

slug: my-amazing-post

If there is no slug then falling back to the filename is fine - however I need to remove the date portion from the filename.

Here is an example of what I want to achieve:

Post File Path Output URL
/posts/2017-12-01-my-post.md /blog/2017/12/01/my-post

Settings[BlogKeys.IncludeDateInPostPath] = true; took me forward a step. It made Wyam include the year and the month in the URL path but sadly not the day. It also didn't remove the date portion from the start of the filename either which is something I'd like. This is what wyam outputs with that setting:

/posts/2017/12/2017-12-01-my-post

So it still leaves me with a way to go, and I think I probably need to take more control of this somehow, but I am not sure how to go about that :-)

Thanks

@dazinator dazinator changed the title Slugs [Question] - Slugs Jan 22, 2018

@dazinator dazinator changed the title [Question] - Slugs [Question] - Blog Recipe - Slugs / Urls Jan 22, 2018

@dazinator dazinator referenced this issue Jan 22, 2018

Closed

Convert to wyam #8

2 of 2 tasks complete
@daveaglick

This comment has been minimized.

Copy link
Member

commented Jan 23, 2018

This is definitely possible. Basically it’ll require replacing the pipeline in the recipe that writes files to use whatever custom logic you want. Gimme another day or two to dig up an example for you (ping me again if I forget).

@dazinator

This comment has been minimized.

Copy link
Author

commented Jan 28, 2018

@daveaglick - I am eagerly anticipating this when you manage to find time :-)

@daveaglick

This comment has been minimized.

Copy link
Member

commented Feb 7, 2018

@dazinator Something like this should do the trick (totally untested). This adds a new module to overwrite the RelativeFilePath metadata with your own path. You might need to tweak it to get exactly what you want, but hopefully this gets you most of the way there.

Pipelines[Blog.BlogPost].Append(
  Meta(Keys.RelativeFilePath, (doc, ctx) =>
  {
    DateTime published = doc.Get<DateTime>(BlogKeys.Published);
    string fileName = doc.Bool("FrontMatterPublished")
      ? doc.FilePath(Keys.SourceFileName).ChangeExtension("html").FullPath
      : doc.FilePath(Keys.SourceFileName).ChangeExtension("html").FullPath.Substring(11);
    return $"{ctx.DirectoryPath(BlogKeys.PostsPath).FullPath}/{published:yyyy}/{published:MM}/{published:DD}/{fileName}"
    }));
@dazinator

This comment has been minimized.

Copy link
Author

commented Feb 8, 2018

Perfect, thanks for showing me how its done. I will tweak as necessary.

@dazinator

This comment has been minimized.

Copy link
Author

commented May 29, 2018

Just trying this out.

Pipelines[Blog.BlogPost] doesn't seem to work.

1 errors compiling configuration:
Line 16: CS0117: 'Blog' does not contain a definition for 'BlogPost'

I am on 1.4.1.

@dazinator

This comment has been minimized.

Copy link
Author

commented May 29, 2018

Had a search to see if I can find the name of the pipelines for the blog recipe. I found this page:
https://wyam.io/recipes/blog/pipelines
But "blogpost" isn't listed there

@dazinator

This comment has been minimized.

Copy link
Author

commented May 29, 2018

Ok as it's just c# - adding this revealed the pipeline keys:

Console.WriteLine(String.Join(",", Pipelines.Keys.ToArray()));

Which are:

Pages,BlogPosts,Tags,TagIndex,BlogArchive,Index,Feed,RenderBlogPosts,RenderPages,Redirects,Less,Sass,Resources,ValidateLinks

Should I submit a PR to that docs page?

@dazinator

This comment has been minimized.

Copy link
Author

commented Jun 15, 2018

@daveaglick I can confirm, using your snippet above as a basis, I now have URL paths of blogs matching,

Here is the wyam.config before I close this in case it helps anybody else:


#recipe Blog
#n Wyam.Html
// Customize your settings and add new ones here
Settings[Keys.Host] = "darrelltunnell.net";
Settings[BlogKeys.Title] = "My Blog";
Settings[BlogKeys.Description] = "Welcome!";
Settings[BlogKeys.PostsPath] = "posts";
Settings[BlogKeys.IndexPageSize] = 1;
Settings[BlogKeys.IndexFullPosts] = true;
Settings[BlogKeys.ArchivePageSize] = 10;
Settings[BlogKeys.ArchiveExcerpts] = true;
Settings[BlogKeys.IncludeDateInPostPath] = true;


// Add any pipeline customizations here
//Console.WriteLine(String.Join(",", Pipelines.Keys.ToArray()));
Pipelines["BlogPosts"].Append(
  Meta(Keys.RelativeFilePath, (doc, ctx) =>
  {
  
    DateTime? publishedDate = doc.Get<DateTime?>(BlogKeys.Published, DateTime.Now);
    //Console.WriteLine("publishedDate: " + publishedDate?.ToString() ?? "");
    
    string slug = doc.Get<string>("slug", ""); 
    //Console.WriteLine("front matter slug: " + slug.ToString());

    bool isPublished = doc.Bool("FrontMatterPublished");
    //Console.WriteLine("front matter published: " + isPublished.ToString());
    
    string fileName;
    if(string.IsNullOrEmpty(slug))
    {
      var fileNameWithoutDatePortion = doc.FilePath(Keys.SourceFileName).ChangeExtension("html").FullPath.Substring(11);
       fileName = fileNameWithoutDatePortion;
    }
    else
    {
       fileName = new FilePath(slug).AppendExtension("html").FullPath; 
    }

    var newPostsPath = ctx.DirectoryPath(BlogKeys.PostsPath).GetRelativePath((DirectoryPath)"../blog");
    return $"{newPostsPath.FullPath}/{publishedDate:yyyy}/{publishedDate:MM}/{publishedDate:dd}/{fileName}";

  })); 

@dazinator dazinator closed this Jun 15, 2018

@daveaglick

This comment has been minimized.

Copy link
Member

commented Jun 15, 2018

Excellent, glad it worked!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.