diff --git a/Cargo.lock b/Cargo.lock index 3df7c8b..630f9c4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -466,7 +466,7 @@ checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" [[package]] name = "corkboard" -version = "0.4.8" +version = "0.5.0" dependencies = [ "askama", "axum", diff --git a/Cargo.toml b/Cargo.toml index 57988f9..6855640 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "corkboard" -version = "0.4.8" +version = "0.5.0" edition = "2024" default-run = "corkboard" diff --git a/README.md b/README.md index e4f6acd..1576f34 100644 --- a/README.md +++ b/README.md @@ -169,6 +169,7 @@ base_url = "https://your-domain-here.com" articles_dir = "articles" port = 3000 truncate_lines = 15 +preview_include_images = false thumbnail_show_articles = false # social_links = [ @@ -209,6 +210,7 @@ Every setting can be configured via a `config.toml` file or overridden using an | `articles_dir` | `CORKBOARD_ARTICLES_DIR` | `articles` | Directory to scan for `.md` files | | `port` | `CORKBOARD_PORT` | `3000` | Internal HTTP port the application listens on | | `truncate_lines` | `CORKBOARD_TRUNCATE_LINES` | `15` | Markdown lines shown per card before truncation | +| `preview_include_images` | `CORKBOARD_PREVIEW_INCLUDE_IMAGES` | `false` | Whether to include images in truncated article previews | | `thumbnail_show_articles` | `CORKBOARD_THUMBNAIL_SHOW_ARTICLES` | `false` | Whether to show article snippets in the blog's generated thumbnail | | `social_links` | `CORKBOARD_SOCIAL_LINKS` | *(empty list)* | List of social/external URLs to show in the footer (comma-separated in env) | | `cors_allowed_origins` | `CORKBOARD_CORS_ALLOWED_ORIGINS` | `[base_url]` | List of allowed CORS origins. Defaults to the configured `base_url`. (comma-separated list in env) | diff --git a/config.toml b/config.toml index 1e9649b..baaa4bc 100644 --- a/config.toml +++ b/config.toml @@ -7,6 +7,7 @@ blog_license_url = "https://creativecommons.org/licenses/by-sa/4.0/" articles_dir = "articles" port = 3000 truncate_lines = 15 +preview_include_images = false thumbnail_show_articles = false # social_links = [ # "https://github.com/your-username", diff --git a/docker/Dockerfile b/docker/Dockerfile index 0c35ce5..773509a 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -51,6 +51,7 @@ ENV CORKBOARD_PORT=3000 ENV CORKBOARD_BASE_URL="http://localhost:3000" ENV CORKBOARD_ARTICLES_DIR=/app/articles ENV CORKBOARD_TRUNCATE_LINES=15 +ENV CORKBOARD_PREVIEW_INCLUDE_IMAGES=false ENV CORKBOARD_THUMBNAIL_SHOW_ARTICLES=false ENV CORKBOARD_SOCIAL_LINKS="" ENV CORKBOARD_CORS_ALLOWED_ORIGINS="" diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml index 55359fc..e70d3c4 100644 --- a/docker/docker-compose.yml +++ b/docker/docker-compose.yml @@ -27,6 +27,7 @@ services: - CORKBOARD_BLOG_AUTHOR=Author - CORKBOARD_ARTICLES_DIR=/app/articles - CORKBOARD_TRUNCATE_LINES=15 + # - CORKBOARD_PREVIEW_INCLUDE_IMAGES=false # - CORKBOARD_THUMBNAIL_SHOW_ARTICLES=false # - CORKBOARD_SOCIAL_LINKS=https://github.com/your-username,https://linkedin.com/in/your-username,https://twitter.com/your-username # - CORKBOARD_CORS_ALLOWED_ORIGINS=http://your-domain-here.com:8080 diff --git a/docker/entrypoint.sh b/docker/entrypoint.sh index ed35c5a..6e5eb4c 100644 --- a/docker/entrypoint.sh +++ b/docker/entrypoint.sh @@ -28,6 +28,7 @@ base_url = "http://localhost:3000" articles_dir = "articles" port = 3000 truncate_lines = 15 +preview_include_images = false thumbnail_show_articles = false # social_links = [ @@ -50,7 +51,19 @@ mkdir -p "$ARTICLES_DIR" mkdir -p /app/assets -if [ -z "$(ls -A "$ARTICLES_DIR" 2>/dev/null)" ]; then +GIT_CONFIGURED=false +if [ -n "$CORKBOARD_GIT__LINK" ]; then + GIT_CONFIGURED=true +elif grep -E "^\[git\]" /app/config.toml >/dev/null 2>&1; then + GIT_CONFIGURED=true +fi + +if [ "$GIT_CONFIGURED" = "true" ]; then + if [ "$(ls -A "$ARTICLES_DIR" 2>/dev/null)" = "welcome.md" ]; then + echo "Git repository configured: removing auto-generated welcome post..." + rm -f "$ARTICLES_DIR/welcome.md" + fi +elif [ -z "$(ls -A "$ARTICLES_DIR" 2>/dev/null)" ]; then echo "No markdown articles found in $ARTICLES_DIR. Creating a default welcome post..." cp /app/welcome.md "$ARTICLES_DIR/welcome.md" CURRENT_DATE=$(date +%Y-%m-%d) diff --git a/src/data/repository/markdown_article_repository.rs b/src/data/repository/markdown_article_repository.rs index 9666f6d..36fd3f4 100644 --- a/src/data/repository/markdown_article_repository.rs +++ b/src/data/repository/markdown_article_repository.rs @@ -5,19 +5,23 @@ use crate::domain::model::article::Article; use crate::domain::model::error::AppError; use crate::domain::repository::article_repository::ArticleRepository; use crate::infrastructure::markdown::helpers::close_open_fences; -use crate::infrastructure::markdown::markdown_renderer::render_markdown; +use crate::infrastructure::markdown::markdown_renderer::{ + render_markdown, render_markdown_preview, +}; use syntect::parsing::SyntaxSet; pub struct MarkdownArticleRepository { data_source: DS, truncate_lines: usize, + preview_include_images: bool, } impl MarkdownArticleRepository { - pub fn new(data_source: DS, truncate_lines: usize) -> Self { + pub fn new(data_source: DS, truncate_lines: usize, preview_include_images: bool) -> Self { Self { data_source, truncate_lines, + preview_include_images, } } } @@ -52,7 +56,11 @@ impl ArticleRepository for MarkdownArticleRepository let (preview, has_more_content) = if lines.len() > self.truncate_lines { let truncated_md = lines[..self.truncate_lines].join("\n"); let safe_md = close_open_fences(&truncated_md); - let (raw_preview, _) = render_markdown(&safe_md, &ps); + let (raw_preview, _) = if self.preview_include_images { + render_markdown(&safe_md, &ps) + } else { + render_markdown_preview(&safe_md, &ps) + }; let p = SanitizeMarkdownHtmlUsecase::execute(raw_preview); (p, true) } else { diff --git a/src/infrastructure/config.rs b/src/infrastructure/config.rs index a5edf2e..11034ce 100644 --- a/src/infrastructure/config.rs +++ b/src/infrastructure/config.rs @@ -10,6 +10,7 @@ pub const DEFAULT_BASE_URL: &str = "http://localhost:3000"; pub const DEFAULT_BLOG_LICENSE: &str = "CC 4.0 BY-SA"; pub const DEFAULT_BLOG_LICENSE_URL: &str = "https://creativecommons.org/licenses/by-sa/4.0/"; pub const DEFAULT_TRUNCATE_LINES: i64 = 15; +pub const DEFAULT_PREVIEW_INCLUDE_IMAGES: bool = false; pub const DEFAULT_LANG: &str = "en"; pub const DEFAULT_THUMBNAIL_SHOW_ARTICLES: bool = false; @@ -29,6 +30,7 @@ pub struct Settings { pub articles_dir: String, pub port: u16, pub truncate_lines: usize, + pub preview_include_images: bool, pub lang: String, #[serde(default)] pub social_links: Vec, @@ -128,6 +130,7 @@ impl Settings { .set_default("blog_license", DEFAULT_BLOG_LICENSE)? .set_default("blog_license_url", DEFAULT_BLOG_LICENSE_URL)? .set_default("truncate_lines", DEFAULT_TRUNCATE_LINES)? + .set_default("preview_include_images", DEFAULT_PREVIEW_INCLUDE_IMAGES)? .set_default("lang", DEFAULT_LANG)? .set_default("thumbnail_show_articles", DEFAULT_THUMBNAIL_SHOW_ARTICLES)? .add_source(File::with_name("config").required(false)) diff --git a/src/infrastructure/markdown/markdown_renderer.rs b/src/infrastructure/markdown/markdown_renderer.rs index 0263dff..5ea802c 100644 --- a/src/infrastructure/markdown/markdown_renderer.rs +++ b/src/infrastructure/markdown/markdown_renderer.rs @@ -8,6 +8,21 @@ use syntect::html::{ClassStyle, ClassedHTMLGenerator}; use syntect::parsing::SyntaxSet; pub fn render_markdown(markdown_content: &str, syntax_set: &SyntaxSet) -> (String, Vec) { + render_markdown_with_options(markdown_content, syntax_set, true) +} + +pub fn render_markdown_preview( + markdown_content: &str, + syntax_set: &SyntaxSet, +) -> (String, Vec) { + render_markdown_with_options(markdown_content, syntax_set, false) +} + +fn render_markdown_with_options( + markdown_content: &str, + syntax_set: &SyntaxSet, + include_images: bool, +) -> (String, Vec) { let mut options = Options::empty(); options.insert(Options::ENABLE_TABLES); options.insert(Options::ENABLE_FOOTNOTES); @@ -34,23 +49,27 @@ pub fn render_markdown(markdown_content: &str, syntax_set: &SyntaxSet) -> (Strin match event { Event::End(TagEnd::Image) => { in_image = false; - let display_caption = if !image_title.is_empty() { - &image_title - } else { - "" - }; + if include_images { + let display_caption = if !image_title.is_empty() { + &image_title + } else { + "" + }; - let template = MarkdownImageTemplate { - src: &image_dest, - alt: &image_alt, - caption: display_caption, - }; + let template = MarkdownImageTemplate { + src: &image_dest, + alt: &image_alt, + caption: display_caption, + }; - let html = template.render().unwrap(); - new_events.push(Event::Html(html.into())); + let html = template.render().unwrap(); + new_events.push(Event::Html(html.into())); + } } Event::Text(text) => { - image_alt.push_str(&text); + if include_images { + image_alt.push_str(&text); + } } _ => {} } @@ -62,9 +81,21 @@ pub fn render_markdown(markdown_content: &str, syntax_set: &SyntaxSet) -> (Strin dest_url, title, .. }) => { in_image = true; - image_dest = dest_url.to_string(); - image_title = title.to_string(); - image_alt.clear(); + if include_images { + image_dest = dest_url.to_string(); + image_title = title.to_string(); + image_alt.clear(); + } + } + Event::Start(Tag::Paragraph) => { + new_events.push(Event::Start(Tag::Paragraph)); + } + Event::End(TagEnd::Paragraph) => { + if let Some(Event::Start(Tag::Paragraph)) = new_events.last() { + new_events.pop(); + } else { + new_events.push(Event::End(TagEnd::Paragraph)); + } } Event::Start(Tag::CodeBlock(kind)) => { in_code_block = true; diff --git a/src/main.rs b/src/main.rs index 67960be..b5290b6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -63,7 +63,11 @@ async fn main() -> Result<(), Box> { tracing::info!("Parsing articles from '{}'...", settings.articles_dir); let data_source = LocalStorageMarkdownDataSource::new(settings.articles_dir.clone()); - let repo = MarkdownArticleRepository::new(data_source, settings.truncate_lines); + let repo = MarkdownArticleRepository::new( + data_source, + settings.truncate_lines, + settings.preview_include_images, + ); #[allow(unused_mut)] let mut repos: Vec> = vec![Box::new(repo)]; @@ -83,6 +87,7 @@ async fn main() -> Result<(), Box> { repos.push(Box::new(MarkdownArticleRepository::new( git_source, settings.truncate_lines, + settings.preview_include_images, ))); } Err(e) => {