Allow links to .md pages in pages contents
This commit is contained in:
parent
ad03aba945
commit
3aa2b89451
|
@ -6,3 +6,4 @@
|
||||||
- Add sections to the index page context
|
- Add sections to the index page context
|
||||||
- Fix page rendering not working when containing `+++`
|
- Fix page rendering not working when containing `+++`
|
||||||
- Add shortcodes (see README for details)
|
- Add shortcodes (see README for details)
|
||||||
|
- Allow relative links to other content in markdown links
|
||||||
|
|
|
@ -150,6 +150,14 @@ built-in:
|
||||||
A gallery containing lots of themes at https://tmtheme-editor.herokuapp.com/#!/editor/theme/Agola%20Dark.
|
A gallery containing lots of themes at https://tmtheme-editor.herokuapp.com/#!/editor/theme/Agola%20Dark.
|
||||||
More themes can be easily added to gutenberg, just make a PR with the wanted theme.
|
More themes can be easily added to gutenberg, just make a PR with the wanted theme.
|
||||||
|
|
||||||
|
### Internal links
|
||||||
|
You can have internal links in your markdown that will be replaced with the full URL when rendering.
|
||||||
|
To do so, use the normal markdown link syntax, start the link with `./` and point to the `.md` file you want
|
||||||
|
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).
|
||||||
|
|
||||||
|
|
||||||
### Shortcodes
|
### Shortcodes
|
||||||
Gutenberg uses markdown for content but sometimes you want to insert some HTML, for example for a YouTube video.
|
Gutenberg uses markdown for content but sometimes you want to insert some HTML, for example for a YouTube video.
|
||||||
Rather than copy/pasting the HTML around, Gutenberg supports shortcodes, allowing you to define templates using Tera and call those templates inside markdown.
|
Rather than copy/pasting the HTML around, Gutenberg supports shortcodes, allowing you to define templates using Tera and call those templates inside markdown.
|
||||||
|
|
|
@ -204,6 +204,22 @@ pub fn markdown_to_html(content: &str, permalinks: &HashMap<String, String>, ter
|
||||||
highlighter = None;
|
highlighter = None;
|
||||||
Event::Html(Owned("</pre>".to_owned()))
|
Event::Html(Owned("</pre>".to_owned()))
|
||||||
},
|
},
|
||||||
|
// Need to handle relative links
|
||||||
|
Event::Start(Tag::Link(ref link, ref title)) => {
|
||||||
|
if link.starts_with("./") {
|
||||||
|
let permalink = match permalinks.get(&link.replacen("./", "", 1)) {
|
||||||
|
Some(p) => p,
|
||||||
|
None => {
|
||||||
|
error = Some(format!("Relative link {} not found.", link).into());
|
||||||
|
return Event::Html(Owned("".to_string()));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
return Event::Start(Tag::Link(Owned(permalink.clone()), title.clone()));
|
||||||
|
}
|
||||||
|
|
||||||
|
return Event::Start(Tag::Link(link.clone(), title.clone()));
|
||||||
|
},
|
||||||
|
// need to know when we are in a code block to disable shortcodes in them
|
||||||
Event::Start(Tag::Code) => {
|
Event::Start(Tag::Code) => {
|
||||||
in_code_block = true;
|
in_code_block = true;
|
||||||
event
|
event
|
||||||
|
@ -212,6 +228,7 @@ pub fn markdown_to_html(content: &str, permalinks: &HashMap<String, String>, ter
|
||||||
in_code_block = false;
|
in_code_block = false;
|
||||||
event
|
event
|
||||||
},
|
},
|
||||||
|
// If we added shortcodes, don't close a paragraph since there's none
|
||||||
Event::End(Tag::Paragraph) => {
|
Event::End(Tag::Paragraph) => {
|
||||||
if added_shortcode {
|
if added_shortcode {
|
||||||
added_shortcode = false;
|
added_shortcode = false;
|
||||||
|
@ -219,6 +236,7 @@ pub fn markdown_to_html(content: &str, permalinks: &HashMap<String, String>, ter
|
||||||
}
|
}
|
||||||
event
|
event
|
||||||
},
|
},
|
||||||
|
// Ignore softbreaks inside shortcodes
|
||||||
Event::SoftBreak => {
|
Event::SoftBreak => {
|
||||||
if shortcode_block.is_some() {
|
if shortcode_block.is_some() {
|
||||||
return Event::Html(Owned("".to_owned()));
|
return Event::Html(Owned("".to_owned()));
|
||||||
|
@ -369,4 +387,33 @@ A quote
|
||||||
"#, &HashMap::new(), &tera, &Config::default()).unwrap();
|
"#, &HashMap::new(), &tera, &Config::default()).unwrap();
|
||||||
assert_eq!(res, "<p>Hello\n</p><blockquote>A quote - Keats</blockquote>");
|
assert_eq!(res, "<p>Hello\n</p><blockquote>A quote - Keats</blockquote>");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_markdown_to_html_unknown_shortcode() {
|
||||||
|
let res = markdown_to_html("{{ hello(flash=true) }}", &HashMap::new(), &Tera::default(), &Config::default());
|
||||||
|
assert!(res.is_err());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_markdown_to_html_relative_link_exists() {
|
||||||
|
let mut permalinks = HashMap::new();
|
||||||
|
permalinks.insert("pages/about.md".to_string(), "https://vincent.is/about".to_string());
|
||||||
|
let res = markdown_to_html(
|
||||||
|
r#"[rel link](./pages/about.md), [abs link](https://vincent.is/about)"#,
|
||||||
|
&permalinks,
|
||||||
|
&GUTENBERG_TERA,
|
||||||
|
&Config::default()
|
||||||
|
).unwrap();
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
res.contains(r#"<p><a href="https://vincent.is/about">rel link</a>, <a href="https://vincent.is/about">abs link</a></p>"#)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_markdown_to_html_relative_link_inexistant() {
|
||||||
|
let res = markdown_to_html("[rel link](./pages/about.md)", &HashMap::new(), &Tera::default(), &Config::default());
|
||||||
|
assert!(res.is_err());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue