Skip to content
This repository has been archived by the owner on Apr 10, 2021. It is now read-only.

Enable using other Utils.slugify mode than the "default" + use predefined slug #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ contentful:
- "First Content Type"
- "Second Content Type"
published_locales_field: "published_languages"
slugify_mode: "default"
localization:
- locale: en-US
url_prefix: ""
Expand All @@ -52,6 +53,21 @@ contentful:
```


### Slugs used in URLs

You can specify slug used in URL by using content field *slug* in the Contentful side, for example:

```{
"title": "My cool title",
"slug": "my-cool-title",
...
}
```

Without *slug* field, the URLs will be slugified with Jekyll's slugify `default` mode. You can specify also slugify modes to `raw`, `pretty` or `ascii`.

If you plan to combine JavaScript to your website and use Contentful via REST/AJAX interface, it is easiest to use `ascii` mode (and match the slugification process with JavaScript regex `/^[a-z0-9]+$/i`).

### Translations
You can globally specify the languages/locales that you want to generate pages for.
See the ``localization`` array in the ``_config.yml`` example above. You need to set the ``locale``, a string that this locale's urls will be prefixed with (``url_prefix``) and the language name (``lang``). The ``lang`` setting is used by the language switcher.
Expand Down
11 changes: 9 additions & 2 deletions lib/jekyll-contentful.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,15 @@ def initialize(site, entry, content_type_name, prefix)
self.data["contentful_locale"] = entry.locale
self.data["contentful_content_type_name"] = content_type_name

# If there is a title fields make it the url
page_title_slug = Utils.slugify(self.data["title"] || "")
# If there is a slug field make it the url, otherwise try slugifying title
page_title_slug = ""
if self.data["slug"]
page_title_slug = self.data["slug"]
elsif self.data["title"]
slugify_mode = site.config["contentful"]["slugify_mode"] || "default"
page_title_slug = Utils.slugify(self.data["title"], mode: slugify_mode)
end

@dir = "/#{prefix}#{content_type_slug}/#{page_title_slug}"

self.process(@name)
Expand Down