Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Adding caching and improving config for fragment includes. Version bump.
  • Loading branch information
ianjevans committed Mar 17, 2021
commit 818176e4b5cc9d5dfb1d10fb7d3ce852cdccd518
30 changes: 22 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ A Liquid tag plugin for Jekyll that allows remote includes from external sources
## Installation

Add this line to your application's Gemfile:

```ruby
group :jekyll_plugins do
gem 'jekyll-remote-include', "~> 1.1.3", github: 'ianjevans/jekyll-remote-include'
gem 'jekyll-remote-include', "~> 1.1.4", github: 'ianjevans/jekyll-remote-include'
end
```

Expand All @@ -30,7 +31,7 @@ plugins:

## Usage

The tag can be used to include the entire contents of a file, or just the content between begin and end tokens, in your Jekyll pages, posts and collections.
The tag can be used to include the entire contents of a file, or just the content between begin and end tokens, in your Jekyll pages, posts and collections. The remote content is [cached](https://jekyllrb.com/tutorials/cache-api/) to improve build times.

To include the entire file:

Expand All @@ -44,12 +45,25 @@ To include only a portion of the file, specify the begin and end tokens separate
{% remote_include https://raw.githubusercontent.com/jekyll/jekyll/master/README.markdown|begin_token|end_token %}
```

If the linked file doesn't contain the begin or end tokens, the entire file will be included.
The `on_token_error` configuration option handles scenarios where the linked file doesn't contain the begin or end tokens. The build log will include information about which file contains the token error and whether the begin or end token is missing.

Set `on_token_error` to `fail` (the default) to stop the build with a failure if the begin or end token doesn't exist in the target file.

Set `on_token_error` to `full` if you want to include the entire remote file if there are token errors.

Set `on_token_error` to `none` if you want no content from the `remote_include`.

To use `on_token_error` add a `remote_include` section to your site's `_config.yml` and set `on_token_error`.

```yaml
remote_include:
on_token_error: full
```

## Contributing

1. Fork it.
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create a new Pull Request
1. Fork it.
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create a new Pull Request
2 changes: 1 addition & 1 deletion jekyll-remote-include.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)

Gem::Specification.new do |spec|
spec.name = "jekyll-remote-include"
spec.version = "1.1.3"
spec.version = "1.1.4"
spec.authors = ["Jason Maggiacomo", "Kris Northfield", "Ian Evans"]
spec.email = ["[email protected]"]
spec.summary = "A Liquid tag plugin for Jekyll that allows remote includes from external assets."
Expand Down
25 changes: 21 additions & 4 deletions lib/jekyll-remote-include.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,24 @@ module Jekyll

class RemoteInclude < Liquid::Tag

def cache
@@cache ||= Jekyll::Cache.new("RemoteInclude")
end

def initialize(tag_name, remote_include, tokens)
super
@remote_include = remote_include
end

# Returns the plugin's config or an empty hash if not set
def config
@config ||= @site.config["remote_include"] || {}
end

def open(url)
Net::HTTP.get(URI.parse(url.strip)).force_encoding 'utf-8'
cache.getset(url) do
Net::HTTP.get(URI.parse(url.strip)).force_encoding 'utf-8'
end
end

def render(context)
Expand All @@ -33,16 +44,22 @@ def render(context)
else
begin_match = raw.match?(begin_token) ? "True" : "False"
end_match = raw.match?(end_token) ? "True" : "False"
# if the file doesn't have the begin and end token just output the entire file
output = raw
@logger.warn("Remote file fragment doesn't contain begin and end token.")
@logger.warn("Page: " << context.environments.first["page"]["path"])
@logger.warn("Url: " << url)
@logger.warn("Begin token: " << begin_token)
@logger.warn("Begin token present? " << begin_match)
@logger.warn("End token: " << end_token)
@logger.warn("End token present? " << end_match)
raise ArgumentError, "Remote fragment include error in " << context.environments.first["page"]["path"]
if config["on_token_error"] == "fail" || !config["on_token_error"]
# Fail the build by default if there are token errors
raise ArgumentError, "Remote fragment include error in " << context.environments.first["page"]["path"]
elsif config["on_token_error"] == "full"
# if the file doesn't have the begin and end token just output the entire file
output = raw
elsif config["on_token_error"] == "none"
output = ""
end
end
else
output = open(url)
Expand Down