Handle orgifying text in regular link path and raw-link.

This commit is contained in:
Tom Alexander
2023-10-06 18:30:08 -04:00
parent 51748afd41
commit d126488891
10 changed files with 151 additions and 10 deletions

View File

@@ -648,3 +648,45 @@ pub enum LinkType<'s> {
CodeRef,
Fuzzy,
}
#[derive(Debug)]
enum ParserState {
Normal,
InWhitespace,
}
/// Org-mode treats multiple consecutive whitespace characters as a single space. This function performs that transformation.
///
/// Example: `orgify_text("foo \t\n bar") == "foo bar"`
pub(crate) fn orgify_text<'s>(raw_text: &'s str) -> String {
let mut ret = String::with_capacity(raw_text.len());
let mut state = ParserState::Normal;
for c in raw_text.chars() {
state = match (&state, c) {
(ParserState::Normal, _) if " \t\r\n".contains(c) => {
ret.push(' ');
ParserState::InWhitespace
}
(ParserState::InWhitespace, _) if " \t\r\n".contains(c) => ParserState::InWhitespace,
(ParserState::Normal, _) => {
ret.push(c);
ParserState::Normal
}
(ParserState::InWhitespace, _) => {
ret.push(c);
ParserState::Normal
}
};
}
ret
}
impl<'s> RegularLink<'s> {
pub fn get_raw_link(&self) -> String {
orgify_text(self.raw_link)
}
pub fn get_path(&self) -> String {
orgify_text(self.path)
}
}