#Paginate large markdown files

1 messages · Page 1 of 1 (latest)

green prism
#

I have a few books (very long .md files) that I want to paginate. Each book is a single file and each page's end is marked by § symbol. So each book has like ~150 pages.
The folder structure looks like this:
src\pages\resources\books[parsed_title][page].astro

I'm using Astro 5 with Typescript.

Prepare the getStaticPaths function for me that will split each book by the § symbol and paginate them.

lone cometBOT
#

Hi @green prism. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Sure, I can help you with that. Here's a basic example of how you might use getStaticPaths() to paginate your books. This example assumes that you have a way to fetch your books and split them into pages by the § symbol.

import { Astro } from 'astro';

export async function getStaticPaths() {
  // Load your books data. This could be a fetch() call, Astro.glob(), etc.
  // For this example, let's assume you have a function called loadBooks() that returns an array of books.
  const books = await loadBooks();

  // Split each book into pages by the § symbol and generate paths for each page.
  const paths = books.flatMap((book, bookIndex) => {
    const pages = book.content.split('§');
    return pages.map((pageContent, pageIndex) => {
      return {
        params: {
          parsed_title: book.title,
          page: String(pageIndex + 1),
        },
        props: {
          pageContent,
        },
      };
    });
  });

  return paths;
}

const { pageContent } = Astro.props;

In this example, loadBooks() is a hypothetical function that you would need to implement. It should fetch your books and return them in a format like { title: 'Book Title', content: 'Book content' }.

Please note that this is a basic example and might need to be adjusted based on your specific needs and the structure of your project.

For more information on getStaticPaths(), you can refer to the Astro documentation (https://docs.astro.build/en/reference/api-reference/#getstaticpaths).

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: