Skip to content

Commit

Permalink
Merge pull request #158 from henriklovhaug/inline-link
Browse files Browse the repository at this point in the history
Make <link> a link
  • Loading branch information
henriklovhaug authored Sep 19, 2024
2 parents b716e0d + 4ec1e88 commit 43cb069
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 24 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ Some key actions are not configurable. Like the following:
> not get reassigned.
> Actions can only be assigned to single characters. Space, fn keys, ctrl+key,
> backspace etc., will not take affect and the default will be in use.
> backspace etc., will not take effect and the default will be in use.
```toml
# Keyboard actions
Expand Down
35 changes: 19 additions & 16 deletions src/md.pest
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
WHITESPACE_S = _{ " " | "\t" }

// Characters
alt_char = _{ (!(NEWLINE | WHITESPACE_S | "[" | "]") ~ ANY)+ }
b_char = _{ (!(NEWLINE | comment | WHITESPACE_S | "**") ~ ANY)+ }
c_char = _{ (!(NEWLINE | WHITESPACE_S | "`") ~ ANY)+ }
c_line_char = _{ (!(NEWLINE | "```" | "~~~") ~ ANY)+ }
comment_char = _{ (!(NEWLINE | "-->") ~ ANY)+ }
digit = _{ '0'..'9' }
i_char = _{ (!(NEWLINE | comment | WHITESPACE_S | "_" | "*") ~ ANY)+ }
indent = { WHITESPACE_S* }
latex_char = _{ (!(NEWLINE | WHITESPACE_S | "$") ~ ANY)+ }
link_char = _{ (!(NEWLINE | WHITESPACE_S | "[" | "]" | "(" | ")") ~ ANY)+ }
p_char = _{ (!(NEWLINE | comment | code | bold_italic | italic | bold | strikethrough | latex | WHITESPACE_S) ~ ANY)+ }
s_char = _{ (!(NEWLINE | comment | WHITESPACE_S | "~~") ~ ANY)+ }
t_char = _{ (!(NEWLINE | comment | code | bold_italic | italic | bold | strikethrough | latex | WHITESPACE_S | "|") ~ ANY)+ }
wiki_link_char = _{ (!(NEWLINE | WHITESPACE_S | "|" | "[[" | "]]") ~ ANY)+ }
alt_char = _{ (!(NEWLINE | WHITESPACE_S | "[" | "]") ~ ANY)+ }
b_char = _{ (!(NEWLINE | comment | WHITESPACE_S | "**") ~ ANY)+ }
c_char = _{ (!(NEWLINE | WHITESPACE_S | "`") ~ ANY)+ }
c_line_char = _{ (!(NEWLINE | "```" | "~~~") ~ ANY)+ }
comment_char = _{ (!(NEWLINE | "-->") ~ ANY)+ }
digit = _{ '0'..'9' }
i_char = _{ (!(NEWLINE | comment | WHITESPACE_S | "_" | "*") ~ ANY)+ }
indent = { WHITESPACE_S* }
latex_char = _{ (!(NEWLINE | WHITESPACE_S | "$") ~ ANY)+ }
link_char = _{ (!(NEWLINE | WHITESPACE_S | "[" | "]" | "(" | ")") ~ ANY)+ }
p_char = _{ (!(NEWLINE | comment | code | bold_italic | italic | bold | strikethrough | latex | WHITESPACE_S | link) ~ ANY)+ }
s_char = _{ (!(NEWLINE | comment | WHITESPACE_S | "~~") ~ ANY)+ }
t_char = _{ (!(NEWLINE | comment | code | bold_italic | italic | bold | strikethrough | latex | WHITESPACE_S | "|") ~ ANY)+ }
wiki_link_char = _{ (!(NEWLINE | WHITESPACE_S | "|" | "[[" | "]]") ~ ANY)+ }
inline_link_char = _{ (!(NEWLINE | WHITESPACE_S | "<" | ">") ~ ANY)+ }

// Words
word = {
Expand All @@ -36,6 +37,7 @@ strikethrough_word = { WHITESPACE_S* ~ s_char+ }
wiki_link_alone = { wiki_link_char+ }
wiki_link_data = { wiki_link_char+ }
wiki_link_word = { (link_char | WHITESPACE_S)+ }
inline_link = { inline_link_char+ }

// Prefixes
task_open = { "- [ ] " }
Expand All @@ -62,14 +64,15 @@ bold_italic = {
code = { NEWLINE? ~ WHITESPACE_S* ~ ("`" ~ code_word+ ~ "`") | ("```" ~ code_word+ ~ "```") }
code_line = { NEWLINE ~ (c_line_char | WHITESPACE_S)* }
latex = { NEWLINE? ~ WHITESPACE_S* ~ !"\\" ~ "$"+ ~ WHITESPACE_S? ~ (latex_word | (NEWLINE ~ quote_prefix?))+ ~ "$"+ }
link = { NEWLINE? ~ WHITESPACE_S* ~ (link_line | wiki_link) }
link = { NEWLINE? ~ WHITESPACE_S* ~ (link_line | wiki_link | inline_link_wrapper) }
link_line = _{ "[" ~ (link_word | NEWLINE)+ ~ "]" ~ "(" ~ link_data+ ~ ")" }
inline_link_wrapper = _{ !comment ~ "<" ~ inline_link ~ ">" }
wiki_link = _{ ("[[" ~ wiki_link_alone+ ~ "]]") | ("[[" ~ wiki_link_data+ ~ "|" ~ wiki_link_word+ ~ "]]") }
normal = _{ word+ }
o_list_counter = { digit+ ~ ". " }
programming_language = { (!NEWLINE ~ ANY)+ }
strikethrough = { NEWLINE? ~ WHITESPACE_S* ~ !"\\" ~ "~~" ~ !"~" ~ (strikethrough_word | (NEWLINE ~ quote_prefix?))+ ~ "~~" }
t_normal = _{ t_word+ }
wiki_link = _{ ("[[" ~ wiki_link_alone+ ~ "]]") | ("[[" ~ wiki_link_data+ ~ "|" ~ wiki_link_word+ ~ "]]") }

italic = {
(NEWLINE? ~ WHITESPACE_S* ~ !"\\" ~ "*" ~ (italic_word | (NEWLINE ~ quote_prefix?))+ ~ "*")
Expand Down
2 changes: 1 addition & 1 deletion src/nodes/word.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl From<MdParseEnum> for WordType {
MdParseEnum::Bold => WordType::Bold,
MdParseEnum::Italic => WordType::Italic,
MdParseEnum::Strikethrough => WordType::Strikethrough,
MdParseEnum::Link | MdParseEnum::WikiLink => WordType::Link,
MdParseEnum::Link | MdParseEnum::WikiLink | MdParseEnum::InlineLink => WordType::Link,
MdParseEnum::BoldItalic => WordType::BoldItalic,

MdParseEnum::Digit => WordType::ListMarker,
Expand Down
15 changes: 9 additions & 6 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ fn parse_component(parse_node: ParseNode) -> Component {
.dedup_by(|x, y| *x == ' ' && *y == ' ')
.collect();

if node.kind() == MdParseEnum::WikiLink {
if matches!(node.kind(), MdParseEnum::WikiLink | MdParseEnum::InlineLink) {
let comp = Word::new(content.clone(), WordType::LinkData);
words.push(comp);
}
Expand All @@ -165,7 +165,7 @@ fn parse_component(parse_node: ParseNode) -> Component {
let word_type = WordType::from(node.kind());
let mut content = node.content().to_owned();

if node.kind() == MdParseEnum::WikiLink {
if matches!(node.kind(), MdParseEnum::WikiLink | MdParseEnum::InlineLink) {
let comp = Word::new(content.clone(), WordType::LinkData);
words.push(comp);
}
Expand Down Expand Up @@ -207,7 +207,7 @@ fn parse_component(parse_node: ParseNode) -> Component {
let word_type = WordType::from(node.kind());
let mut content = node.content().to_owned();

if node.kind() == MdParseEnum::WikiLink {
if matches!(node.kind(), MdParseEnum::WikiLink | MdParseEnum::InlineLink) {
let comp = Word::new(content.clone(), WordType::LinkData);
words.push(comp);
}
Expand All @@ -232,7 +232,7 @@ fn parse_component(parse_node: ParseNode) -> Component {
let word_type = WordType::from(node.kind());
let mut content = node.content().to_owned();

if node.kind() == MdParseEnum::WikiLink {
if matches!(node.kind(), MdParseEnum::WikiLink | MdParseEnum::InlineLink) {
let comp = Word::new(content.clone(), WordType::LinkData);
words.push(comp);
}
Expand Down Expand Up @@ -279,7 +279,7 @@ fn parse_component(parse_node: ParseNode) -> Component {
.collect(),
};

if node.kind() == MdParseEnum::WikiLink {
if matches!(node.kind(), MdParseEnum::WikiLink | MdParseEnum::InlineLink) {
let comp = Word::new(content.clone(), WordType::LinkData);
inner_words.push(comp);
}
Expand Down Expand Up @@ -324,7 +324,7 @@ fn parse_component(parse_node: ParseNode) -> Component {
let word_type = WordType::from(word.kind());
let mut content = word.content().to_owned();

if word.kind() == MdParseEnum::WikiLink {
if matches!(word.kind(), MdParseEnum::WikiLink | MdParseEnum::InlineLink) {
let comp = Word::new(content.clone(), WordType::LinkData);
inner_words.push(comp);
}
Expand Down Expand Up @@ -491,6 +491,7 @@ pub enum MdParseEnum {
Indent,
Italic,
ItalicStr,
InlineLink,
Link,
LinkData,
ListContainer,
Expand Down Expand Up @@ -534,6 +535,7 @@ impl From<Rule> for MdParseEnum {
Rule::programming_language => Self::PLanguage,
Rule::link_word | Rule::link_line | Rule::link | Rule::wiki_link_word => Self::Link,
Rule::wiki_link_alone => Self::WikiLink,
Rule::inline_link | Rule::inline_link_wrapper => Self::InlineLink,
Rule::o_list_counter | Rule::digit => Self::Digit,
Rule::task_open => Self::TaskOpen,
Rule::task_complete => Self::TaskClosed,
Expand Down Expand Up @@ -591,6 +593,7 @@ impl From<Rule> for MdParseEnum {
| Rule::i_char
| Rule::latex_char
| Rule::quote_marking
| Rule::inline_link_char
| Rule::s_char
| Rule::WHITESPACE_S
| Rule::wiki_link => todo!(),
Expand Down

0 comments on commit 43cb069

Please sign in to comment.