-
-
Save matbrgz/2c8db43d0ec3a9bd1670599a9e7039a8 to your computer and use it in GitHub Desktop.
Revisions
-
MichaelCurrin revised this gist
May 22, 2022 . 2 changed files with 19 additions and 9 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 | 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. This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 posts. ## Input file - `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 | 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":"/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 set a permalink as: ```yaml permalink: /api/posts/ ``` Then you can access it as `/api/posts/` instead of `/api/posts.json`. -
MichaelCurrin revised this gist
Nov 3, 2021 . 2 changed files with 89 additions and 88 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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. This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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. -
MichaelCurrin revised this gist
Nov 3, 2021 . 4 changed files with 4 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ # All pages endpoint Make a single JSON file containing data for all pages. This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ # Pages as JSON files Render each Markdown page as a JSON file, using frontmatter for data. Note that no HTML file is generated here. This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ # All posts endpoint Make a single JSON file containing data for all pages. This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ # Posts as JSON files How to output each Markdown post into a JSON file. -
MichaelCurrin revised this gist
Nov 3, 2021 . 5 changed files with 11 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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. Alternatively, you can replace every HTML page and post with a JSON version. ## Data source This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,8 @@ # Page listing 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. This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,6 @@ # Page 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 This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,6 @@ # Post listing Make a single JSON file containing data for all pages. ## Input file This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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. -
MichaelCurrin revised this gist
Nov 3, 2021 . 3 changed files with 6 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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. 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 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. This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ # Page Render a single page with frontmatter as a JSON file. Note that no HTML file is generated here. This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ # Post sample ## Input file -
MichaelCurrin revised this gist
Oct 24, 2021 . 1 changed file with 16 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,11 +1,23 @@ # 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 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. -
MichaelCurrin revised this gist
Aug 16, 2021 . 2 changed files with 9 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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. 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": {{- 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 -
MichaelCurrin revised this gist
Aug 16, 2021 . 3 changed files with 72 additions and 16 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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`. This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 --- This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,42 +1,59 @@ # Post listing Make JSON endpoint listing all posts on your site. ## Input file Add `content` if you want that too, but it will make the output a lot longer. - `api/posts.json` ```liquid --- layout: none --- [ {%- for post in site.posts %} { "id": {{- post.id | jsonify -}}, "title": {{- post.title | jsonify }}, "date": {{- post.date | jsonify }}, "url": {{- post.url | jsonify }}, "tags": {{- post.tags | jsonify }}, "categories": {{- post.categories | jsonify }} } {% unless forloop.last %},{% endunless %} {% endfor -%} ] ``` ## Output file - `/posts/` ```json5 [ { "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`. -
MichaelCurrin revised this gist
Jan 13, 2021 . 3 changed files with 13 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,5 @@ # 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. This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,6 @@ # Post ## 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 Visit at `http://localhost:4000/go/python/2020/12/12/hello.json` This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,9 +1,9 @@ # Post listing If you want to make as summary of all your posts. ## 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/` -
MichaelCurrin revised this gist
Jan 10, 2021 . 1 changed file with 31 additions and 9 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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. 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. @@ -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 -
MichaelCurrin revised this gist
Jan 10, 2021 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 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 -
MichaelCurrin revised this gist
Jan 10, 2021 . 1 changed file with 29 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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. 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: ```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 -
MichaelCurrin revised this gist
Jan 10, 2021 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ # 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. -
MichaelCurrin revised this gist
Dec 24, 2020 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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/` ```json5 [ { "title": "Hello world", -
MichaelCurrin revised this gist
Dec 24, 2020 . 3 changed files with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes.File renamed without changes.File renamed without changes. -
MichaelCurrin revised this gist
Dec 24, 2020 . 3 changed files with 2 additions and 1 deletion.There are no files selected for viewing
File renamed without changes.File renamed without changes.This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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... ] ``` -
MichaelCurrin revised this gist
Dec 24, 2020 . 1 changed file with 40 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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" } ] ``` -
MichaelCurrin revised this gist
Dec 24, 2020 . 1 changed file with 21 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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. -
MichaelCurrin revised this gist
Dec 24, 2020 . 2 changed files with 6 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,11 +1,11 @@ ## Page - `_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 | 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":"<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":"<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"}, This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 --- -
MichaelCurrin revised this gist
Dec 24, 2020 . 2 changed files with 12 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -5,14 +5,17 @@ --- layout: none --- {"title":{{ page.title | jsonify }},"content":{{ page.body | jsonify }},"links":{{ page.links | jsonify }}} ``` - `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","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"}, This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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** 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**\nI am here\n"} ``` -
MichaelCurrin revised this gist
Dec 24, 2020 . 1 changed file with 14 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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"}]} ``` 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"} ] } ``` -
MichaelCurrin revised this gist
Dec 24, 2020 . 1 changed file with 6 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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"}]} ``` I wrapped the output for readability, but it render all one one line. -
MichaelCurrin revised this gist
Dec 24, 2020 . 1 changed file with 7 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 }} ``` - `_posts/2020-12-12-hello.json` - a post with a `.json`extension. ```liquid --- layout: post -
MichaelCurrin revised this gist
Dec 24, 2020 . 1 changed file with 3 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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. -
MichaelCurrin revised this gist
Dec 24, 2020 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -7,7 +7,7 @@ --- {"title":{{ page.title | jsonify }},"links": {{ page.links | jsonify }}} ``` - `foo.json` - a page with a `.json` extension. ```liquid --- layout: page @@ -28,6 +28,7 @@ --- ``` ## Output file Visit at `http://localhost:4000/foo.json` -
MichaelCurrin created this gist
Dec 24, 2020 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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"}]} ``` This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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!"} ```