Skip to content

Instantly share code, notes, and snippets.

@matbrgz
Forked from MichaelCurrin/README.md
Created February 24, 2025 01:30
Show Gist options
  • Select an option

  • Save matbrgz/2c8db43d0ec3a9bd1670599a9e7039a8 to your computer and use it in GitHub Desktop.

Select an option

Save matbrgz/2c8db43d0ec3a9bd1670599a9e7039a8 to your computer and use it in GitHub Desktop.

Revisions

  1. @MichaelCurrin MichaelCurrin revised this gist May 22, 2022. 2 changed files with 19 additions and 9 deletions.
    3 changes: 2 additions & 1 deletion page-listing.md
    Original file line number Diff line number Diff line change
    @@ -15,7 +15,7 @@ Use `site.pages` if you want to list pages. This will exclude posts and collecti
    {%- for page in site.pages %}
    {
    "title": {{- page.title | jsonify }},
    "url": {{- page.url | jsonify }}
    "url": {{- page.url | relative_url | jsonify }}
    }
    {% unless forloop.last %},{% endunless %}
    {% endfor -%}
    @@ -24,6 +24,7 @@ Use `site.pages` if you want to list pages. This will exclude posts and collecti
    You can do something similar with items in `site.my_collection` or all collections with `site.collections`.
    ## Mix formats
    If you mix your pages as HTML and JSON, you can add a filter to get only JSON files.
    25 changes: 17 additions & 8 deletions post-listing.md
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,10 @@
    # All posts endpoint

    Make a single JSON file containing data for all pages.
    Make a _single_ JSON file containing data for all posts.


    ## Input file

    Add `content` if you want that too, but it will make the output a lot longer.

    - `api/posts.json`
    ```liquid
    ---
    @@ -18,16 +16,27 @@ Add `content` if you want that too, but it will make the output a lot longer.
    "id": {{- post.id | jsonify -}},
    "title": {{- post.title | jsonify }},
    "date": {{- post.date | jsonify }},
    "url": {{- post.url | jsonify }},
    "url": {{- post.url | relative_url | jsonify }},
    "tags": {{- post.tags | jsonify }},
    "categories": {{- post.categories | jsonify }}
    }
    {% unless forloop.last %},{% endunless %}
    {% endfor -%}
    ]
    ```
    Add `content` too if you need that, but it will make the output a lot longer and the JSON file will make a lot bigger to download.
    ```
    "content": {{- content | jsonify }}
    ```
    You might want to put that through a Markdown to HTML converter filter if you want HTML.
    Also note that Liquid code in the content won't get rendered - you'll get the raw code.
    ## Output file
    - `/posts/`
    @@ -37,7 +46,7 @@ Add `content` if you want that too, but it will make the output a lot longer.
    "id":"/2020/05/15/meaning-and-recognition",
    "title":"Meaning and recognition",
    "date":"2020-05-15 00:00:00 +0200",
    "url":"/2020/05/15/meaning-and-recognition.html",
    "url":"/my-repo-name/2020/05/15/meaning-and-recognition.html",
    "tags":["reflection","motivation"],
    "categories":[]
    }
    @@ -49,11 +58,11 @@ Add `content` if you want that too, but it will make the output a lot longer.
    ## Permalink
    Optionally add permalink:
    Optionally set a permalink as:
    ```yaml
    permalink: /api/posts/
    ```

    Then you can access as `/api/posts/` instead of `/api/posts.json`.
    Then you can access it as `/api/posts/` instead of `/api/posts.json`.

  2. @MichaelCurrin MichaelCurrin revised this gist Nov 3, 2021. 2 changed files with 89 additions and 88 deletions.
    89 changes: 1 addition & 88 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -7,91 +7,4 @@ How to make a read-only JSON REST API using Jekyll.

    This doesn't need any Ruby plugins - you just use some built-in templating features in Jekyll 3 or 4.

    You will end up with a single JSON file contains data for _all_ pages on the site, and another JSON file of just posts. Alternatively, you can replace every HTML page and post with a JSON version.


    ## Data source

    You might have data stored in your Jekyll project. Typically in YAML, CSV or JSON file in the `_data/` directory. Or in the frontmatter of your pages. This tutorial shows you how to render as JSON pages.

    The data files could be:

    - in version control (visible in Git and on GitHub)
    - dynamic (ignored by Git) - for example if you have a script to fetch API data and write to `_data` every time you deploy your site.


    ## Why

    This is useful if you want to make your data available for another service or for others to consume (like GitHub, Twitter and Facebook make their data available on APIs). Or perhaps you have JavaScript single-page application that reads from your API backend to serve the app or build a search index.


    ## Example

    Given data in page frontmatter or a YAML data file:

    ```yaml
    ---
    my_data:
    - a: 1
    b: 2
    - a: 100
    b: 200
    ---
    ```

    Or CSV data as:

    ```
    a, b
    1, 2
    100,200
    ```

    The rendered JSON page will look like this:

    - `localhost:4000/foo.json`
    ```json
    [
    { "a": "1", "b": "2" },
    { "a": "100", "b": "200" }
    ]
    ```


    ## Static site note

    Note that Jekyll is a _static site generator_. So your JSON API data will only be updated whenever you make a commit and the site rebuilds. If your API needs to have data which changes based on many users or needs to change in realtime, then you're better off using Node.js or Python for your API.


    ## Summary of approach

    Here we will use `.json` as the file extension, instead of the usual `.md` or `.html`. This means your HTTP header will be set correctly:

    ```
    Content-Type application/json
    ```
    We'll also make sure to set the layout to `null` so that we get a pure JSON page without any HTML styling.
    We'll use our the YAML data to build the response. If you use the `jsonify` Jekyll, you can convert data straight to JSON without worrying about `for` loops or JSON syntax.
    I recommend using a Jekyll extension for your IDE, to handle Liquid syntax highlighting and recognizing frontmatter. Then make sure you choose `jekyll` as your formatter for your `.json` files.
    ## Sitemap note
    It looks like JSON files are excluded from a sitemap file by default. The plugin recognizes just HTML and Markdown files.
    ## Resources
    - [JSON](https://en.wikipedia.org/wiki/JSON) on Wikipedia
    - [API](https://en.wikipedia.org/wiki/API) on Wikipedia
    - [Representational state transfer](https://en.wikipedia.org/wiki/Representational_state_transfer)
    ## Taking it further
    See also [Data files](https://jekyllrb.com/docs/datafiles/) in the Jekyll docs. You might iterate over data in a JSON or YAML file or files to build up output on a page. The data files are your database and then Jekyll builds your API. This is also a way to build a databse JSON file to be consumed on the frontend such as for search functionality.
    Or you might store your data in the frontmatter of Jekyll [Collections](https://jekyllrb.com/docs/collections/) which get outputted as pages similar to the Page example below. That way you can group your JSON data into collections as interate over them easily.
    You will end up with a single JSON file contains data for _all_ pages on the site, and another JSON file of just posts. Alternatively, you can replace every HTML page and post with a JSON version.
    88 changes: 88 additions & 0 deletions notes.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,88 @@
    # Notes


    ## Data source

    You might have data stored in your Jekyll project. Typically in YAML, CSV or JSON file in the `_data/` directory. Or in the frontmatter of your pages. This tutorial shows you how to render as JSON pages.

    The data files could be:

    - in version control (visible in Git and on GitHub)
    - dynamic (ignored by Git) - for example if you have a script to fetch API data and write to `_data` every time you deploy your site.


    ## Why

    This is useful if you want to make your data available for another service or for others to consume (like GitHub, Twitter and Facebook make their data available on APIs). Or perhaps you have JavaScript single-page application that reads from your API backend to serve the app or build a search index.


    ## Example

    Given data in page frontmatter or a YAML data file:

    ```yaml
    ---
    my_data:
    - a: 1
    b: 2
    - a: 100
    b: 200
    ---
    ```

    Or CSV data as:

    ```
    a, b
    1, 2
    100,200
    ```

    The rendered JSON page will look like this:

    - `localhost:4000/foo.json`
    ```json
    [
    { "a": "1", "b": "2" },
    { "a": "100", "b": "200" }
    ]
    ```


    ## Static site note

    Note that Jekyll is a _static site generator_. So your JSON API data will only be updated whenever you make a commit and the site rebuilds. If your API needs to have data which changes based on many users or needs to change in realtime, then you're better off using Node.js or Python for your API.


    ## Summary of approach

    Here we will use `.json` as the file extension, instead of the usual `.md` or `.html`. This means your HTTP header will be set correctly:

    ```
    Content-Type application/json
    ```
    We'll also make sure to set the layout to `null` so that we get a pure JSON page without any HTML styling.
    We'll use our the YAML data to build the response. If you use the `jsonify` Jekyll, you can convert data straight to JSON without worrying about `for` loops or JSON syntax.
    I recommend using a Jekyll extension for your IDE, to handle Liquid syntax highlighting and recognizing frontmatter. Then make sure you choose `jekyll` as your formatter for your `.json` files.
    ## Sitemap note
    It looks like JSON files are excluded from a sitemap file by default. The plugin recognizes just HTML and Markdown files.
    ## Resources
    - [JSON](https://en.wikipedia.org/wiki/JSON) on Wikipedia
    - [API](https://en.wikipedia.org/wiki/API) on Wikipedia
    - [Representational state transfer](https://en.wikipedia.org/wiki/Representational_state_transfer)
    ## Taking it further
    See also [Data files](https://jekyllrb.com/docs/datafiles/) in the Jekyll docs. You might iterate over data in a JSON or YAML file or files to build up output on a page. The data files are your database and then Jekyll builds your API. This is also a way to build a databse JSON file to be consumed on the frontend such as for search functionality.
    Or you might store your data in the frontmatter of Jekyll [Collections](https://jekyllrb.com/docs/collections/) which get outputted as pages similar to the Page example below. That way you can group your JSON data into collections as interate over them easily.
  3. @MichaelCurrin MichaelCurrin revised this gist Nov 3, 2021. 4 changed files with 4 additions and 4 deletions.
    2 changes: 1 addition & 1 deletion page-listing.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # Page listing
    # All pages endpoint

    Make a single JSON file containing data for all pages.

    2 changes: 1 addition & 1 deletion page-sample.md → page.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # Page
    # Pages as JSON files

    Render each Markdown page as a JSON file, using frontmatter for data. Note that no HTML file is generated here.

    2 changes: 1 addition & 1 deletion post-listing.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # Post listing
    # All posts endpoint

    Make a single JSON file containing data for all pages.

    2 changes: 1 addition & 1 deletion post-sample.md → post.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # Post sample
    # Posts as JSON files

    How to output each Markdown post into a JSON file.

  4. @MichaelCurrin MichaelCurrin revised this gist Nov 3, 2021. 5 changed files with 11 additions and 4 deletions.
    4 changes: 3 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,9 @@

    How to make a read-only JSON REST API using Jekyll.

    This doesn't need any Ruby plugins - you just use some built-in templating features in Jekyll 3 or 4. You will end up with a single JSON file contains data for _all_ pages on the site, and another JSON file of just posts.
    This doesn't need any Ruby plugins - you just use some built-in templating features in Jekyll 3 or 4.

    You will end up with a single JSON file contains data for _all_ pages on the site, and another JSON file of just posts. Alternatively, you can replace every HTML page and post with a JSON version.


    ## Data source
    4 changes: 3 additions & 1 deletion page-listing.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,8 @@
    # Page listing

    See [Post listing](#post-listing) section.
    Make a single JSON file containing data for all pages.

    See also [Post listing](#post-listing) section.

    Use `site.pages` if you want to list pages. This will exclude posts and collections.

    2 changes: 1 addition & 1 deletion page-sample.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    # Page

    Render a single page with frontmatter as a JSON file. Note that no HTML file is generated here.
    Render each Markdown page as a JSON file, using frontmatter for data. Note that no HTML file is generated here.

    - `_layouts/page.html` - build up the JSON object, quoting all values and in the case of the body text we convert from markdown to HTML.
    ```liquid
    2 changes: 1 addition & 1 deletion post-listing.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    # Post listing

    Make JSON endpoint listing all posts on your site.
    Make a single JSON file containing data for all pages.


    ## Input file
    3 changes: 3 additions & 0 deletions post-sample.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,8 @@
    # Post sample

    How to output each Markdown post into a JSON file.


    ## Input file

    Here we make a post in the usual `_posts` directory.
  5. @MichaelCurrin MichaelCurrin revised this gist Nov 3, 2021. 3 changed files with 6 additions and 5 deletions.
    7 changes: 4 additions & 3 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -5,13 +5,14 @@

    How to make a read-only JSON REST API using Jekyll.

    This doesn't need any Ruby plugins - you just use some built-in templating features in Jekyll 3 or 4.
    This doesn't need any Ruby plugins - you just use some built-in templating features in Jekyll 3 or 4. You will end up with a single JSON file contains data for _all_ pages on the site, and another JSON file of just posts.

    ## Data

    ## Data source

    You might have data stored in your Jekyll project. Typically in YAML, CSV or JSON file in the `_data/` directory. Or in the frontmatter of your pages. This tutorial shows you how to render as JSON pages.

    The data files could be
    The data files could be:

    - in version control (visible in Git and on GitHub)
    - dynamic (ignored by Git) - for example if you have a script to fetch API data and write to `_data` every time you deploy your site.
    2 changes: 1 addition & 1 deletion page.md → page-sample.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    ## Page
    # Page

    Render a single page with frontmatter as a JSON file. Note that no HTML file is generated here.

    2 changes: 1 addition & 1 deletion post-file.md → post-sample.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # Post
    # Post sample

    ## Input file

  6. @MichaelCurrin MichaelCurrin revised this gist Oct 24, 2021. 1 changed file with 16 additions and 4 deletions.
    20 changes: 16 additions & 4 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,23 @@
    # Build a REST API with Jekyll
    > Serve your data as JSON
    # Jekyll - how to build a REST API
    > Serve your data as static JSON
    <!-- Note to self: This gist was created as part of a PR discussion for a tutorial on Jekyll docs. That tutorial links to this gist, which is kept here as a permalink and should not be moved to repo -->

    How to make a read-only JSON REST API using Jekyll. This doesn't need any Ruby plugins - you just use some builtin templating features in Jekyll 3 or 4.
    How to make a read-only JSON REST API using Jekyll.

    You might have data stored in your Jekyll project, such as in a YAML, CSV or JSON file in the `_data/` directory or in the frontmatter of your pages. This tutorial shows you how to render as JSON pages.
    This doesn't need any Ruby plugins - you just use some built-in templating features in Jekyll 3 or 4.

    ## Data

    You might have data stored in your Jekyll project. Typically in YAML, CSV or JSON file in the `_data/` directory. Or in the frontmatter of your pages. This tutorial shows you how to render as JSON pages.

    The data files could be

    - in version control (visible in Git and on GitHub)
    - dynamic (ignored by Git) - for example if you have a script to fetch API data and write to `_data` every time you deploy your site.


    ## Why

    This is useful if you want to make your data available for another service or for others to consume (like GitHub, Twitter and Facebook make their data available on APIs). Or perhaps you have JavaScript single-page application that reads from your API backend to serve the app or build a search index.

  7. @MichaelCurrin MichaelCurrin revised this gist Aug 16, 2021. 2 changed files with 9 additions and 5 deletions.
    5 changes: 5 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -63,6 +63,11 @@ We'll use our the YAML data to build the response. If you use the `jsonify` Jeky
    I recommend using a Jekyll extension for your IDE, to handle Liquid syntax highlighting and recognizing frontmatter. Then make sure you choose `jekyll` as your formatter for your `.json` files.
    ## Sitemap note
    It looks like JSON files are excluded from a sitemap file by default. The plugin recognizes just HTML and Markdown files.
    ## Resources
    - [JSON](https://en.wikipedia.org/wiki/JSON) on Wikipedia
    9 changes: 4 additions & 5 deletions page-listing.md
    Original file line number Diff line number Diff line change
    @@ -2,9 +2,7 @@

    See [Post listing](#post-listing) section.

    Use `site.pages` if you want to list pages, posts and collections. You could filter by `type` to exclude posts and collections.

    You can do something similar with `site.my-collection` for collections.
    Use `site.pages` if you want to list pages. This will exclude posts and collections.

    - `api/pages.json`
    ```liquid
    @@ -14,14 +12,15 @@ You can do something similar with `site.my-collection` for collections.
    [
    {%- for page in site.pages %}
    {
    "title": {{- post.title | jsonify }},
    "url": {{- post.url | jsonify }}
    "title": {{- page.title | jsonify }},
    "url": {{- page.url | jsonify }}
    }
    {% unless forloop.last %},{% endunless %}
    {% endfor -%}
    ]
    ```
    You can do something similar with items in `site.my_collection` or all collections with `site.collections`.
    ## Mix formats
  8. @MichaelCurrin MichaelCurrin revised this gist Aug 16, 2021. 3 changed files with 72 additions and 16 deletions.
    37 changes: 37 additions & 0 deletions page-listing.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    # Page listing

    See [Post listing](#post-listing) section.

    Use `site.pages` if you want to list pages, posts and collections. You could filter by `type` to exclude posts and collections.

    You can do something similar with `site.my-collection` for collections.

    - `api/pages.json`
    ```liquid
    ---
    layout: none
    ---
    [
    {%- for page in site.pages %}
    {
    "title": {{- post.title | jsonify }},
    "url": {{- post.url | jsonify }}
    }
    {% unless forloop.last %},{% endunless %}
    {% endfor -%}
    ]
    ```
    ## Mix formats
    If you mix your pages as HTML and JSON, you can add a filter to get only JSON files.
    ```
    {% if post.ext == '.json' %}
    ```
    This field works for posts only.
    For pages, you'll have to use `item.name` (e.g. `foo.md`) and check if it ends with `.json`.
    2 changes: 2 additions & 0 deletions page.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,7 @@
    ## Page

    Render a single page with frontmatter as a JSON file. Note that no HTML file is generated here.

    - `_layouts/page.html` - build up the JSON object, quoting all values and in the case of the body text we convert from markdown to HTML.
    ```liquid
    ---
    49 changes: 33 additions & 16 deletions post-listing.md
    Original file line number Diff line number Diff line change
    @@ -1,42 +1,59 @@
    # Post listing

    If you want to make as summary of all your posts.
    Make JSON endpoint listing all posts on your site.


    ## Input file

    - `posts.json`
    Add `content` if you want that too, but it will make the output a lot longer.

    - `api/posts.json`
    ```liquid
    ---
    layout: none
    permalink: /posts/
    ---
    [
    {%- for post in site.posts %}
    {%- for post in site.posts %}
    {
    "title": {{ post.title | jsonify }},
    "date": {{ post.date | jsonify }},
    "url": {{ post.url | jsonify}}
    "id": {{- post.id | jsonify -}},
    "title": {{- post.title | jsonify }},
    "date": {{- post.date | jsonify }},
    "url": {{- post.url | jsonify }},
    "tags": {{- post.tags | jsonify }},
    "categories": {{- post.categories | jsonify }}
    }
    {% endfor -%}
    {% unless forloop.last %},{% endunless %}
    {% endfor -%}
    ]
    ```
    You can change out `site.posts` for `site.pages` if you want to list your all pages. You can do something similar with `site.my-collection`.
    If you mix your posts as HTML and JSON, you can add a filter to get only JSON files - `{% if post.ext == '.json' %}`. This works for posts only. For pages, you'll have to use `item.name` (e.g. `foo.md`) and check if it ends with `.json`.
    ## Output file
    - `/posts/`
    ```json5
    [
    {
    "title": "Hello world",
    "date": "2020-12-12 00:00:00 +0200",
    "url": "/go/python/2020/12/12/hello.json"
    },
    "id":"/2020/05/15/meaning-and-recognition",
    "title":"Meaning and recognition",
    "date":"2020-05-15 00:00:00 +0200",
    "url":"/2020/05/15/meaning-and-recognition.html",
    "tags":["reflection","motivation"],
    "categories":[]
    }
    ,
    // more posts...
    ]
    ```
    ## Permalink
    Optionally add permalink:
    ```yaml
    permalink: /api/posts/
    ```

    Then you can access as `/api/posts/` instead of `/api/posts.json`.

  9. @MichaelCurrin MichaelCurrin revised this gist Jan 13, 2021. 3 changed files with 13 additions and 6 deletions.
    7 changes: 6 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    # Serving JSON as a Jekyll REST API
    # Build a REST API with Jekyll
    > Serve your data as JSON
    <!-- Note to self: This gist was created as part of a PR discussion for a tutorial on Jekyll docs. That tutorial links to this gist, which is kept here as a permalink and should not be moved to repo -->

    @@ -8,6 +9,7 @@ You might have data stored in your Jekyll project, such as in a YAML, CSV or JSO

    This is useful if you want to make your data available for another service or for others to consume (like GitHub, Twitter and Facebook make their data available on APIs). Or perhaps you have JavaScript single-page application that reads from your API backend to serve the app or build a search index.


    ## Example

    Given data in page frontmatter or a YAML data file:
    @@ -40,10 +42,12 @@ The rendered JSON page will look like this:
    ]
    ```


    ## Static site note

    Note that Jekyll is a _static site generator_. So your JSON API data will only be updated whenever you make a commit and the site rebuilds. If your API needs to have data which changes based on many users or needs to change in realtime, then you're better off using Node.js or Python for your API.


    ## Summary of approach

    Here we will use `.json` as the file extension, instead of the usual `.md` or `.html`. This means your HTTP header will be set correctly:
    @@ -65,6 +69,7 @@ I recommend using a Jekyll extension for your IDE, to handle Liquid syntax highl
    - [API](https://en.wikipedia.org/wiki/API) on Wikipedia
    - [Representational state transfer](https://en.wikipedia.org/wiki/Representational_state_transfer)
    ## Taking it further
    See also [Data files](https://jekyllrb.com/docs/datafiles/) in the Jekyll docs. You might iterate over data in a JSON or YAML file or files to build up output on a page. The data files are your database and then Jekyll builds your API. This is also a way to build a databse JSON file to be consumed on the frontend such as for search functionality.
    7 changes: 4 additions & 3 deletions post-file.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    ## Post
    # Post

    ### Input file
    ## Input file

    Here we make a post in the usual `_posts` directory.

    @@ -37,7 +37,8 @@ Also note unlike in the page example above, we don't covert to body content from
    ---
    ```
    ### Output file
    ## Output file
    Visit at `http://localhost:4000/go/python/2020/12/12/hello.json`
    5 changes: 3 additions & 2 deletions post-listing.md
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,9 @@
    ## Post listing
    # Post listing

    If you want to make as summary of all your posts.


    ### Input file
    ## Input file

    - `posts.json`
    ```liquid
    @@ -26,6 +26,7 @@ You can change out `site.posts` for `site.pages` if you want to list your all pa
    If you mix your posts as HTML and JSON, you can add a filter to get only JSON files - `{% if post.ext == '.json' %}`. This works for posts only. For pages, you'll have to use `item.name` (e.g. `foo.md`) and check if it ends with `.json`.
    ## Output file
    - `/posts/`
  10. @MichaelCurrin MichaelCurrin revised this gist Jan 10, 2021. 1 changed file with 31 additions and 9 deletions.
    40 changes: 31 additions & 9 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,27 +1,46 @@
    # Serving JSON as a Jekyll REST API

    <!-- Note to self: This gist was created as part of a PR discussion for a tutorial on Jekyll docs. That tutorial links to this gist, which is kept here as a permalink and should not be moved to repo -->

    How to make a read-only JSON REST API using Jekyll. This doesn't need any Ruby plugins - you just use some builtin templating features in Jekyll 3 or 4.

    You might have data stored in your Jekyll project, such as in a YAML, CSV or JSON file in the `_data/` directory or in the frontmatter of your pages. This tutorial shows you how to render as JSON pages.

    Given YAML data as:
    This is useful if you want to make your data available for another service or for others to consume (like GitHub, Twitter and Facebook make their data available on APIs). Or perhaps you have JavaScript single-page application that reads from your API backend to serve the app or build a search index.

    ## Example

    Given data in page frontmatter or a YAML data file:

    ```yaml
    a: 1
    b: 2
    ---
    my_data:
    - a: 1
    b: 2
    - a: 100
    b: 200
    ---
    ```

    The rendered page will look something like this:
    Or CSV data as:

    ```
    a, b
    1, 2
    100,200
    ```

    The rendered JSON page will look like this:

    - `localhost:4000/foo.json`
    ```json
    {
    "a": 1,
    "b": 2
    }
    [
    { "a": "1", "b": "2" },
    { "a": "100", "b": "200" }
    ]
    ```

    This is useful if you want to make your data available for another service or for others to consume (like GitHub, Twitter and Facebook make their data available on APIs). Or perhaps you have JavaScript single-page application that reads from your API backend to serve the app or build a search index.
    ## Static site note

    Note that Jekyll is a _static site generator_. So your JSON API data will only be updated whenever you make a commit and the site rebuilds. If your API needs to have data which changes based on many users or needs to change in realtime, then you're better off using Node.js or Python for your API.

    @@ -37,6 +56,9 @@ We'll also make sure to set the layout to `null` so that we get a pure JSON page
    We'll use our the YAML data to build the response. If you use the `jsonify` Jekyll, you can convert data straight to JSON without worrying about `for` loops or JSON syntax.
    I recommend using a Jekyll extension for your IDE, to handle Liquid syntax highlighting and recognizing frontmatter. Then make sure you choose `jekyll` as your formatter for your `.json` files.
    ## Resources
    - [JSON](https://en.wikipedia.org/wiki/JSON) on Wikipedia
  11. @MichaelCurrin MichaelCurrin revised this gist Jan 10, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -35,7 +35,7 @@ Content-Type application/json

    We'll also make sure to set the layout to `null` so that we get a pure JSON page without any HTML styling.

    We'll iterate over the YAML data to build or get the values we need, to build up the response.
    We'll use our the YAML data to build the response. If you use the `jsonify` Jekyll, you can convert data straight to JSON without worrying about `for` loops or JSON syntax.

    ## Resources

  12. @MichaelCurrin MichaelCurrin revised this gist Jan 10, 2021. 1 changed file with 29 additions and 3 deletions.
    32 changes: 29 additions & 3 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,15 +1,41 @@
    # Serving JSON as a Jekyll REST API

    How to make a read-only JSON REST API using Jekyll and some builtin templating features in Jekyll 3 or 4.
    How to make a read-only JSON REST API using Jekyll. This doesn't need any Ruby plugins - you just use some builtin templating features in Jekyll 3 or 4.

    This is useful if you have content in your pages which you want to make available to an external service or a JavaScript single-page application that reads from your API backend.
    You might have data stored in your Jekyll project, such as in a YAML, CSV or JSON file in the `_data/` directory or in the frontmatter of your pages. This tutorial shows you how to render as JSON pages.

    The headers also get set correctly by using `.json` instead of `.md` or `.html` as the source files.
    Given YAML data as:

    ```yaml
    a: 1
    b: 2
    ```
    The rendered page will look something like this:
    - `localhost:4000/foo.json`
    ```json
    {
    "a": 1,
    "b": 2
    }
    ```

    This is useful if you want to make your data available for another service or for others to consume (like GitHub, Twitter and Facebook make their data available on APIs). Or perhaps you have JavaScript single-page application that reads from your API backend to serve the app or build a search index.

    Note that Jekyll is a _static site generator_. So your JSON API data will only be updated whenever you make a commit and the site rebuilds. If your API needs to have data which changes based on many users or needs to change in realtime, then you're better off using Node.js or Python for your API.

    ## Summary of approach

    Here we will use `.json` as the file extension, instead of the usual `.md` or `.html`. This means your HTTP header will be set correctly:

    ```
    Content-Type application/json
    ```

    We'll also make sure to set the layout to `null` so that we get a pure JSON page without any HTML styling.

    We'll iterate over the YAML data to build or get the values we need, to build up the response.

    ## Resources

  13. @MichaelCurrin MichaelCurrin revised this gist Jan 10, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # Jekyll JSON API
    # Serving JSON as a Jekyll REST API

    How to make a read-only JSON REST API using Jekyll and some builtin templating features in Jekyll 3 or 4.

  14. @MichaelCurrin MichaelCurrin revised this gist Dec 24, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion post-listing.md
    Original file line number Diff line number Diff line change
    @@ -29,7 +29,7 @@ If you mix your posts as HTML and JSON, you can add a filter to get only JSON fi
    ## Output file
    - `/posts/`
    ```json
    ```json5
    [
    {
    "title": "Hello world",
  15. @MichaelCurrin MichaelCurrin revised this gist Dec 24, 2020. 3 changed files with 0 additions and 0 deletions.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
  16. @MichaelCurrin MichaelCurrin revised this gist Dec 24, 2020. 3 changed files with 2 additions and 1 deletion.
    File renamed without changes.
    File renamed without changes.
    3 changes: 2 additions & 1 deletion post-listing.md → 3-post-listing.md
    Original file line number Diff line number Diff line change
    @@ -35,6 +35,7 @@ If you mix your posts as HTML and JSON, you can add a filter to get only JSON fi
    "title": "Hello world",
    "date": "2020-12-12 00:00:00 +0200",
    "url": "/go/python/2020/12/12/hello.json"
    }
    },
    // more posts...
    ]
    ```
  17. @MichaelCurrin MichaelCurrin revised this gist Dec 24, 2020. 1 changed file with 40 additions and 0 deletions.
    40 changes: 40 additions & 0 deletions post-listing.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    ## Post listing

    If you want to make as summary of all your posts.


    ### Input file

    - `posts.json`
    ```liquid
    ---
    layout: none
    permalink: /posts/
    ---
    [
    {%- for post in site.posts %}
    {
    "title": {{ post.title | jsonify }},
    "date": {{ post.date | jsonify }},
    "url": {{ post.url | jsonify}}
    }
    {% endfor -%}
    ]
    ```
    You can change out `site.posts` for `site.pages` if you want to list your all pages. You can do something similar with `site.my-collection`.
    If you mix your posts as HTML and JSON, you can add a filter to get only JSON files - `{% if post.ext == '.json' %}`. This works for posts only. For pages, you'll have to use `item.name` (e.g. `foo.md`) and check if it ends with `.json`.
    ## Output file
    - `/posts/`
    ```json
    [
    {
    "title": "Hello world",
    "date": "2020-12-12 00:00:00 +0200",
    "url": "/go/python/2020/12/12/hello.json"
    }
    ]
    ```
  18. @MichaelCurrin MichaelCurrin revised this gist Dec 24, 2020. 1 changed file with 21 additions and 0 deletions.
    21 changes: 21 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,24 @@
    # Jekyll JSON API

    How to make a read-only JSON REST API using Jekyll and some builtin templating features in Jekyll 3 or 4.

    This is useful if you have content in your pages which you want to make available to an external service or a JavaScript single-page application that reads from your API backend.

    The headers also get set correctly by using `.json` instead of `.md` or `.html` as the source files.

    ```
    Content-Type application/json
    ```


    ## Resources

    - [JSON](https://en.wikipedia.org/wiki/JSON) on Wikipedia
    - [API](https://en.wikipedia.org/wiki/API) on Wikipedia
    - [Representational state transfer](https://en.wikipedia.org/wiki/Representational_state_transfer)

    ## Taking it further

    See also [Data files](https://jekyllrb.com/docs/datafiles/) in the Jekyll docs. You might iterate over data in a JSON or YAML file or files to build up output on a page. The data files are your database and then Jekyll builds your API. This is also a way to build a databse JSON file to be consumed on the frontend such as for search functionality.

    Or you might store your data in the frontmatter of Jekyll [Collections](https://jekyllrb.com/docs/collections/) which get outputted as pages similar to the Page example below. That way you can group your JSON data into collections as interate over them easily.
  19. @MichaelCurrin MichaelCurrin revised this gist Dec 24, 2020. 2 changed files with 6 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions page.md
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,11 @@
    ## Page

    - `_layouts/page.html`
    - `_layouts/page.html` - build up the JSON object, quoting all values and in the case of the body text we convert from markdown to HTML.
    ```liquid
    ---
    layout: none
    ---
    {"title":{{ page.title | jsonify }},"content":{{ page.body | jsonify }},"links":{{ page.links | jsonify }}}
    {"title":{{ page.title | jsonify }},"content":{{ page.body | markdownify | jsonify }},"links":{{ page.links | jsonify }}}
    ```
    - `foo.json` - a Jekyll HTML/markdown page with a `.json` extension.
    ```liquid
    @@ -37,15 +37,15 @@
    Visit at `http://localhost:4000/foo.json`
    ```json
    {"title":"Testing","content":"Hello, **world**.\nI am here.\n","links":[{"title":"Go track on Exercism","url":"https://exercism.io/tracks/go"},{"title":"Go tour welcome page","url":"https://tour.golang.org/welcome/1"},{"title":"Learn Go with Tests","url":"https://quii.gitbook.io/learn-go-with-tests/"},{"title":"Go for Python programmers","url":"https://golang-for-python-programmers.readthedocs.io/en/latest/index.html"}]}
    {"title":"Testing","content":"<p>Hello, <strong>world</strong>.\nI am here.</p>\n","links":[{"title":"Go track on Exercism","url":"https://exercism.io/tracks/go"},{"title":"Go tour welcome page","url":"https://tour.golang.org/welcome/1"},{"title":"Learn Go with Tests","url":"https://quii.gitbook.io/learn-go-with-tests/"},{"title":"Go for Python programmers","url":"https://golang-for-python-programmers.readthedocs.io/en/latest/index.html"}]}
    ```

    Here it is in a more readable format:

    ```json
    {
    "title":"Testing",
    "content":"Hello, **world**.\nI am here.\n",
    "content":"<p>Hello, <strong>world</strong>.\nI am here.</p>\n",
    "links": [
    {"title":"Go track on Exercism","url":"https://exercism.io/tracks/go"},
    {"title":"Go tour welcome page","url":"https://tour.golang.org/welcome/1"},
    2 changes: 2 additions & 0 deletions post.md
    Original file line number Diff line number Diff line change
    @@ -8,6 +8,8 @@ The layout is also much lighter and more flexible - using the `data` object with

    The downside is that you have to be more verbose in your post frontmatter to define a YAML variable with `&` and then use it with `*`. Based on this [YAML tutorial](https://idratherbewriting.com/documentation-theme-jekyll/mydoc_yaml_tutorial.html).

    Also note unlike in the page example above, we don't covert to body content from markdown to HTML.

    - `_layouts/post.html` - a layout which uses frontmatter and converts it to JSON output.
    ```liquid
    ---
  20. @MichaelCurrin MichaelCurrin revised this gist Dec 24, 2020. 2 changed files with 12 additions and 6 deletions.
    12 changes: 8 additions & 4 deletions page.md
    Original file line number Diff line number Diff line change
    @@ -5,14 +5,17 @@
    ---
    layout: none
    ---
    {"title":{{ page.title | jsonify }},"links": {{ page.links | jsonify }}}
    {"title":{{ page.title | jsonify }},"content":{{ page.body | jsonify }},"links":{{ page.links | jsonify }}}
    ```
    - `foo.json` - a page with a `.json` extension.
    - `foo.json` - a Jekyll HTML/markdown page with a `.json` extension.
    ```liquid
    ---
    layout: page
    title: Foo
    body: |
    Hello, **world**.
    I am here.
    links:
    - title: Go track on Exercism
    url: https://exercism.io/tracks/go
    @@ -34,14 +37,15 @@
    Visit at `http://localhost:4000/foo.json`
    ```json
    {"title":"Testing","links": [{"title":"Go track on Exercism","url":"https://exercism.io/tracks/go"},{"title":"Go tour welcome page","url":"https://tour.golang.org/welcome/1"},{"title":"Learn Go with Tests","url":"https://quii.gitbook.io/learn-go-with-tests/"},{"title":"Go for Python programmers","url":"https://golang-for-python-programmers.readthedocs.io/en/latest/index.html"}]}
    {"title":"Testing","content":"Hello, **world**.\nI am here.\n","links":[{"title":"Go track on Exercism","url":"https://exercism.io/tracks/go"},{"title":"Go tour welcome page","url":"https://tour.golang.org/welcome/1"},{"title":"Learn Go with Tests","url":"https://quii.gitbook.io/learn-go-with-tests/"},{"title":"Go for Python programmers","url":"https://golang-for-python-programmers.readthedocs.io/en/latest/index.html"}]}
    ```

    Here it is in a more readable format:

    ```json
    {
    "title":"Testing",
    "content":"Hello, **world**.\nI am here.\n",
    "links": [
    {"title":"Go track on Exercism","url":"https://exercism.io/tracks/go"},
    {"title":"Go tour welcome page","url":"https://tour.golang.org/welcome/1"},
    6 changes: 4 additions & 2 deletions post.md
    Original file line number Diff line number Diff line change
    @@ -24,7 +24,9 @@ The downside is that you have to be more verbose in your post frontmatter to def
    categories: &categories
    - Go
    - Python
    content: &content Hello, world!
    content: &content |
    Hello, **world**
    I am here
    data:
    title: *title
    @@ -38,5 +40,5 @@ The downside is that you have to be more verbose in your post frontmatter to def
    Visit at `http://localhost:4000/go/python/2020/12/12/hello.json`
    ```json
    {"title":"Hello world","categories":["Go","Python"],"content":"Hello, world!"}
    {"title":"Hello world","categories":["Go","Python"],"content":"Hello, **world**\nI am here\n"}
    ```
  21. @MichaelCurrin MichaelCurrin revised this gist Dec 24, 2020. 1 changed file with 14 additions and 5 deletions.
    19 changes: 14 additions & 5 deletions page.md
    Original file line number Diff line number Diff line change
    @@ -34,10 +34,19 @@
    Visit at `http://localhost:4000/foo.json`
    ```json
    {"title":"Testing","links": [{"title":"Go track on Exercism","url":"https://exercism.io/tracks/go"},
    {"title":"Go tour welcome page","url":"https://tour.golang.org/welcome/1"},
    {"title":"Learn Go with Tests","url":"https://quii.gitbook.io/learn-go-with-tests/"},
    {"title":"Go for Python programmers","url":"https://golang-for-python-programmers.readthedocs.io/en/latest/index.html"}]}
    {"title":"Testing","links": [{"title":"Go track on Exercism","url":"https://exercism.io/tracks/go"},{"title":"Go tour welcome page","url":"https://tour.golang.org/welcome/1"},{"title":"Learn Go with Tests","url":"https://quii.gitbook.io/learn-go-with-tests/"},{"title":"Go for Python programmers","url":"https://golang-for-python-programmers.readthedocs.io/en/latest/index.html"}]}
    ```

    I wrapped the output for readability, but it render all one one line.
    Here it is in a more readable format:

    ```json
    {
    "title":"Testing",
    "links": [
    {"title":"Go track on Exercism","url":"https://exercism.io/tracks/go"},
    {"title":"Go tour welcome page","url":"https://tour.golang.org/welcome/1"},
    {"title":"Learn Go with Tests","url":"https://quii.gitbook.io/learn-go-with-tests/"},
    {"title":"Go for Python programmers","url":"https://golang-for-python-programmers.readthedocs.io/en/latest/index.html"}
    ]
    }
    ```
  22. @MichaelCurrin MichaelCurrin revised this gist Dec 24, 2020. 1 changed file with 6 additions and 1 deletion.
    7 changes: 6 additions & 1 deletion page.md
    Original file line number Diff line number Diff line change
    @@ -34,5 +34,10 @@
    Visit at `http://localhost:4000/foo.json`
    ```json
    {"title":"Testing","links": [{"title":"Go track on Exercism","url":"https://exercism.io/tracks/go"},{"title":"Go tour welcome page","url":"https://tour.golang.org/welcome/1"},{"title":"Learn Go with Tests","url":"https://quii.gitbook.io/learn-go-with-tests/"},{"title":"Go for Python programmers","url":"https://golang-for-python-programmers.readthedocs.io/en/latest/index.html"}]}
    {"title":"Testing","links": [{"title":"Go track on Exercism","url":"https://exercism.io/tracks/go"},
    {"title":"Go tour welcome page","url":"https://tour.golang.org/welcome/1"},
    {"title":"Learn Go with Tests","url":"https://quii.gitbook.io/learn-go-with-tests/"},
    {"title":"Go for Python programmers","url":"https://golang-for-python-programmers.readthedocs.io/en/latest/index.html"}]}
    ```

    I wrapped the output for readability, but it render all one one line.
  23. @MichaelCurrin MichaelCurrin revised this gist Dec 24, 2020. 1 changed file with 7 additions and 1 deletion.
    8 changes: 7 additions & 1 deletion post.md
    Original file line number Diff line number Diff line change
    @@ -2,14 +2,20 @@

    ### Input file

    Here we make a post in the usual `_posts` directory.

    The layout is also much lighter and more flexible - using the `data` object without caring about keys, values and formatting.

    The downside is that you have to be more verbose in your post frontmatter to define a YAML variable with `&` and then use it with `*`. Based on this [YAML tutorial](https://idratherbewriting.com/documentation-theme-jekyll/mydoc_yaml_tutorial.html).

    - `_layouts/post.html` - a layout which uses frontmatter and converts it to JSON output.
    ```liquid
    ---
    layout: none
    ---
    {{ page.data | jsonify }}
    ```
    - `_post/2020-12-12-hello.json` - a post with a `.json`extension.
    - `_posts/2020-12-12-hello.json` - a post with a `.json`extension.
    ```liquid
    ---
    layout: post
  24. @MichaelCurrin MichaelCurrin revised this gist Dec 24, 2020. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    # Jekyll JSON API

    How to make a read-only JSON REST API using Jekyll and some builtin templating features in Jekyll 3 or 4.
  25. @MichaelCurrin MichaelCurrin revised this gist Dec 24, 2020. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion page.md
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@
    ---
    {"title":{{ page.title | jsonify }},"links": {{ page.links | jsonify }}}
    ```
    - `foo.json`
    - `foo.json` - a page with a `.json` extension.
    ```liquid
    ---
    layout: page
    @@ -28,6 +28,7 @@
    ---
    ```
    ## Output file
    Visit at `http://localhost:4000/foo.json`
  26. @MichaelCurrin MichaelCurrin created this gist Dec 24, 2020.
    37 changes: 37 additions & 0 deletions page.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    ## Page

    - `_layouts/page.html`
    ```liquid
    ---
    layout: none
    ---
    {"title":{{ page.title | jsonify }},"links": {{ page.links | jsonify }}}
    ```
    - `foo.json`
    ```liquid
    ---
    layout: page
    title: Foo
    links:
    - title: Go track on Exercism
    url: https://exercism.io/tracks/go
    - title: Go tour welcome page
    url: https://tour.golang.org/welcome/1
    - title: Learn Go with Tests
    url: https://quii.gitbook.io/learn-go-with-tests/
    - title: Go for Python programmers
    url: https://golang-for-python-programmers.readthedocs.io/en/latest/index.html
    ---
    ```
    ## Output file
    Visit at `http://localhost:4000/foo.json`
    ```json
    {"title":"Testing","links": [{"title":"Go track on Exercism","url":"https://exercism.io/tracks/go"},{"title":"Go tour welcome page","url":"https://tour.golang.org/welcome/1"},{"title":"Learn Go with Tests","url":"https://quii.gitbook.io/learn-go-with-tests/"},{"title":"Go for Python programmers","url":"https://golang-for-python-programmers.readthedocs.io/en/latest/index.html"}]}
    ```
    36 changes: 36 additions & 0 deletions post.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    ## Post

    ### Input file

    - `_layouts/post.html` - a layout which uses frontmatter and converts it to JSON output.
    ```liquid
    ---
    layout: none
    ---
    {{ page.data | jsonify }}
    ```
    - `_post/2020-12-12-hello.json` - a post with a `.json`extension.
    ```liquid
    ---
    layout: post
    title: &title Hello world
    categories: &categories
    - Go
    - Python
    content: &content Hello, world!
    data:
    title: *title
    categories: *categories
    content: *content
    ---
    ```
    ### Output file
    Visit at `http://localhost:4000/go/python/2020/12/12/hello.json`
    ```json
    {"title":"Hello world","categories":["Go","Python"],"content":"Hello, world!"}
    ```