Remove all whitespace from search option if it contains any line breaks and handle triple+ slashes for file links.
This commit is contained in:
@@ -738,6 +738,12 @@ enum PathState {
|
||||
HasLineBreak(String),
|
||||
}
|
||||
|
||||
enum SearchOptionState {
|
||||
Normal,
|
||||
HasWhitespace(usize),
|
||||
HasLineBreak(String),
|
||||
}
|
||||
|
||||
impl<'s> AngleLink<'s> {
|
||||
/// Remove line breaks but preserve multiple consecutive spaces.
|
||||
pub fn get_path(&self) -> Cow<'s, str> {
|
||||
@@ -761,4 +767,44 @@ impl<'s> AngleLink<'s> {
|
||||
PathState::HasLineBreak(ret) => Cow::Owned(ret),
|
||||
}
|
||||
}
|
||||
|
||||
/// Remove all whitespace but only if search_option contains a line break.
|
||||
pub fn get_search_option(&self) -> Option<Cow<'s, str>> {
|
||||
self.search_option.map(|search_option| {
|
||||
let mut state = SearchOptionState::Normal;
|
||||
for (offset, c) in search_option.char_indices() {
|
||||
match (&mut state, c) {
|
||||
(SearchOptionState::Normal, '\n') => {
|
||||
let mut ret = String::with_capacity(search_option.len());
|
||||
ret.push_str(&search_option[..offset]);
|
||||
state = SearchOptionState::HasLineBreak(ret);
|
||||
}
|
||||
(SearchOptionState::Normal, ' ' | '\t') => {
|
||||
state = SearchOptionState::HasWhitespace(offset);
|
||||
}
|
||||
(SearchOptionState::Normal, _) => {}
|
||||
(SearchOptionState::HasWhitespace(first_whitespace_offset), '\n') => {
|
||||
let mut ret = String::with_capacity(search_option.len());
|
||||
ret.push_str(&search_option[..*first_whitespace_offset]);
|
||||
for c in search_option[*first_whitespace_offset..offset].chars() {
|
||||
if !c.is_ascii_whitespace() {
|
||||
ret.push(c);
|
||||
}
|
||||
}
|
||||
state = SearchOptionState::HasLineBreak(ret);
|
||||
}
|
||||
(SearchOptionState::HasWhitespace(_), _) => {}
|
||||
(SearchOptionState::HasLineBreak(_), ' ' | '\t' | '\r' | '\n') => {}
|
||||
(SearchOptionState::HasLineBreak(ret), _) => {
|
||||
ret.push(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
match state {
|
||||
SearchOptionState::Normal => Cow::Borrowed(search_option),
|
||||
SearchOptionState::HasWhitespace(_) => Cow::Borrowed(search_option),
|
||||
SearchOptionState::HasLineBreak(ret) => Cow::Owned(ret),
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user