Skip to content

Commit

Permalink
Eliminate regex and once_cell dependencies.
Browse files Browse the repository at this point in the history
Replace usage with `unicode_categories` crate which comrak already
depends on.
  • Loading branch information
nicoburns committed Dec 18, 2024
1 parent dc07704 commit f1b2e1d
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 11 deletions.
2 changes: 0 additions & 2 deletions Cargo.lock

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

2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ doc = false

[dependencies]
typed-arena = "2.0.2"
regex = "1"
once_cell = "1.19.0"
entities = "1.0.1"
unicode_categories = "0.1.1"
memchr = "2"
Expand Down
19 changes: 14 additions & 5 deletions src/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ use crate::nodes::{
};
use crate::parser::{Options, Plugins};
use crate::scanners;
use once_cell::sync::Lazy;
use regex::Regex;
use std::borrow::Cow;
use std::cell::Cell;
use std::collections::{HashMap, HashSet};
use std::io::{self, Write};
use std::str;
use unicode_categories::UnicodeCategories;

use crate::adapters::HeadingMeta;

Expand Down Expand Up @@ -102,11 +101,21 @@ impl Anchorizer {
/// assert_eq!("ticks-arent-in".to_string(), anchorizer.anchorize(source.to_string()));
/// ```
pub fn anchorize(&mut self, header: String) -> String {
static REJECTED_CHARS: Lazy<Regex> =
Lazy::new(|| Regex::new(r"[^\p{L}\p{M}\p{N}\p{Pc} -]").unwrap());
fn is_permitted_char(&c: &char) -> bool {
c == ' '
|| c == '-'
|| c.is_letter()
|| c.is_mark()
|| c.is_number()
|| c.is_punctuation_connector()
}

let mut id = header.to_lowercase();
id = REJECTED_CHARS.replace_all(&id, "").replace(' ', "-");
id = id
.chars()
.filter(is_permitted_char)
.map(|c| if c == ' ' { '-' } else { c })
.collect();

let mut uniq = 0;
id = loop {
Expand Down
3 changes: 1 addition & 2 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -787,8 +787,7 @@ pub struct RenderOptions {
///
/// options.render.full_info_string = true;
/// let html = markdown_to_html("``` rust extra info\nfn hello();\n```\n", &options);
/// let re = regex::Regex::new(r#"data-meta="extra info""#).unwrap();
/// assert!(re.is_match(&html));
/// assert!(html.contains(r#"data-meta="extra info""#));
/// ```
#[builder(default)]
pub full_info_string: bool,
Expand Down

0 comments on commit f1b2e1d

Please sign in to comment.