Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 7d831b1

Browse files
committedDec 17, 2024
WIP: Removing a lot of extra code in anticipation of kivikakk/comrak#494
1 parent aea2f66 commit 7d831b1

File tree

5 files changed

+21
-95
lines changed

5 files changed

+21
-95
lines changed
 

‎Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ debug = true
1515
aho-corasick = "1.1.3"
1616
bon = "2.3.0"
1717
clap = { version = "4.5.16", features = ["derive"] }
18-
comrak = "0.29.0"
18+
comrak = "0.31.0"
1919
derive_more = { version = "1.0.0", features = ["full"] }
2020
env_logger = "0.11.5"
2121
fuzzy-matcher = "0.3.7"

‎src/file/content/front_matter.rs

-49
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ use comrak::{
88
arena_tree::Node,
99
nodes::{Ast, NodeValue},
1010
};
11-
use log::debug;
12-
use miette::SourceSpan;
1311
use serde::Deserialize;
1412

1513
use super::wikilink::Alias;
@@ -34,53 +32,6 @@ impl FrontMatterVisitor {
3432
}
3533
}
3634

37-
fn get_frontmatter_from_any_node(node: &Node<RefCell<Ast>>) -> Option<String> {
38-
let mut node = node; // Its not the node which is now mutable, but you can change this value
39-
// First check ourselves
40-
if let NodeValue::FrontMatter(text) = &node.data.borrow().value {
41-
return Some(text.clone());
42-
}
43-
44-
// Then go to the root "Document" node
45-
while let Some(this_node) = node.parent() {
46-
node = this_node;
47-
if let NodeValue::Document = node.data.borrow().value {
48-
break;
49-
}
50-
}
51-
52-
// Down one is elther NodeValue::FrontMatter or Something else
53-
// If its frontmatter, return it
54-
if let Some(child) = &node.first_child() {
55-
if let NodeValue::FrontMatter(text) = &child.data.borrow().value {
56-
return Some(text.clone());
57-
}
58-
}
59-
None
60-
}
61-
62-
/// Spans get messed up because frontmatter is not considered part of the nodes sourcespan
63-
/// This adds the frontmatter length to the span offset
64-
pub fn repair_span_due_to_frontmatter(span: SourceSpan, node: &Node<RefCell<Ast>>) -> SourceSpan {
65-
let frontmatter = get_frontmatter_from_any_node(node);
66-
if let Some(frontmatter) = frontmatter {
67-
debug!("Frontmatter: {:?}", frontmatter);
68-
SourceSpan::new((span.offset() + frontmatter.len()).into(), span.len())
69-
} else {
70-
span
71-
}
72-
}
73-
74-
/// Remove the frontmatter from the source
75-
pub fn remove_frontmatter_from_source(source: &str, node: &Node<RefCell<Ast>>) -> String {
76-
let frontmatter = get_frontmatter_from_any_node(node);
77-
if let Some(frontmatter) = frontmatter {
78-
source[frontmatter.len()..].to_string()
79-
} else {
80-
source.to_string()
81-
}
82-
}
83-
8435
impl Visitor for FrontMatterVisitor {
8536
fn name(&self) -> &'static str {
8637
"FrontMatterVisitor"

‎src/file/content/wikilink.rs

+11-25
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ use comrak::{
1717
use miette::{SourceOffset, SourceSpan};
1818
use regex::Regex;
1919

20-
use super::front_matter::{remove_frontmatter_from_source, repair_span_due_to_frontmatter};
21-
2220
/// A linkable string, like that in a wikilink, or its corresponding filename
2321
/// Aliases are always lowercase
2422
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
@@ -108,24 +106,15 @@ impl Visitor for WikilinkVisitor {
108106
.get(1)
109107
.expect("The regex has 2 capture groups")
110108
.start();
111-
let text_without_frontmatter = remove_frontmatter_from_source(source, node);
112-
let sourcepos_start_offset_bytes = SourceOffset::from_location(
113-
text_without_frontmatter,
114-
sourcepos.start.line,
115-
sourcepos.start.column,
116-
)
117-
.offset();
109+
let sourcepos_start_offset_bytes =
110+
SourceOffset::from_location(text, sourcepos.start.line, sourcepos.start.column)
111+
.offset();
118112
let span = SourceSpan::new(
119113
(sourcepos_start_offset_bytes + capture_start_byte).into(),
120114
alias.char_len(),
121115
);
122-
let span_repaired = repair_span_due_to_frontmatter(span, node);
123-
self.wikilinks.push(
124-
Wikilink::builder()
125-
.alias(alias.clone())
126-
.span(span_repaired)
127-
.build(),
128-
);
116+
self.wikilinks
117+
.push(Wikilink::builder().alias(alias.clone()).span(span).build());
129118
}
130119
};
131120
match data {
@@ -136,16 +125,13 @@ impl Visitor for WikilinkVisitor {
136125
self.wikilinks.push(
137126
Wikilink::builder()
138127
.alias(Alias::new(url))
139-
.span(repair_span_due_to_frontmatter(
140-
SourceSpan::new(
141-
SourceOffset::from_location(
142-
remove_frontmatter_from_source(source, node),
143-
sourcepos.start.line,
144-
sourcepos.start.column,
145-
),
146-
url.len() + 4,
128+
.span(SourceSpan::new(
129+
SourceOffset::from_location(
130+
source,
131+
sourcepos.start.line,
132+
sourcepos.start.column,
147133
),
148-
node,
134+
url.len() + 4,
149135
))
150136
.build(),
151137
);

‎src/rules/unlinked_text.rs

+5-13
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
use crate::{
22
config::Config,
33
file::{
4-
content::{
5-
front_matter::{remove_frontmatter_from_source, repair_span_due_to_frontmatter},
6-
wikilink::{Alias, WikilinkVisitor},
7-
},
4+
content::wikilink::{Alias, WikilinkVisitor},
85
name::{get_filename, Filename},
96
},
107
sed::ReplacePair,
@@ -184,17 +181,12 @@ impl Visitor for UnlinkedTextVisitor {
184181
if "lorem" == alias.to_string() {
185182
println!("Found lorem");
186183
}
187-
let text_without_frontmatter = remove_frontmatter_from_source(source, node);
188-
let sourcepos_start_offset_bytes = SourceOffset::from_location(
189-
text_without_frontmatter,
190-
sourcepos.start.line,
191-
sourcepos.start.column,
192-
)
193-
.offset();
184+
let sourcepos_start_offset_bytes =
185+
SourceOffset::from_location(text, sourcepos.start.line, sourcepos.start.column)
186+
.offset();
194187
let byte_length = found.end() - found.start();
195188
let offset_bytes = sourcepos_start_offset_bytes + found.start();
196189
let span = SourceSpan::new(offset_bytes.into(), byte_length);
197-
let span_repaired = repair_span_due_to_frontmatter(span, node);
198190

199191
// Dont match inside wikilinks
200192
if let Some(parent) = parent {
@@ -204,7 +196,7 @@ impl Visitor for UnlinkedTextVisitor {
204196
}
205197
}
206198

207-
self.new_unlinked_texts.push((alias, span_repaired));
199+
self.new_unlinked_texts.push((alias, span));
208200
}
209201
}
210202
Ok(())

‎src/visitor.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ use std::{
55
rc::Rc,
66
};
77

8-
use comrak::{
9-
arena_tree::Node, nodes::Ast, parse_document, Arena, ExtensionOptionsBuilder, Options,
10-
};
8+
use comrak::{arena_tree::Node, nodes::Ast, parse_document, Arena, ExtensionOptions, Options};
119
use log::{debug, trace};
1210
use std::backtrace;
1311
use thiserror::Error;
@@ -135,11 +133,10 @@ pub fn parse(path: &PathBuf, visitors: Vec<Rc<RefCell<dyn Visitor>>>) -> Result<
135133

136134
// Parse the source code
137135
let arena = Arena::new();
138-
let options = ExtensionOptionsBuilder::default()
139-
.front_matter_delimiter(Some("---".to_string()))
136+
let options = ExtensionOptions::builder()
137+
.front_matter_delimiter("---".to_string())
140138
.wikilinks_title_before_pipe(true)
141-
.build()
142-
.expect("Constant");
139+
.build();
143140
let root = parse_document(
144141
&arena,
145142
&source,

0 commit comments

Comments
 (0)
Please sign in to comment.