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

Feature Request: Add capability to define sort order for blog posts #111

Open
cpah opened this issue Aug 27, 2024 · 3 comments
Open

Feature Request: Add capability to define sort order for blog posts #111

cpah opened this issue Aug 27, 2024 · 3 comments
Labels
good first issue Good for newcomers help wanted Extra attention is needed

Comments

@cpah
Copy link

cpah commented Aug 27, 2024

As far as I can see (or work out) there is no way to define the order, in which Blog Posts are listed on a Blog page. I copied / plagiarised the Blog.swift and BlogPost.swift files from the link provided in the article at https://www.hackingwithswift.com/plus/live-streams/brush-bark into my website project. The resulting Blog worked just fine but the order, in which my Blog Posts are listed, appears to be purely random (as it does in the Brush&Bark example).

I would find it very useful indeed to have blogs listed in ascending YAML Date value order. Would it be possible, please?

@loseth
Copy link

loseth commented Aug 29, 2024

Here is a quick fix for your Blog.swift file:

struct Blog: StaticPage {
    var title = "Blog"

    func body(context: PublishingContext) -> [BlockElement] {
        let sortedContent = context.allContent.sorted {
            $0.date > $1.date
        }

        Group {
            Text("Blog")
                .font(.title3)
                .fontWeight(.black)
                .margin(.top, .large)

            Text(placeholderLength: 50)
                .font(.body)

            Section {
                for content in sortedContent {
                    ContentPreview(for: content)
                        .margin(.top, 20)
                }
            }
            .columns(2)
        }
        .frame(width: "90%")
    }
}

@cpah
Copy link
Author

cpah commented Aug 29, 2024

Thanks loseth. That works a treat! I was also able to apply the same of procedure to the List of posts on the TagPage to have the posts listed in reverse chronological order.

struct Tags: TagPage {
    func body(tag: String?, context: PublishingContext) async -> [any BlockElement] {
            if let tag {
                Text(tag)
                    .font(.title1)
                    .fontWeight(.black)
                    .margin(.top, .large)
                    .frame(width: "95%")
            } else {
                Text("All tags")
                    .font(.title1)
                    .frame(width: "95%")
            }

            let posts = context.content(tagged: tag)
            let sortedPosts = posts.sorted  { $0.date > $1.date }
            
            List {
                for post in sortedPosts {
                    Link(post)
                }
            }
            .frame(width: "95%")
    }
}

@twostraws
Copy link
Owner

I had anticipated folks doing exactly what @loseth is suggesting, but:

  1. If it's a random order right now, that's clearly wrong! I think date descending should be the default.
  2. We should make the sample repo document a different sort order, just so folks can see how it's done.

Perhaps someone could contribute a PR for this?

@twostraws twostraws added help wanted Extra attention is needed good first issue Good for newcomers labels Sep 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants