mirror of
https://github.com/Lumither/nocturne.git
synced 2026-10-17 10:26:21 +00:00
feat(idx) add auto idx
This commit is contained in:
8 files changed
+115
-17
No files matched your search
@@ -14,3 +14,4 @@ tokio = { version = "1.37.0", features = ["full"] }
|
|||||||
uuid = { version = "1.8.0", features = ["v4", "fast-rng", "serde"] }
|
uuid = { version = "1.8.0", features = ["v4", "fast-rng", "serde"] }
|
||||||
sqlx = { version = "0.7.4", features = ["runtime-tokio", "tls-native-tls", "postgres", "chrono", "uuid"] }
|
sqlx = { version = "0.7.4", features = ["runtime-tokio", "tls-native-tls", "postgres", "chrono", "uuid"] }
|
||||||
chrono = { version = "0.4.38", features = ["serde"] }
|
chrono = { version = "0.4.38", features = ["serde"] }
|
||||||
|
regex = "1.10.4"
|
||||||
+4
-2
@@ -2,13 +2,15 @@ CREATE TABLE IF NOT EXISTS Post
|
|||||||
(
|
(
|
||||||
post_id UUID PRIMARY KEY,
|
post_id UUID PRIMARY KEY,
|
||||||
title VARCHAR(255) NOT NULL,
|
title VARCHAR(255) NOT NULL,
|
||||||
|
summary TEXT NOT NULL,
|
||||||
content TEXT NOT NULL,
|
content TEXT NOT NULL,
|
||||||
last_update TIMESTAMP
|
last_update TIMESTAMP
|
||||||
);
|
);
|
||||||
|
|
||||||
INSERT INTO Post (post_id, title, content, last_update)
|
INSERT INTO Post (post_id, title, summary, content, last_update)
|
||||||
VALUES (?1, ?2, ?3, ?4)
|
VALUES ($1, $2, $3, $4, $5)
|
||||||
ON CONFLICT (post_id)
|
ON CONFLICT (post_id)
|
||||||
DO UPDATE SET title = excluded.title,
|
DO UPDATE SET title = excluded.title,
|
||||||
|
summary = excluded.summary,
|
||||||
content = excluded.content,
|
content = excluded.content,
|
||||||
last_update = excluded.last_update
|
last_update = excluded.last_update
|
||||||
@@ -29,30 +29,30 @@ pub async fn refresh(
|
|||||||
|
|
||||||
let post_list: Vec<Post> = md_list
|
let post_list: Vec<Post> = md_list
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(Post::from_path)
|
.map(|post| Post::from_path(post).unwrap())
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
for post in post_list {
|
for post in post_list {
|
||||||
match query(
|
match query(
|
||||||
r##"
|
r##"
|
||||||
INSERT INTO Post (post_id, title, content, last_update)
|
INSERT INTO Post (post_id, title, summary, content, last_update)
|
||||||
VALUES ($1, $2, $3, $4)
|
VALUES ($1, $2, $3, $4, $5)
|
||||||
ON CONFLICT (post_id)
|
ON CONFLICT (post_id)
|
||||||
DO UPDATE SET title = excluded.title,
|
DO UPDATE SET title = excluded.title,
|
||||||
|
summary = excluded.summary,
|
||||||
content = excluded.content,
|
content = excluded.content,
|
||||||
last_update = excluded.last_update
|
last_update = excluded.last_update
|
||||||
"##,
|
"##,
|
||||||
)
|
)
|
||||||
.bind(post.post_id)
|
.bind(post.post_id)
|
||||||
.bind(post.title)
|
.bind(post.title)
|
||||||
|
.bind(post.summary)
|
||||||
.bind(post.content)
|
.bind(post.content)
|
||||||
.bind(post.last_update)
|
.bind(post.last_update)
|
||||||
.execute(&db_connection)
|
.execute(&db_connection)
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
Ok(_) => {
|
Ok(_) => {}
|
||||||
dbg!("inserted");
|
|
||||||
}
|
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
return Err((
|
return Err((
|
||||||
StatusCode::INTERNAL_SERVER_ERROR,
|
StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ use crate::api::post::refresh::refresh;
|
|||||||
|
|
||||||
mod api;
|
mod api;
|
||||||
mod model;
|
mod model;
|
||||||
|
mod markdown;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<(), Box<dyn Error>> {
|
async fn main() -> Result<(), Box<dyn Error>> {
|
||||||
|
|||||||
@@ -0,0 +1,51 @@
|
|||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
use regex::Regex;
|
||||||
|
|
||||||
|
pub fn parse_meta(md_content: &str) -> HashMap<String, String> {
|
||||||
|
let mut fields: HashMap<String, String> = HashMap::new();
|
||||||
|
let match_regex = Regex::new(r"(?s)---\n(.*\n)---\n").unwrap();
|
||||||
|
if let Some(front_matter) = match_regex.captures(md_content) {
|
||||||
|
let fm_content = front_matter.get(1).unwrap().as_str();
|
||||||
|
for line in fm_content.lines() {
|
||||||
|
if let Some((k, v)) = extract_kv(line) {
|
||||||
|
fields.insert(k, v);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fields
|
||||||
|
}
|
||||||
|
|
||||||
|
fn extract_kv(line: &str) -> Option<(String, String)> {
|
||||||
|
let split: Vec<&str> = line.splitn(2, ':').map(|s| s.trim()).collect();
|
||||||
|
if split.len() == 2 {
|
||||||
|
let key = split[0].to_string();
|
||||||
|
let value = split[1].to_string();
|
||||||
|
Some((key, value))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod meta_test {
|
||||||
|
use std::error::Error;
|
||||||
|
|
||||||
|
use crate::markdown::meta::parse_meta;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_parse_meta_front_matter() -> Result<(), Box<dyn Error>> {
|
||||||
|
let parsed_meta = parse_meta(
|
||||||
|
r##"
|
||||||
|
---
|
||||||
|
title: Hello World!
|
||||||
|
tags: [ "test", "hello_world" ]
|
||||||
|
---
|
||||||
|
"##
|
||||||
|
);
|
||||||
|
dbg!(&parsed_meta);
|
||||||
|
assert_eq!(parsed_meta.get("title"), Some(&"Hello World!".to_string()));
|
||||||
|
assert_eq!(parsed_meta.get("tags"), Some(&r#"[ "test", "hello_world" ]"#.to_string()));
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
pub mod meta;
|
||||||
@@ -9,6 +9,9 @@ pub mod post;
|
|||||||
pub struct Post {
|
pub struct Post {
|
||||||
pub post_id: Uuid,
|
pub post_id: Uuid,
|
||||||
pub title: String,
|
pub title: String,
|
||||||
|
pub summary: String,
|
||||||
pub content: String,
|
pub content: String,
|
||||||
pub last_update: NaiveDateTime,
|
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 uuid::Uuid;
|
||||||
|
|
||||||
|
use crate::markdown::meta::parse_meta;
|
||||||
use crate::model::Post;
|
use crate::model::Post;
|
||||||
|
|
||||||
impl super::Post {
|
impl Post {
|
||||||
pub fn from_path(path: String) -> Self {
|
pub fn from_path(path: String) -> Result<Self, Box<dyn Error>> {
|
||||||
// todo: remove test
|
let md_content = fs::read_to_string(&path)?;
|
||||||
Post {
|
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(),
|
post_id: Uuid::new_v4(),
|
||||||
title: "test title".to_string(),
|
title,
|
||||||
content: "test content".to_string(),
|
summary,
|
||||||
last_update: Utc::now().naive_utc(),
|
content: md_content,
|
||||||
}
|
last_update,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user