mirror of
https://github.com/Lumither/nocturne.git
synced 2026-10-17 18:36:21 +00:00
feat(idx) add auto idx
This commit is contained in:
8 files changed
+115
-17
No files matched your search
@@ -9,6 +9,9 @@ pub mod post;
|
||||
pub struct Post {
|
||||
pub post_id: Uuid,
|
||||
pub title: String,
|
||||
pub summary: String,
|
||||
pub content: String,
|
||||
pub last_update: NaiveDateTime,
|
||||
}
|
||||
|
||||
|
||||
@@ -1,16 +1,55 @@
|
||||
use chrono::Utc;
|
||||
use std::{env, fs};
|
||||
use std::error::Error;
|
||||
use std::process::Command;
|
||||
use std::str;
|
||||
|
||||
use chrono::{NaiveDateTime, Utc};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::markdown::meta::parse_meta;
|
||||
use crate::model::Post;
|
||||
|
||||
impl super::Post {
|
||||
pub fn from_path(path: String) -> Self {
|
||||
// todo: remove test
|
||||
Post {
|
||||
impl Post {
|
||||
pub fn from_path(path: String) -> Result<Self, Box<dyn Error>> {
|
||||
let md_content = fs::read_to_string(&path)?;
|
||||
let meta = parse_meta(&md_content);
|
||||
|
||||
let title = match meta.get("title") {
|
||||
None => "Title Not Found".to_string(),
|
||||
Some(title) => title.to_owned(),
|
||||
};
|
||||
let summary = match meta.get("summary") {
|
||||
None => "".to_string(),
|
||||
Some(summary) => summary.to_owned(),
|
||||
};
|
||||
|
||||
let git_output = Command::new("git")
|
||||
.arg("--no-pager")
|
||||
.arg("log")
|
||||
.arg("-1")
|
||||
.arg("--format=%cI")
|
||||
.arg("--")
|
||||
.current_dir(env::var("GIT_WORK_DIR")?)
|
||||
.arg(&path)
|
||||
.output()
|
||||
.expect("Failed to execute git command");
|
||||
|
||||
let last_update = if git_output.status.success() {
|
||||
NaiveDateTime::parse_from_str(
|
||||
str::from_utf8(&git_output.stdout).unwrap().trim_end(),
|
||||
"%Y-%m-%dT%H:%M:%S%z",
|
||||
)
|
||||
.unwrap_or_else(|_| Utc::now().naive_utc())
|
||||
} else {
|
||||
Utc::now().naive_utc()
|
||||
};
|
||||
|
||||
Ok(Post {
|
||||
post_id: Uuid::new_v4(),
|
||||
title: "test title".to_string(),
|
||||
content: "test content".to_string(),
|
||||
last_update: Utc::now().naive_utc(),
|
||||
}
|
||||
title,
|
||||
summary,
|
||||
content: md_content,
|
||||
last_update,
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user