1
Fork 0
This repository has been archived on 2024-07-16. You can view files and clone it, but cannot push or open issues or pull requests.
wagtail-draftail-snippet/README.md

59 lines
3.5 KiB
Markdown
Raw Normal View History

2020-01-08 04:41:21 +00:00
# wagtail-draftail-snippet
Wagtail has support for adding numerous types of links to `RichText` content, but there is not a way to tie a link to an arbitrary `snippet` model currently. `wagtail-draftail-snippet` provides a way to create either an `a href` element for a specific `snippet` model based on a template that can be provided, or a completely free-form piece of HTML for a snippet.
2020-04-07 17:57:48 +01:00
2020-04-03 17:48:38 +01:00
The project provides the following draftail (Wagtail Richtext editor) features:
1. `snippet-link`: Allows to create a link using the snippet chooser modal. The link is rendered using the template `{app_name}/{model_name}_snippet_link.html`.
1. `snippet-embed`: Allows to embed a block using the snippet chooser modal. The block is embedded using the template `{app_name}/{model_name}_snippet_embed.html`.
2020-04-07 17:57:48 +01:00
Unsure whether you want to use a snippet link or embed? Embeds have complete flexibility, but 1) break the flow of text in the Draftail richtext editor and 2) can't be used to link arbitrary text. If you want to render a link to a snippet model inside of body copy, then you probably want to use the snippet link. For anything else, the embed can be used.
2020-01-08 04:41:21 +00:00
![Demo of wagtail-draftail-snippet plugin](draftail-snippet-demo.gif)
2020-01-08 04:41:21 +00:00
## Install
2020-02-11 19:54:50 +00:00
1. `pip install wagtail-draftail-snippet`
2020-01-08 04:41:21 +00:00
1. Add `wagtail_draftail_snippet` to `INSTALLED_APPS` in Django settings
2020-04-03 17:48:38 +01:00
1. Add `"snippet-link"` and `"snippet-embed"` to the `features` keyword list argument when instantiating a `RichTextBlock`, e.g. `paragraph = RichTextBlock(features=["bold", "italic", "h1", "h2", "h3", "snippet-link", "snippet-embed"])`
2022-02-17 21:59:35 +00:00
1. Create a frontend template to determine how the snippet model will be rendered. Frontend templates are required for a snippet to be selected and are discovered when they match a path like `{app_name}/{model_name}_snippet_link.html` and `{app_name}/{model_name}_snippet_embed.html`. For example, if you have an `Affiliate` snippet model in `affiliates/models.py`, then a file in `affiliates/templates/affiliates/affiliate_snippet_link.html` and `affiliates/templates/affiliates/affiliate_snippet_embed.html` would be required.
2020-01-08 04:41:21 +00:00
## Example use-case
Wagtail is used for a content site that will display articles that have affiliate links embedded inside the content. Affiliate links have a snippet data model to store information with a URL, start, and end dates; the urls need to be rendered in such a way that JavaScript can attach an event listener to their clicks for analytics.
When the content gets rendered, it uses the specific affiliate model to get the URL stored in the snippet model. If the affiliate's URL ever changes, the snippet can be changed in the Wagtail admin, and the all of the content will use the correct link when rendered.
2020-04-03 17:48:38 +01:00
An example frontend template in `affiliates/templates/affiliates/affiliate_snippet_link.html` could be the following.
2022-02-17 21:59:35 +00:00
2020-01-08 04:41:21 +00:00
```
<a href="{{ object.url }}" data-vars-action="content-cta" data-vars-label="{{ object.slug }}" rel="sponsored">
```
2020-02-12 10:10:03 +00:00
## Requirements
The package requires Wagtail 2.5 or above.
2020-01-08 04:41:21 +00:00
2020-01-17 19:10:32 +00:00
## Build the library
2020-01-08 04:41:21 +00:00
1. `poetry build`
2022-05-26 16:20:16 +01:00
## Push a new version release to PyPI
Travis-CI will build the library and deploy it to PyPI on every commit to `master` that is tagged with a version.
2020-02-15 03:23:14 +00:00
## Run tests
2022-02-17 21:59:35 +00:00
2020-04-07 17:57:48 +01:00
1. `poetry install`
2020-02-15 03:23:14 +00:00
1. `poetry run pytest`
2020-01-08 04:41:21 +00:00
## Contributors
- [Parbhat Puri](https://github.com/Parbhat)
- [Adam Hill](https://github.com/adamghill/)
2022-02-17 21:59:35 +00:00
- [Brady Moe](https://github.com/bmoe872/)
2020-01-17 19:10:32 +00:00
## License
[BSD](https://github.com/themotleyfool/wagtail-draftail-snippet/blob/master/LICENSE)