I found a solution to this on Jessica Reel's blog.
To your default.html
layout, in your head (or better yet at the end of your body, unless your scripts need to be in the head), add something like this:
{% if page.custom_js %}
{% for js_file in page.custom_js %}
<script type="text/javascript" src="{{ site.baseurl }}/assets/js/{{ js_file }}.js"></script>
{% endfor %}
{% endif %}
Then in your page's front-matter, add:
custom_js:
- example-js-file-1
- example-js-file-2
Exact same principle works great for CSS, too.
I ended up taking it a step further with my own website since I use nested layouts. Jekyll 3 so has a separate variable namespace for layout variables, so in the head
of my main layout I specify first the layout's custom css then the page's custom css:
{% if layout.custom_css %}
{% for stylesheet in layout.custom_css %}
<link rel="stylesheet" href="/assets/css/{{ stylesheet }}.css">
{% endfor %}
{% endif %}
{% if page.custom_css %}
{% for stylesheet in page.custom_css %}
<link rel="stylesheet" href="/assets/css/{{ stylesheet }}.css">
{% endfor %}
{% endif %}
So you can see that it applies the custom css from the layout's front matter, then the custom css from the specific page's front matter, so that stuff cascades properly from general to specific styles.