Skip to content

Commit

Permalink
Changed to GitHab complient fragment generator
Browse files Browse the repository at this point in the history
  • Loading branch information
HU90m committed Jul 27, 2023
1 parent c942139 commit e50d692
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 21 deletions.
16 changes: 0 additions & 16 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion lychee-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ version = "0.13.0"
async-stream = "0.3.5"
cached = "0.44.0"
check-if-email-exists = { version = "0.9.0", optional = true }
convert_case = "0.6.0"
email_address = "0.2.4"
futures = "0.3.27"
glob = "0.3.1"
Expand Down
45 changes: 41 additions & 4 deletions lychee-lib/src/extract/markdown.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! Extract things from markdown documents
use std::collections::{HashMap, HashSet};

use convert_case::{Case, Casing};
use pulldown_cmark::{Event, Options, Parser, Tag};

use crate::{extract::plaintext::extract_plaintext, types::uri::raw::RawUri};
Expand Down Expand Up @@ -100,7 +99,7 @@ pub(crate) fn extract_markdown_fragments(input: &str) -> HashSet<String> {
}

if !heading.is_empty() {
let id = id_generator.generate(&mut heading);
let id = id_generator.generate(&heading);
out.insert(id);
heading.clear();
}
Expand All @@ -126,8 +125,8 @@ struct HeadingIdGenerator {
}

impl HeadingIdGenerator {
fn generate(&mut self, heading: &mut String) -> String {
let mut id = heading.to_case(Case::Kebab);
fn generate(&mut self, heading: &str) -> String {
let mut id = Self::into_kebab_case(heading);
let count = self.counter.entry(id.clone()).or_insert(0);
if *count != 0 {
id = format!("{}-{}", id, *count);
Expand All @@ -136,6 +135,22 @@ impl HeadingIdGenerator {

id
}

/// Converts text into kebab case
#[must_use]
fn into_kebab_case(text: &str) -> String {
text.chars()
.filter_map(|ch| {
if ch.is_alphanumeric() || ch == '_' || ch == '-' {
Some(ch.to_ascii_lowercase())
} else if ch.is_whitespace() {
Some('-')
} else {
None
}
})
.collect::<String>()
}
}

#[cfg(test)]
Expand Down Expand Up @@ -222,4 +237,26 @@ Some pre-formatted http://pre.com
let uris = extract_markdown(input, false);
assert_eq!(uris, expected);
}

#[test]
fn test_kebab_case() {
let check = |input, expected| {
let actual = HeadingIdGenerator::into_kebab_case(input);
assert_eq!(actual, expected);
};
check("A Heading", "a-heading");
check(
"This header has a :thumbsup: in it",
"this-header-has-a-thumbsup-in-it",
);
check(
"Header with 한글 characters (using unicode)",
"header-with-한글-characters-using-unicode",
);
check(
"Underscores foo_bar_, dots . and numbers 1.7e-3",
"underscores-foo_bar_-dots--and-numbers-17e-3",
);
check("Many spaces", "many----------spaces");
}
}

0 comments on commit e50d692

Please sign in to comment.