diff --git a/Cargo.lock b/Cargo.lock index 92db0d1..330f690 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -514,6 +514,7 @@ dependencies = [ "rusqlite", "serde", "serde_json", + "unidecode", "uuid 1.3.0", ] @@ -2397,6 +2398,12 @@ version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b" +[[package]] +name = "unidecode" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "402bb19d8e03f1d1a7450e2bd613980869438e0666331be3e073089124aa1adc" + [[package]] name = "universal-hash" version = "0.5.0" diff --git a/Cargo.toml b/Cargo.toml index 87bc987..2ce9183 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,3 +31,4 @@ rusqlite = {version = "0.28.0", features=["bundled", "chrono"]} lazy_static = "1.4.0" mongodb = "2.3.1" +unidecode = "0.3.0" diff --git a/src/commons.rs b/src/commons.rs index 6e86091..376bf6f 100644 --- a/src/commons.rs +++ b/src/commons.rs @@ -1,6 +1,26 @@ use clap::Parser; use r2d2::Pool; use r2d2_sqlite::SqliteConnectionManager; +use regex::Regex; +use unidecode::unidecode; + +pub trait NormalizeName { + fn normalize_name(&self) -> String; +} + +impl NormalizeName for String { + + fn normalize_name(&self) -> String { + let space_re = Regex::new(r"[\s_]+").unwrap(); + let remove_re = Regex::new(r"[^a-z0-9_]").unwrap(); + + let mut str = unidecode(self); + str = space_re.replace_all(&str, "_").to_string(); + + remove_re.replace_all(&str, "").to_string().to_lowercase() + } + +} #[derive(Parser)] pub struct Arguments { @@ -19,12 +39,10 @@ pub struct Arguments { } impl Arguments { - pub fn parse_args() -> Arguments { let args = Arguments::parse(); args } - } #[derive(Clone)] diff --git a/static/modules/md.js b/static/modules/md.js index ea27dec..f8fcae6 100644 --- a/static/modules/md.js +++ b/static/modules/md.js @@ -1,4 +1,5 @@ import { saveSelection, loadSelection } from "./caret.js"; +import { normalizePagename } from "./tools.js"; function setEditable() { let editModeButton = document.getElementById('editModeButton'); diff --git a/static/modules/tools.js b/static/modules/tools.js new file mode 100644 index 0000000..4df069e --- /dev/null +++ b/static/modules/tools.js @@ -0,0 +1,6 @@ + +function normalizeName(pagename) { + return str.normalize("NFKD").replace(/\p{Diacritic}/gu, "").toLowerCase().replace(/œ/g, "oe").replace(/æ/g, "ae").replace(/\s+/g, "_").replace(/[^a-z0-9_]/g, "").replace(/_+/g, "_"); +} + +export { normalizeName }; \ No newline at end of file