Skip to content
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
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "corkboard"
version = "0.4.8"
version = "0.5.0"
edition = "2024"
default-run = "corkboard"

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -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) |
Expand Down
1 change: 1 addition & 0 deletions config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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=""
Expand Down
1 change: 1 addition & 0 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 14 additions & 1 deletion docker/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -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)
Expand Down
14 changes: 11 additions & 3 deletions src/data/repository/markdown_article_repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<DS: MarkdownDataSource> {
data_source: DS,
truncate_lines: usize,
preview_include_images: bool,
}

impl<DS: MarkdownDataSource> MarkdownArticleRepository<DS> {
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,
}
}
}
Expand Down Expand Up @@ -52,7 +56,11 @@ impl<DS: MarkdownDataSource> ArticleRepository for MarkdownArticleRepository<DS>
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 {
Expand Down
3 changes: 3 additions & 0 deletions src/infrastructure/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<url::Url>,
Expand Down Expand Up @@ -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))
Expand Down
63 changes: 47 additions & 16 deletions src/infrastructure/markdown/markdown_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<TocEntry>) {
render_markdown_with_options(markdown_content, syntax_set, true)
}

pub fn render_markdown_preview(
markdown_content: &str,
syntax_set: &SyntaxSet,
) -> (String, Vec<TocEntry>) {
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<TocEntry>) {
let mut options = Options::empty();
options.insert(Options::ENABLE_TABLES);
options.insert(Options::ENABLE_FOOTNOTES);
Expand All @@ -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);
}
}
_ => {}
}
Expand All @@ -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;
Expand Down
7 changes: 6 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

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<Box<dyn ArticleRepository + Send + Sync>> = vec![Box::new(repo)];
Expand All @@ -83,6 +87,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
repos.push(Box::new(MarkdownArticleRepository::new(
git_source,
settings.truncate_lines,
settings.preview_include_images,
)));
}
Err(e) => {
Expand Down
Loading