Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cards description #94

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions example/content/markdown-format.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
date: 2024-10-17 12:00:01
slug: markdown-format
title: Markdown Formatting Options
description: The content here accepts any valid CommonMark or Github Flavoured markdown and some GFM extensions.
tags: docs, markdown, Common Mark, GFM
extra:
math: true
Expand Down
8 changes: 7 additions & 1 deletion example/templates/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@
{%- for content in content_list %}
<article class="content-list-item">
<h2 class="content-title"><a href="./{{content.slug}}.html">{{ content.title | capitalize }}</a></h2>
<p class="content-excerpt">{{ content.html | striptags | trim_start_matches(pat=content.title) | truncate(length=100, end="...") }}</p>
<p class="content-excerpt">
{% if content.description %}
{{ content.description | replace(from='"', to="") | truncate(length=100, end="...") }}
{% else %}
{{ content.html | striptags | trim_start_matches(pat=content.title) | truncate(length=100, end="...") }}
{%- endif %}
</p>
{% if content.date -%}
<footer class="data-tags-footer">
<span class="content-date">{{ content.date | date(format="%b %e, %Y") }}</span>
Expand Down
8 changes: 8 additions & 0 deletions src/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use unicode_normalization::UnicodeNormalization;
#[derive(Debug, Deserialize, Clone, Serialize)]
pub struct Content {
pub title: String,
pub description: Option<String>,
pub slug: String,
pub html: String,
pub tags: Vec<String>,
Expand All @@ -32,6 +33,13 @@ pub fn get_title<'a>(frontmatter: &'a Frontmatter, html: &'a str) -> String {
}
}

pub fn get_description<'a>(frontmatter: &'a Frontmatter) -> Option<String> {
if let Some(description) = frontmatter.get("description") {
return Some(description.to_string());
}
None
}

pub fn get_slug<'a>(frontmatter: &'a Frontmatter, path: &'a Path) -> String {
if let Some(slug) = frontmatter.get("slug") {
return slugify(&slug.to_string());
Expand Down
4 changes: 3 additions & 1 deletion src/markdown.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::content::{get_date, get_slug, get_tags, get_title, Content};
use crate::content::{get_date, get_description, get_slug, get_tags, get_title, Content};
use crate::site::Data;
use comrak::{markdown_to_html, ComrakOptions};
use frontmatter_gen::{extract, Frontmatter};
Expand All @@ -21,12 +21,14 @@ pub fn get_content(path: &Path) -> Result<Content, String> {
let (frontmatter, markdown) = parse_front_matter(&file_content)?;
let html = get_html(markdown);
let title = get_title(&frontmatter, markdown);
let description = get_description(&frontmatter);
let tags = get_tags(&frontmatter);
let slug = get_slug(&frontmatter, path);
let date = get_date(&frontmatter, path);
let extra = frontmatter.get("extra").map(std::borrow::ToOwned::to_owned);
let content = Content {
title,
description,
slug,
html,
tags,
Expand Down
1 change: 1 addition & 0 deletions src/site.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,7 @@ fn handle_404(
let mut content = Content {
html: String::from("Page not found :/"),
title: String::from("Page not found"),
description: None,
date: None,
slug: "404".to_string(),
extra: None,
Expand Down