Skip to content
This repository has been archived by the owner on May 8, 2024. It is now read-only.

Rename StructuredText to RichText #66

Merged
merged 1 commit into from
Oct 15, 2018
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## Unreleased
### Added
* Added capability to autoload mappers [#22](https://github.com/contentful/jekyll-contentful-data-import/issues/22)
* *This feature is currently in ALPHA state, changes may occur as patch fixes* Added support for StructuredText and added the `structured_text` view filter.
* *This feature is currently in BETA state, changes may occur as patch fixes* Added support for RichText and added the `rich_text` view filter.

## v1.7.0
### Added
Expand Down
44 changes: 22 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,24 +60,24 @@ contentful:
base_path: app_path # Optional - Defaults to Current directory
destination: destination_in_data # Optional - Defaults to _data/contentful/spaces
individual_entry_files: true # Optional - Defaults to false
structured_text_mappings: # Optional - Defaults to {}
rich_text_mappings: # Optional - Defaults to {}
embedded-entry-block: MyEntryRenderer
```

Parameter | Description
---------- | ------------
space | Contentful Space ID
access_token | Contentful Delivery API access token
environment | Space environment, defaults to `master`
cda_query | Hash describing query configuration. See [contentful.rb](https://github.com/contentful/contentful.rb) for more info (look for filter options there). Note that by default only 100 entries will be fetched, this can be configured to up to 1000 entries using the `limit` option.
all_entries | Boolean, if true will run multiple queries to the API until it fetches all entries for the space
all_entries_page_size | Integer, the amount of maximum entries per CDA Request when fetching :all_entries
content_types | Hash describing the mapping applied to entries of the imported content types
client_options | Hash describing `Contentful::Client` configuration. See [contentful.rb](https://github.com/contentful/contentful.rb) for more info.
base_path | String with path to your Jekyll Application, defaults to current directory. Path is relative to your current location.
destination | String with path within `_data` under which to store the output yaml file. Defaults to contentful/spaces
individual_entry_files | Boolean, if true will create an individual file per entry separated in folders by content type, file path will be `{space_alias}/{content_type_id}/{entry_id}.yaml`. Default behavior is to create a file per space. Usage is affected when this is set to true, please look in the section below.
structured_text_mappings | Hash with `'nodeTyoe' => RendererClass` pairs determining overrides for the [`StructuredTextRenderer` library](https://github.com/contentful/structured-text-renderer.rb) configuration.
Parameter | Description
---------- | ------------
space | Contentful Space ID
access_token | Contentful Delivery API access token
environment | Space environment, defaults to `master`
cda_query | Hash describing query configuration. See [contentful.rb](https://github.com/contentful/contentful.rb) for more info (look for filter options there). Note that by default only 100 entries will be fetched, this can be configured to up to 1000 entries using the `limit` option.
all_entries | Boolean, if true will run multiple queries to the API until it fetches all entries for the space
all_entries_page_size | Integer, the amount of maximum entries per CDA Request when fetching :all_entries
content_types | Hash describing the mapping applied to entries of the imported content types
client_options | Hash describing `Contentful::Client` configuration. See [contentful.rb](https://github.com/contentful/contentful.rb) for more info.
base_path | String with path to your Jekyll Application, defaults to current directory. Path is relative to your current location.
destination | String with path within `_data` under which to store the output yaml file. Defaults to contentful/spaces
individual_entry_files | Boolean, if true will create an individual file per entry separated in folders by content type, file path will be `{space_alias}/{content_type_id}/{entry_id}.yaml`. Default behavior is to create a file per space. Usage is affected when this is set to true, please look in the section below.
rich_text_mappings | Hash with `'nodeTyoe' => RendererClass` pairs determining overrides for the [`RichTextRenderer` library](https://github.com/contentful/rich-text-renderer.rb) configuration.

You can add multiple spaces to your configuration

Expand Down Expand Up @@ -121,19 +121,19 @@ With the autoload mechanism, there is no longer a need to create a `rake` task f

If you already have a custom `rake` task, the new autoload mechanism will not affect it from working as it was working previously.

### Structured Text *ALPHA*
### Rich Text *Beta*

To render structured text in your views, you can use the `structured_text` filter:
To render rich text in your views, you can use the `rich_text` filter:

```liquid
{{ entry.structured_text_field | structured_text }}
{{ entry.rich_text_field | rich_text }}
```

This will output the generated HTML generated by the [`StructuredTextRenderer` library](https://github.com/contentful/structured-text-renderer.rb).
This will output the generated HTML generated by the [`RichTextRenderer` library](https://github.com/contentful/rich-text-renderer.rb).

#### Adding custom renderers

When using structured text, if you're planning to embed entries, then you need to create your custom renderer for them. You can read how create your own renderer classes [here](https://github.com/contentful/structured-text-renderer.rb#using-different-renderers).
When using rich text, if you're planning to embed entries, then you need to create your custom renderer for them. You can read how create your own renderer classes [here](https://github.com/contentful/rich-text-renderer.rb#using-different-renderers).

To configure the mappings, you need to add them in your `contentful` block like follows:

Expand All @@ -142,7 +142,7 @@ contentful:
spaces:
- example:
# ... all the regular config ...
structured_text_mappings:
rich_text_mappings:
embedded-entry-block: MyCustomRenderer
```

Expand All @@ -156,7 +156,7 @@ In case you have multiple configured spaces, and have different mapping configur

The helper receives an additional optional parameter for the space name. By default it is `nil`, indicating the first available space.

So, if for example you have 2 spaces with different configurations, to use the space called `foo`, you should call the helper as: `{{ entry.field | structured_text: "foo" }}`.
So, if for example you have 2 spaces with different configurations, to use the space called `foo`, you should call the helper as: `{{ entry.field | rich_text: "foo" }}`.

### Hiding Space and Access Token in Public Repositories

Expand Down
2 changes: 1 addition & 1 deletion jekyll-contentful.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Gem::Specification.new do |s|

# Additional dependencies
s.add_dependency("contentful", '~> 2.1')
s.add_dependency("structured_text_renderer", '~> 0.0', '> 0.0.1')
s.add_dependency("rich_text_renderer", '~> 0.1')

s.add_development_dependency 'rubygems-tasks', '~> 0.2'
s.add_development_dependency "guard"
Expand Down
14 changes: 7 additions & 7 deletions lib/jekyll-contentful-data-import/helpers.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
require 'structured_text_renderer'
require 'rich_text_renderer'

module Jekyll
module Contentful
# Liquid filter for the StructuredText field.
module StructuredTextFilter
def structured_text(field, space = nil)
# Liquid filter for the RichText field.
module RichTextFilter
def rich_text(field, space = nil)
return if field.nil?

StructuredTextRenderer::Renderer.new(mappings_for(space)).render(field)
RichTextRenderer::Renderer.new(mappings_for(space)).render(field)
end

private

def mappings_for(space)
mappings = {}
config_for(space).fetch('structured_text_mappings', {}).each do |k, v|
config_for(space).fetch('rich_text_mappings', {}).each do |k, v|
mappings[k.to_s] = Module.const_get(v)
end

Expand All @@ -32,4 +32,4 @@ def config_for(space)
end
end

Liquid::Template.register_filter(Jekyll::Contentful::StructuredTextFilter)
Liquid::Template.register_filter(Jekyll::Contentful::RichTextFilter)
20 changes: 10 additions & 10 deletions spec/jekyll-contentful/helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,26 @@ def initialize(config)
end

class MockSiteContext
include Jekyll::Contentful::StructuredTextFilter
include Jekyll::Contentful::RichTextFilter

def initialize(config)
@context = MockContext.new(config)
end
end

class MyRenderer < StructuredTextRenderer::BaseNodeRenderer
class MyRenderer < RichTextRenderer::BaseNodeRenderer
def render(node)
return "<div>I eat nodes for breakfast</div>"
end
end

describe Jekyll::Contentful::StructuredTextFilter do
describe Jekyll::Contentful::RichTextFilter do
let(:config) do
{
'contentful' => {
'spaces' => [
'foo' => {
'structured_text_mappings' => {
'rich_text_mappings' => {
'embedded-entry-block' => 'MyRenderer'
}
},
Expand All @@ -48,7 +48,7 @@ def render(node)
}
end

let(:st_field) do
let(:rt_field) do
{
"content" => [
{
Expand Down Expand Up @@ -650,7 +650,7 @@ def render(node)
"nodeClass" => "block"
}
],
"nodeType" => "quote",
"nodeType" => "blockquote",
"nodeClass" => "block"
},
{
Expand All @@ -677,18 +677,18 @@ def render(node)

subject { MockSiteContext.new(config) }

describe 'renders structured text' do
describe 'renders rich text' do
it 'by defaults uses first available config' do
result = subject.structured_text(st_field)
result = subject.rich_text(rt_field)

expect(result).to include("<div>I eat nodes for breakfast</div>")
end

it 'can define which space configuration to use' do
result = subject.structured_text(st_field, 'foo')
result = subject.rich_text(rt_field, 'foo')
expect(result).to include("<div>I eat nodes for breakfast</div>")

result = subject.structured_text(st_field, 'bar')
result = subject.rich_text(rt_field, 'bar')
expect(result).not_to include("<div>I eat nodes for breakfast</div>")
end
end
Expand Down