pub enum Token {
Show 16 variants
Heading(Vec<Token>, usize),
Emphasis {
level: usize,
content: Vec<Token>,
},
StrongEmphasis(Vec<Token>),
Code(String, String),
BlockQuote(String),
ListItem {
content: Vec<Token>,
ordered: bool,
number: Option<usize>,
},
Link(String, String),
Image(String, String),
Text(String),
Table {
headers: Vec<Vec<Token>>,
aligns: Vec<Alignment>,
rows: Vec<Vec<Vec<Token>>>,
},
TableAlignment(Alignment),
HtmlComment(String),
Math {
content: String,
display: bool,
},
Newline,
HorizontalRule,
Unknown(String),
}Expand description
Represents the different types of tokens that can be parsed from Markdown text. Each variant captures both the semantic meaning and associated content/metadata needed to properly render the element.
Variants§
Heading(Vec<Token>, usize)
A heading with nested content and level (e.g., # h1, ## h2)
Emphasis
Emphasized text with configurable level (1-3) for * or _ delimiters
StrongEmphasis(Vec<Token>)
Strong emphasis (bold) text using ** or __ delimiters
Code(String, String)
Code block with optional language specification and content
BlockQuote(String)
Block quote containing quoted text
ListItem
List item with nested content and type information
Link(String, String)
Link with display text and URL
Image(String, String)
Image with alt text and URL
Text(String)
Plain text content
Table
Table with header, alignment info, and rows
TableAlignment(Alignment)
Text alignment for table columns
HtmlComment(String)
HTML comment content
Math
LaTeX math block (inline $…$ or display $$…$$)
Newline
Line break
HorizontalRule
Horizontal rule (—)
Unknown(String)
Unknown or malformed token
Implementations§
Source§impl Token
impl Token
Sourcepub fn save_to_json_file(tokens: Vec<Token>, file_path: &str) -> Result<()>
pub fn save_to_json_file(tokens: Vec<Token>, file_path: &str) -> Result<()>
Saves tokens to a JSON file for visualization. Recursively formats nested tokens with proper indentation.
§Arguments
tokens- The tokens to savefile_path- Path to the output JSON file (e.g., “tokens.json”)
§Returns
Result indicating success or IO error
§Example
use markdown2pdf::markdown::Lexer;
let mut lexer = Lexer::new("# Title".to_string());
let tokens = lexer.parse().unwrap();
markdown2pdf::markdown::Token::save_to_json_file(tokens, "tokens.json").unwrap();Sourcefn to_readable_json(&self, indent_level: usize) -> String
fn to_readable_json(&self, indent_level: usize) -> String
Converts a token into a readable JSON representation for visualization. Recursively formats nested tokens with proper indentation.
Sourcefn tokens_to_readable_json(tokens: Vec<Token>) -> String
fn tokens_to_readable_json(tokens: Vec<Token>) -> String
Convenience method to convert a vector of tokens into a readable JSON array.
Source§impl Token
impl Token
Sourcepub fn collect_all_text(tokens: &[Token]) -> String
pub fn collect_all_text(tokens: &[Token]) -> String
Recursively extracts all text content from a token and its nested tokens. This is useful for collecting all characters used in a document for font subsetting.
§Returns
A string containing all text content from this token and any nested tokens.
§Example
use markdown2pdf::markdown::Token;
let tokens = vec![
Token::Heading(vec![Token::Text("Title".to_string())], 1),
Token::Text("Body text with ăâîșț".to_string()),
];
let all_text = Token::collect_all_text(&tokens);
assert!(all_text.contains("Title"));
assert!(all_text.contains("ăâîșț"));fn collect_text_recursive(&self, result: &mut String)
Trait Implementations§
impl StructuralPartialEq for Token
Auto Trait Implementations§
impl Freeze for Token
impl RefUnwindSafe for Token
impl Send for Token
impl Sync for Token
impl Unpin for Token
impl UnwindSafe for Token
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more§impl<T> Pointable for T
impl<T> Pointable for T
§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
§impl<U, T> ToOwnedObj<U> for Twhere
U: FromObjRef<T>,
impl<U, T> ToOwnedObj<U> for Twhere
U: FromObjRef<T>,
§fn to_owned_obj(&self, data: FontData<'_>) -> U
fn to_owned_obj(&self, data: FontData<'_>) -> U
T, using the provided data to resolve any offsets.