Make raw /_data file viewable on site [duplicate] - jekyll

I have a data file which is being used inside a Jekyll template:
/{project}/_data/mydata.json
I also want this data to be available on the live website from a JavaScript:
/{project}/_site/mydata.json
Somehow I want one of the following:
Automatically copy itself from "_data" to "_sites" whenever changes are made.
Have the template read the data file from /{project}/mydata.json since this file will already get copied to the "_sites" folder.
What is the easiest way to maintain a single version of the data file inside both Jekyll templates and JavaScripts?

File /{project}/mydata.json
---
layout: null
---
{{ site.data.mydata | jsonify }}
With jekyll serve or on github, this will be updated each time your /{project}/_data/mydata.json is updated.
Et voilà !

Related

How can I use a Liquid-generated JSON as a "_data" site.data object in Jekyll?

Using Liquid, I am trying to build a JSON object (skills.json) containing data from all of my Jekyll posts.
When I place this file in my _data folder in my project root directory per https://jekyllrb.com/docs/datafiles/, trying to access the data via site.data.skills with the inspect filter or console log resolves to nothing.
final output section of my Liquid json
When I instead place my skills.json file in the /assets/js/ folder in my root, I do see that the properly populated JSON file is added to my _site folder as expected. Copying this NEW file into the root _data folder successfully populates to my page as intended, and I am able to access all the data with site.data.skills.KEY.
json generated from assets folder at build time
Is there any way that I could specify that the generated skills.json in my /assets/js/ folder be the source for my data call?
Alternatively, is there a way to generate the final data and automatically move it to the _data folder ahead of building the site? I am open to any suggestions for how to automate this. As a warning, I am pretty new to web development in general, so any references or links would be a tremendous help. Thanks!
Following up on this, I believe that the issue lies in how the site is built. The skills.json file gets generated on the first build, and then when it is added to the _data folder, the site gets built AGAIN to update everything that hits that data. With this iterative process, I don't know if it would be possible to both generate the new file and use it as a source to update everything dependent on the data in the same pass.
As far as automating goes, my thinking is that a Ruby plugin to compare the newly generated files against the _data folder and overwrite if the source is different/newer would be the way to go, but I'm still open to any suggestions!

Why does removing yaml front matter stop Jekyll from converting md file to html

I've inherited a process that converts markdown content into html using jekyll.
If I remove the yaml front matter between ---, by client request to simplify the process for editors,
---
product: Product Name
capability: testing
infotype: Overview
audience:
---
# Testing file 2
This is another testing file.
The jekyll build doesn't convert the file.
# Testing file 2
This is another testing file.
When I have the front matter in the testing 2 file I see the following in the log when running build --verbose
Rendering: user-administration/testing-file-2.md
Pre-Render Hooks: user-administration/testing-file-2.md
Rendering Markup: user-administration/testing-file-2.md
Rendering Layout: user-administration/testing-file-2.md
but without the front matter there is no message in the log related to testing-file-2.md
This testing-file-2.md is part of a collection of other files that have metadata. They are render into an html website but not the testing-file-2.md when the metadata is removed.
Is there a way for jekyll to build and render files without front matter?
Jekyll does not ignore any files. Rather, for each file, it decides whether the file is:
a static file, which can be copied as-is to the output folder (_site), or
a file to be processed first.
Markdown files (.md) are processed by kramdown and Liquid if they start with YAML frontmatter:
---
---
otherwise they are treated as static files, and copied to _site with no processing.
There is a workaround that might work for you using include_relative; but it may cause more trouble for your client's editors than it's worth, depending how they work.
You can include a static file inside a file to be processed. Your static file might be plain-text.md:
# Static text file
This is a file with no YAML frontmatter.
Then, separately, you create a markdown file with frontmatter that will include the plain-text file inside it. Say, processed-text.md:
---
---
{% include_relative plaintext.md %}
Then your plain text will be processed and appear on your site as /processed-text. Think of the file processed-text.md as a kind of template for holding plain-text.md.
You'll want to see the docs on include_relative, especially the fact that the file to be included can't be above the including file in the file system.
I've read that it's a requirement to have at minimum at empty front matter or jekyll will ignore the file
---
---

Can you define Jekyll config defaults from variables in another file?

I'm trying to do this because I'm using a CMS where users will be able to edit a data file to make changes to the page instead of the _config.yml.
I'm wondering if it's possible to reference a variable from the data file and place this reference within the _config.yml.
Here's an example of what I'm trying to do;
Data File (/_data/site-data.yml)
navigation:
navigation_colour: '#462634'
Config File (/_config.yml)
defaults:
-
values:
navigation:
navigation_colour: site.data.site-data.navigation.navigation-colour
Is something similar to this possible?
Thanks!
You can assign at least in one config-file variables, I have not tested this over multiple files.
Variables inside YAML
YAML, hello will become Greetings earthling!
something: &hello Greetings earthling!
myref: *hello
MARKDOWN
{{ site.data.samplelist.myref }}
Jekyll does not parse variables in _config.yml. However inside your blog you can use liquid tags like {{site-data.navigation.navigation-colour}}. See here.
If its mandate to replace variables in _config.yml then use a custom or standard replacement plugin with grunt. So effective grunt build task will first perform token replacement in _config.yml and then do jekyll build.

How do I provide files for download?

I have written a post and would like to provide a file as attachment for download.
So far I have
created a link [Download files][download-file]
declared the link as [download-file]: download.zip
put download.zip in the _posts folder
However, the file does not get copied to the correct location.
I have
read about data files
read about directory structure
read about static files, which IMHO was the closest hit. Unfortunately, no example is provided.
So neither page gave me an answer on how to provide files for download.
I don't know where to put static files in the directory structure. Once I knew that, it seems I could use some Liquid like {{ site.static_files }}, but even then I'd still need to specify a file name.
Here is my solution. I have a "myname".github.io, I created a folder /download inside the myname.github.io and inside this download folder I uploaded the files I want to include in my posts.
In the single post then I created the following code:
some text and [here is possible to download the file in PDF][1]
[1]:{{ site.url }}/download/file.pdf
You can place your download.zip file in the root of your project, and it will get copied over to {{ site.url }}/download.zip. If you'd like it contained in a folder, you can create any folder, eg download/, and any file will be able to accessed via {{ site.url }}/download/*.
Every other directory and file except for [the Jekyll files]—such as css and images folders, favicon.ico files, and so forth—will be copied verbatim to the generated site.
from the Jekyll Docs

Copy data file to '_sites" in Jekyll project

I have a data file which is being used inside a Jekyll template:
/{project}/_data/mydata.json
I also want this data to be available on the live website from a JavaScript:
/{project}/_site/mydata.json
Somehow I want one of the following:
Automatically copy itself from "_data" to "_sites" whenever changes are made.
Have the template read the data file from /{project}/mydata.json since this file will already get copied to the "_sites" folder.
What is the easiest way to maintain a single version of the data file inside both Jekyll templates and JavaScripts?
File /{project}/mydata.json
---
layout: null
---
{{ site.data.mydata | jsonify }}
With jekyll serve or on github, this will be updated each time your /{project}/_data/mydata.json is updated.
Et voilà !