Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I generate navigation from folder structure with Jekyll?

Tags:

jekyll

I have a folder hierarchy like this:

movie scripts/
    Independence Day.md
    Alien.md
    The Omega Man.md
books/
    fiction/
        Dune.md
        Childhood's End.md
    nonfiction/
        Unended Quest.md
software/
    Photoshop.md
    Excel.md

You get the idea.

My goal is to use Jekyll to generate a static non-blog site that lets me browse HTML versions of all my Markdown files. So the navbar would have Movie Scripts, Books, and Software on it. Clicking Books would unfurl two submenus, Fiction and Nonfiction, and clicking one of those would show all pages in that folder.

I've read Jekyll's documentation and watched the Pluralsight course on it, and I know how to render pages from a pages folder... but I can't quite work out how to create navigation from this directory structure.

Could anyone give me some tips? Is this something Jekyll natively supports, or will I have to write something that generates the output myself? Where do I start with this?

like image 291
GreenTriangle Avatar asked Dec 14 '15 07:12

GreenTriangle


People also ask

Can You programmatically retrieve a list of pages in Jekyll?

Instead of hard-coding navigation links, you can programmatically retrieve a list of pages to build the navigation for your site. Although there’s already information about interacting with data files in other Jekyll docs, this tutorial dives into building more robust navigation for your site.

Which files can be transformed by Jekyll?

Provided that the file has a front matter section, it will be transformed by Jekyll. The same will happen for any .html, .markdown , .md, or .textile file in your site’s root directory or directories not listed above.

What is the default directory structure in Jekyll?

Since version 3.2 , a new Jekyll project bootstrapped with jekyll new uses gem-based themes to define the look of the site. This results in a lighter default directory structure: _layouts, _includes and _sass are stored in the theme-gem, by default.

How to access the contents of a yml file in Jekyll?

The Jekyll engine will autoload all data files (using either the .yml , .yaml, .json, .csv or .tsv formats and extensions) in this directory, and they will be accessible via `site.data`. If there's a file members.yml under the directory, then you can access contents of the file through site.data.members .


1 Answers

I'm sure there will be many approaches, but I would use Jekyll collections.

If is your first time, I will be very detailed:

  1. Configure Collections on your _config.yml file

    collections:
      movie-scripts:
        output: true
    
      books:
        output: true
    
      software:
        output: true
    
  2. Add folders to the root of the project directory (same name as each one of the collections)

    _movie-scripts
    _books
    _software
    
  3. Create subfolder for each categorie. For e.g.:

    _movie-scripts/sci-fi
    _books/fiction
    _software/Jekyll
    
  4. Add the markdown files to each of the collection’s subfolders.

    Note: You should use also the Front-Mater to create categories that you might need to filter or search in the future.

  5. Add a folder _data and create 3 YAML files with all the data for your movie-scripts, books and software. For e.g the movie-scripts.yml:

    - title: Back to the Future
      image-path: /images/movie-scripts/back-to-the-future.jpg
      url: http://www.imdb.com/title/tt0088763/
      short-description: This is a 1980s sci-fi classic about traveling through time
      year: 1980
      categorie: sci-fi
    
    - title: Star Wars
      image-path: /images/movie-scripts/start-wars.jpg
      url: http://www.imdb.com/title/tt0076759/
      short-description: Star Wars is an American epic space opera franchise centered on a film series created by George Lucas
      year: 1977
      categorie: sci-fi 
    
  6. Create 3 HTML's pages to access your 3 collections (movie-scripts, books and software). For e.g. the movie-scripts, the 10 most recent additions to your site:

    ---
    layout: page
    permalink: /movie-scripts/top-ten/
    ---
    {% for movie in site.data.movie-scripts limit:10 %}
        <li>
                <img src="{{ movie.image-path }}" alt="{{ movie.title }}"/>
            <a href="{{ movie.url }}">{{ movie.title }}</a>
            <p>{{ movie.short-description }}</p>
            <p>{{ movie.year }}</p>
            <p>{{ movie.categorie }}</p>
         </li>
    {% endfor %}
    

Advice: If this is your first time, do baby steps. Try to do the first colection first, step by step, run the Jekyll server at each time and see if works, go to the next step...

Let me know if it help you, please!

like image 148
Maria Coding Avatar answered Oct 20 '22 22:10

Maria Coding