Skip to content

Commit 4593064

Browse files
authored
Repeated title annoyance (#94)
1 parent 561fd60 commit 4593064

File tree

5 files changed

+20
-2
lines changed

5 files changed

+20
-2
lines changed

example/content/markdown-format.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
date: 2024-10-17 12:00:01
33
slug: markdown-format
44
title: Markdown Formatting Options
5+
description: The content here accepts any valid CommonMark or Github Flavoured markdown and some GFM extensions.
56
tags: docs, markdown, Common Mark, GFM
67
extra:
78
math: true

example/templates/list.html

+7-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@
1616
{%- for content in content_list %}
1717
<article class="content-list-item">
1818
<h2 class="content-title"><a href="./{{content.slug}}.html">{{ content.title | capitalize }}</a></h2>
19-
<p class="content-excerpt">{{ content.html | striptags | trim_start_matches(pat=content.title) | truncate(length=100, end="...") }}</p>
19+
<p class="content-excerpt">
20+
{% if content.description %}
21+
{{ content.description | replace(from='"', to="") | truncate(length=100, end="...") }}
22+
{% else %}
23+
{{ content.html | striptags | trim_start_matches(pat=content.title) | truncate(length=100, end="...") }}
24+
{%- endif %}
25+
</p>
2026
{% if content.date -%}
2127
<footer class="data-tags-footer">
2228
<span class="content-date">{{ content.date | date(format="%b %e, %Y") }}</span>

src/content.rs

+8
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use unicode_normalization::UnicodeNormalization;
1212
#[derive(Debug, Deserialize, Clone, Serialize)]
1313
pub struct Content {
1414
pub title: String,
15+
pub description: Option<String>,
1516
pub slug: String,
1617
pub html: String,
1718
pub tags: Vec<String>,
@@ -32,6 +33,13 @@ pub fn get_title<'a>(frontmatter: &'a Frontmatter, html: &'a str) -> String {
3233
}
3334
}
3435

36+
pub fn get_description<'a>(frontmatter: &'a Frontmatter) -> Option<String> {
37+
if let Some(description) = frontmatter.get("description") {
38+
return Some(description.to_string());
39+
}
40+
None
41+
}
42+
3543
pub fn get_slug<'a>(frontmatter: &'a Frontmatter, path: &'a Path) -> String {
3644
if let Some(slug) = frontmatter.get("slug") {
3745
return slugify(&slug.to_string());

src/markdown.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::content::{get_date, get_slug, get_tags, get_title, Content};
1+
use crate::content::{get_date, get_description, get_slug, get_tags, get_title, Content};
22
use crate::site::Data;
33
use comrak::{markdown_to_html, ComrakOptions};
44
use frontmatter_gen::{extract, Frontmatter};
@@ -21,12 +21,14 @@ pub fn get_content(path: &Path) -> Result<Content, String> {
2121
let (frontmatter, markdown) = parse_front_matter(&file_content)?;
2222
let html = get_html(markdown);
2323
let title = get_title(&frontmatter, markdown);
24+
let description = get_description(&frontmatter);
2425
let tags = get_tags(&frontmatter);
2526
let slug = get_slug(&frontmatter, path);
2627
let date = get_date(&frontmatter, path);
2728
let extra = frontmatter.get("extra").map(std::borrow::ToOwned::to_owned);
2829
let content = Content {
2930
title,
31+
description,
3032
slug,
3133
html,
3234
tags,

src/site.rs

+1
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,7 @@ fn handle_404(
466466
let mut content = Content {
467467
html: String::from("Page not found :/"),
468468
title: String::from("Page not found"),
469+
description: None,
469470
date: None,
470471
slug: "404".to_string(),
471472
extra: None,

0 commit comments

Comments
 (0)