From 7d831b10b01ece546c43e36c770e382f0a95fa87 Mon Sep 17 00:00:00 2001 From: Ryan Peach Date: Tue, 17 Dec 2024 02:03:43 -0500 Subject: [PATCH] WIP: Removing a lot of extra code in anticipation of https://github.com/kivikakk/comrak/pull/494 --- Cargo.toml | 2 +- src/file/content/front_matter.rs | 49 -------------------------------- src/file/content/wikilink.rs | 36 +++++++---------------- src/rules/unlinked_text.rs | 18 ++++-------- src/visitor.rs | 11 +++---- 5 files changed, 21 insertions(+), 95 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index af043c0..b47ca15 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,7 +15,7 @@ debug = true aho-corasick = "1.1.3" bon = "2.3.0" clap = { version = "4.5.16", features = ["derive"] } -comrak = "0.29.0" +comrak = "0.31.0" derive_more = { version = "1.0.0", features = ["full"] } env_logger = "0.11.5" fuzzy-matcher = "0.3.7" diff --git a/src/file/content/front_matter.rs b/src/file/content/front_matter.rs index 17e087b..29f0d06 100644 --- a/src/file/content/front_matter.rs +++ b/src/file/content/front_matter.rs @@ -8,8 +8,6 @@ use comrak::{ arena_tree::Node, nodes::{Ast, NodeValue}, }; -use log::debug; -use miette::SourceSpan; use serde::Deserialize; use super::wikilink::Alias; @@ -34,53 +32,6 @@ impl FrontMatterVisitor { } } -fn get_frontmatter_from_any_node(node: &Node>) -> Option { - let mut node = node; // Its not the node which is now mutable, but you can change this value - // First check ourselves - if let NodeValue::FrontMatter(text) = &node.data.borrow().value { - return Some(text.clone()); - } - - // Then go to the root "Document" node - while let Some(this_node) = node.parent() { - node = this_node; - if let NodeValue::Document = node.data.borrow().value { - break; - } - } - - // Down one is elther NodeValue::FrontMatter or Something else - // If its frontmatter, return it - if let Some(child) = &node.first_child() { - if let NodeValue::FrontMatter(text) = &child.data.borrow().value { - return Some(text.clone()); - } - } - None -} - -/// Spans get messed up because frontmatter is not considered part of the nodes sourcespan -/// This adds the frontmatter length to the span offset -pub fn repair_span_due_to_frontmatter(span: SourceSpan, node: &Node>) -> SourceSpan { - let frontmatter = get_frontmatter_from_any_node(node); - if let Some(frontmatter) = frontmatter { - debug!("Frontmatter: {:?}", frontmatter); - SourceSpan::new((span.offset() + frontmatter.len()).into(), span.len()) - } else { - span - } -} - -/// Remove the frontmatter from the source -pub fn remove_frontmatter_from_source(source: &str, node: &Node>) -> String { - let frontmatter = get_frontmatter_from_any_node(node); - if let Some(frontmatter) = frontmatter { - source[frontmatter.len()..].to_string() - } else { - source.to_string() - } -} - impl Visitor for FrontMatterVisitor { fn name(&self) -> &'static str { "FrontMatterVisitor" diff --git a/src/file/content/wikilink.rs b/src/file/content/wikilink.rs index 78cdf6e..0051f6b 100644 --- a/src/file/content/wikilink.rs +++ b/src/file/content/wikilink.rs @@ -17,8 +17,6 @@ use comrak::{ use miette::{SourceOffset, SourceSpan}; use regex::Regex; -use super::front_matter::{remove_frontmatter_from_source, repair_span_due_to_frontmatter}; - /// A linkable string, like that in a wikilink, or its corresponding filename /// Aliases are always lowercase #[derive(Clone, Debug, Eq, PartialEq, Hash)] @@ -108,24 +106,15 @@ impl Visitor for WikilinkVisitor { .get(1) .expect("The regex has 2 capture groups") .start(); - let text_without_frontmatter = remove_frontmatter_from_source(source, node); - let sourcepos_start_offset_bytes = SourceOffset::from_location( - text_without_frontmatter, - sourcepos.start.line, - sourcepos.start.column, - ) - .offset(); + let sourcepos_start_offset_bytes = + SourceOffset::from_location(text, sourcepos.start.line, sourcepos.start.column) + .offset(); let span = SourceSpan::new( (sourcepos_start_offset_bytes + capture_start_byte).into(), alias.char_len(), ); - let span_repaired = repair_span_due_to_frontmatter(span, node); - self.wikilinks.push( - Wikilink::builder() - .alias(alias.clone()) - .span(span_repaired) - .build(), - ); + self.wikilinks + .push(Wikilink::builder().alias(alias.clone()).span(span).build()); } }; match data { @@ -136,16 +125,13 @@ impl Visitor for WikilinkVisitor { self.wikilinks.push( Wikilink::builder() .alias(Alias::new(url)) - .span(repair_span_due_to_frontmatter( - SourceSpan::new( - SourceOffset::from_location( - remove_frontmatter_from_source(source, node), - sourcepos.start.line, - sourcepos.start.column, - ), - url.len() + 4, + .span(SourceSpan::new( + SourceOffset::from_location( + source, + sourcepos.start.line, + sourcepos.start.column, ), - node, + url.len() + 4, )) .build(), ); diff --git a/src/rules/unlinked_text.rs b/src/rules/unlinked_text.rs index f10b9f4..b07bf8d 100644 --- a/src/rules/unlinked_text.rs +++ b/src/rules/unlinked_text.rs @@ -1,10 +1,7 @@ use crate::{ config::Config, file::{ - content::{ - front_matter::{remove_frontmatter_from_source, repair_span_due_to_frontmatter}, - wikilink::{Alias, WikilinkVisitor}, - }, + content::wikilink::{Alias, WikilinkVisitor}, name::{get_filename, Filename}, }, sed::ReplacePair, @@ -184,17 +181,12 @@ impl Visitor for UnlinkedTextVisitor { if "lorem" == alias.to_string() { println!("Found lorem"); } - let text_without_frontmatter = remove_frontmatter_from_source(source, node); - let sourcepos_start_offset_bytes = SourceOffset::from_location( - text_without_frontmatter, - sourcepos.start.line, - sourcepos.start.column, - ) - .offset(); + let sourcepos_start_offset_bytes = + SourceOffset::from_location(text, sourcepos.start.line, sourcepos.start.column) + .offset(); let byte_length = found.end() - found.start(); let offset_bytes = sourcepos_start_offset_bytes + found.start(); let span = SourceSpan::new(offset_bytes.into(), byte_length); - let span_repaired = repair_span_due_to_frontmatter(span, node); // Dont match inside wikilinks if let Some(parent) = parent { @@ -204,7 +196,7 @@ impl Visitor for UnlinkedTextVisitor { } } - self.new_unlinked_texts.push((alias, span_repaired)); + self.new_unlinked_texts.push((alias, span)); } } Ok(()) diff --git a/src/visitor.rs b/src/visitor.rs index 5683aa5..d41c278 100644 --- a/src/visitor.rs +++ b/src/visitor.rs @@ -5,9 +5,7 @@ use std::{ rc::Rc, }; -use comrak::{ - arena_tree::Node, nodes::Ast, parse_document, Arena, ExtensionOptionsBuilder, Options, -}; +use comrak::{arena_tree::Node, nodes::Ast, parse_document, Arena, ExtensionOptions, Options}; use log::{debug, trace}; use std::backtrace; use thiserror::Error; @@ -135,11 +133,10 @@ pub fn parse(path: &PathBuf, visitors: Vec>>) -> Result< // Parse the source code let arena = Arena::new(); - let options = ExtensionOptionsBuilder::default() - .front_matter_delimiter(Some("---".to_string())) + let options = ExtensionOptions::builder() + .front_matter_delimiter("---".to_string()) .wikilinks_title_before_pipe(true) - .build() - .expect("Constant"); + .build(); let root = parse_document( &arena, &source,