mirror of
https://github.com/getzola/zola
synced 2024-12-13 13:52:28 +00:00
Add emphasis, strong and code support in header
This commit is contained in:
parent
774514f4d4
commit
972aab1ac4
2 changed files with 54 additions and 13 deletions
|
@ -82,23 +82,37 @@ fn fix_link(link: &str, context: &RenderContext) -> Result<String> {
|
|||
Ok(result)
|
||||
}
|
||||
|
||||
/// returns true if event have been processed
|
||||
fn push_to_temp_header(event: &Event, temp_header: &mut TempHeader) -> bool {
|
||||
match event {
|
||||
Event::End(Tag::Link(_, _)) => {
|
||||
temp_header.add_html("</a>");
|
||||
}
|
||||
Event::Start(Tag::Code) => {
|
||||
temp_header.add_html("<code>");
|
||||
}
|
||||
Event::End(Tag::Code) => {
|
||||
temp_header.add_html("</code>");
|
||||
}
|
||||
fn start_tag(temp_header: &mut TempHeader, tag: &Tag) -> bool {
|
||||
match tag {
|
||||
Tag::Emphasis => temp_header.add_html("<em>"),
|
||||
Tag::Strong => temp_header.add_html("<strong>"),
|
||||
Tag::Code => temp_header.add_html("<code>"),
|
||||
// Tag::Link is handled elsewhere
|
||||
_ => return false,
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
fn end_tag(temp_header: &mut TempHeader, tag: &Tag) -> bool {
|
||||
match tag {
|
||||
Tag::Emphasis => temp_header.add_html("</em>"),
|
||||
Tag::Strong => temp_header.add_html("</strong>"),
|
||||
Tag::Code => temp_header.add_html("</code>"),
|
||||
Tag::Link(_, _) => temp_header.add_html("</a>"),
|
||||
_ => return false,
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
/// returns true if event have been processed
|
||||
fn push_to_temp_header(event: &Event, temp_header: &mut TempHeader) -> bool {
|
||||
match event {
|
||||
Event::Start(tag) => start_tag(temp_header, tag),
|
||||
Event::End(tag) => end_tag(temp_header, tag),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn markdown_to_html(content: &str, context: &RenderContext) -> Result<Rendered> {
|
||||
// the rendered html
|
||||
let mut html = String::with_capacity(content.len());
|
||||
|
@ -126,7 +140,7 @@ pub fn markdown_to_html(content: &str, context: &RenderContext) -> Result<Render
|
|||
|
||||
{
|
||||
let parser = Parser::new_ext(content, opts).map(|event| {
|
||||
// Header first
|
||||
// Trivial markup generation
|
||||
if in_header && push_to_temp_header(&event, &mut temp_header) {
|
||||
return Event::Html(Borrowed(""));
|
||||
}
|
||||
|
|
|
@ -522,6 +522,33 @@ fn can_understand_link_with_title_in_header() {
|
|||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn can_understand_emphasis_in_header() {
|
||||
let permalinks_ctx = HashMap::new();
|
||||
let config = Config::default();
|
||||
let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::None);
|
||||
let res = render_content("# *Emphasis* text", &context).unwrap();
|
||||
assert_eq!(res.body, "<h1 id=\"emphasis-text\"><em>Emphasis</em> text</h1>\n")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn can_understand_strong_in_header() {
|
||||
let permalinks_ctx = HashMap::new();
|
||||
let config = Config::default();
|
||||
let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::None);
|
||||
let res = render_content("# **Strong** text", &context).unwrap();
|
||||
assert_eq!(res.body, "<h1 id=\"strong-text\"><strong>Strong</strong> text</h1>\n")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn can_understand_code_in_header() {
|
||||
let permalinks_ctx = HashMap::new();
|
||||
let config = Config::default();
|
||||
let context = RenderContext::new(&ZOLA_TERA, &config, "", &permalinks_ctx, InsertAnchor::None);
|
||||
let res = render_content("# `Code` text", &context).unwrap();
|
||||
assert_eq!(res.body, "<h1 id=\"code-text\"><code>Code</code> text</h1>\n")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn can_make_valid_relative_link_in_header() {
|
||||
let mut permalinks = HashMap::new();
|
||||
|
|
Loading…
Reference in a new issue