Add get_section fn and update readme

This commit is contained in:
Vincent Prouillet 2017-05-23 20:03:25 +09:00
parent 958f1cf88c
commit bae3ade471
4 changed files with 35 additions and 7 deletions

View file

@ -4,9 +4,11 @@
- Fix missing serialized data for sections
- Change the single item template context for categories/tags
- Add a `get_url` global Tera function
- Add a `get_url` and a `get_section` global Tera function
- Add a config option to control how many articles to show in RSS feed
- Move `insert_anchor_links` from config to being a section option
- Move `insert_anchor_links` from config to being a section option and it can
now be insert left or right
## 0.0.5 (2017-05-15)

View file

@ -169,10 +169,13 @@ to link to. The path to the file starts from the `content` directory.
For example, linking to a file located at `content/pages/about.md` would be `[my link](./pages/about.md)`.
### Anchors
Headers get an automatic id from their content in order to be able to add deep links. By default no links are actually created but
the `insert_anchor_links` option in `config.toml` can be set to `true` to link tags. The default template is very ugly and will need
CSS tweaks in your projet to look decent. The default template can also be easily overwritten by creating a `anchor-link.html` file in
the `templates` directory.
Headers get an automatic id from their content in order to be able to add deep links.
You can also choose, at the section level, whether to automatically insert an anchor link next to it. It is turned off by default
but can be turned on by setting `insert_anchor = "left"` or `insert_anchor = "right"` in the `_index.md` file. `left` will insert
the anchor link before the title text and right will insert it after.
The default template is very basic and will need CSS tweaks in your projet to look decent.
It can easily be overwritten by creating a `anchor-link.html` file in the `templates` directory.
### Shortcodes
Gutenberg uses markdown for content but sometimes you want to insert some HTML, for example for a YouTube video.

View file

@ -132,6 +132,7 @@ impl Site {
self.populate_tags_and_categories();
self.tera.register_global_function("get_page", global_fns::make_get_page(&self.pages));
self.tera.register_global_function("get_section", global_fns::make_get_section(&self.sections));
self.register_get_url_fn();
Ok(())

View file

@ -3,7 +3,7 @@ use std::path::{PathBuf};
use tera::{GlobalFn, Value, from_value, to_value, Result};
use content::Page;
use content::{Page, Section};
use site::resolve_internal_link;
@ -29,6 +29,28 @@ pub fn make_get_page(all_pages: &HashMap<PathBuf, Page>) -> GlobalFn {
})
}
pub fn make_get_section(all_sections: &HashMap<PathBuf, Section>) -> GlobalFn {
let mut sections = HashMap::new();
for section in all_sections.values() {
sections.insert(section.file.relative.clone(), section.clone());
}
Box::new(move |args| -> Result<Value> {
match args.get("path") {
Some(val) => match from_value::<String>(val.clone()) {
Ok(v) => {
match sections.get(&v) {
Some(p) => Ok(to_value(p).unwrap()),
None => Err(format!("Section `{}` not found.", v).into())
}
},
Err(_) => Err(format!("`get_section` received path={:?} but it requires a string", val).into()),
},
None => Err("`get_section` requires a `path` argument.".into()),
}
})
}
pub fn make_get_url(permalinks: HashMap<String, String>,) -> GlobalFn {
Box::new(move |args| -> Result<Value> {
match args.get("link") {