Markdown parser fork with extended syntax for personal use.
at main 37 lines 1.1 kB view raw
1//! Lots of helpers for dealing with SWC, particularly from unist, and for 2//! building its ES AST. 3 4use swc_core::common::{BytePos, Span, DUMMY_SP}; 5use swc_core::ecma::visit::{noop_visit_mut_type, VisitMut}; 6 7/// Visitor to fix SWC byte positions by removing a prefix. 8/// 9/// > 👉 **Note**: SWC byte positions are offset by one: they are `0` when they 10/// > are missing or incremented by `1` when valid. 11#[derive(Debug, Default, Clone)] 12pub struct RewritePrefixContext { 13 /// Size of prefix considered outside this tree. 14 pub prefix_len: u32, 15} 16 17impl VisitMut for RewritePrefixContext { 18 noop_visit_mut_type!(); 19 20 /// Rewrite spans. 21 fn visit_mut_span(&mut self, span: &mut Span) { 22 let mut result = DUMMY_SP; 23 if span.lo.0 > self.prefix_len && span.hi.0 > self.prefix_len { 24 result = create_span(span.lo.0 - self.prefix_len, span.hi.0 - self.prefix_len); 25 } 26 27 *span = result; 28 } 29} 30 31/// Generate a span. 32pub fn create_span(lo: u32, hi: u32) -> Span { 33 Span { 34 lo: BytePos(lo), 35 hi: BytePos(hi), 36 } 37}